mirror of https://gitlab.com/ceda_ei/sky
Fix #1. Add full_name to cache_db. Show name in output.
This commit is contained in:
parent
fb8cf56ef6
commit
ffdaef11d8
|
@ -1 +1 @@
|
||||||
CREATE TABLE IF NOT EXISTS location(name varchar(255) PRIMARY KEY, lat real, lon real);
|
CREATE TABLE IF NOT EXISTS location(name varchar(255) PRIMARY KEY, lat real, lon real, full_name varchar(255));
|
||||||
|
|
19
server.py
19
server.py
|
@ -16,17 +16,18 @@ cache_db = sqlite3.connect("cache.sqlite", check_same_thread=False)
|
||||||
|
|
||||||
|
|
||||||
def get_coordinates(location, geocoder, cache_db):
|
def get_coordinates(location, geocoder, cache_db):
|
||||||
row = cache_db.execute("select lat,lon from location where name = ?;",
|
row = cache_db.execute("select lat,lon,full_name from location where "
|
||||||
(location,)).fetchone()
|
"name = ?", (location,)).fetchone()
|
||||||
|
|
||||||
if row is not None:
|
if row is not None:
|
||||||
return (row[0], row[1])
|
return (row[0], row[1], row[2])
|
||||||
|
|
||||||
loc = geocoder.forward(location)
|
loc = geocoder.forward(location)
|
||||||
coordinates = loc.geojson()['features'][0]['center']
|
coordinates = loc.geojson()['features'][0]['center']
|
||||||
cache_db.execute("insert into location(name, lon, lat) values(?,?,?)",
|
name = loc.geojson()['features'][0]['place_name']
|
||||||
(location, *coordinates))
|
cache_db.execute("insert into location(name, lon, lat, full_name) values("
|
||||||
return (coordinates[1], coordinates[0])
|
"?,?,?,?)", (location, *coordinates, name))
|
||||||
|
return (coordinates[1], coordinates[0], name)
|
||||||
|
|
||||||
|
|
||||||
def get_weather(coordinates):
|
def get_weather(coordinates):
|
||||||
|
@ -34,8 +35,8 @@ def get_weather(coordinates):
|
||||||
coordinates[1])
|
coordinates[1])
|
||||||
|
|
||||||
|
|
||||||
def weather_to_text(forecast, kind, unit):
|
def weather_to_text(forecast, kind, unit, name):
|
||||||
text = forecast[kind]["summary"] + "\n\n"
|
text = name + "\n" + forecast[kind]["summary"] + "\n\n"
|
||||||
row_0 = Columnizer()
|
row_0 = Columnizer()
|
||||||
row_1 = Columnizer()
|
row_1 = Columnizer()
|
||||||
# Add today
|
# Add today
|
||||||
|
@ -138,7 +139,7 @@ def index():
|
||||||
def main(location, geocoder, cache_db, kind, unit):
|
def main(location, geocoder, cache_db, kind, unit):
|
||||||
coordinates = get_coordinates(location, geocoder, cache_db)
|
coordinates = get_coordinates(location, geocoder, cache_db)
|
||||||
weather = get_weather(coordinates)
|
weather = get_weather(coordinates)
|
||||||
text = weather_to_text(weather, kind, unit)
|
text = weather_to_text(weather, kind, unit, coordinates[2])
|
||||||
return ("\n" + text + "\n\n" + config.source + "\nPowered by Dark Sky - "
|
return ("\n" + text + "\n\n" + config.source + "\nPowered by Dark Sky - "
|
||||||
"https://darksky.net/poweredby/\n")
|
"https://darksky.net/poweredby/\n")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue