diff --git a/sample.config.py b/sample.config.py index 77cf5cf..ee504b0 100644 --- a/sample.config.py +++ b/sample.config.py @@ -3,3 +3,7 @@ mapbox_key = "pk.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # Access token for Dark Sky dark_sky_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + +# Source line. Will be attached to the end of every message +# This line is necessary for complying with AGPL. +source = "Source: https://gitlab.com/ceda_ei/sky\n" diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..bee50fd --- /dev/null +++ b/schema.sql @@ -0,0 +1 @@ +CREATE TABLE IF NOT EXISTS location(name varchar(255) PRIMARY KEY, lat real, lon real); diff --git a/server.py b/server.py index fbe3a8b..674f60d 100644 --- a/server.py +++ b/server.py @@ -10,16 +10,40 @@ app = Flask(__name__) cache_db = sqlite3.connect("cache.sqlite") +def get_coordinates(location, geocoder, cache_db): + row = cache_db.execute("select lat,lon from location where name = ?;", + (location,)).fetchone() + + if row is not None: + return (row[0], row[1]) + + loc = geocoder.forward(location) + coordinates = loc.geojson()['features'][0]['center'] + cache_db.execute("insert into location(name, lat, lon) values(?,?,?)", + (location, *coordinates)) + return tuple(coordinates) + + +def get_weather(coordinates): + return coordinates + + +def weather_to_text(text): + return str(text) + + @app.route('/') def index(): text = ("To get the weather for a location, run curl " - "https://sky.webionite.com/location. \n" - "Source: https://gitlab.com/ceda_ei/sky\n") + "https://sky.webionite.com/location. \n" + config.source) return text def main(location, geocoder, cache_db): - return + coordinates = get_coordinates(location, geocoder, cache_db) + weather = get_weather(coordinates) + text = weather_to_text(weather) + return text + "\n" + config.source app.add_url_rule('/', 'location', lambda location: