mirror of https://gitlab.com/ceda_ei/sky
Add weather_to_text for hourly. Add endpoints with / at end
This commit is contained in:
parent
46364f127f
commit
e976173ae8
53
server.py
53
server.py
|
@ -35,8 +35,7 @@ def get_weather(coordinates):
|
|||
|
||||
|
||||
def weather_to_text(forecast, kind, unit):
|
||||
if kind == "daily":
|
||||
text = forecast["daily"]["summary"] + "\n\n"
|
||||
text = forecast[kind]["summary"] + "\n\n"
|
||||
row_0 = Columnizer()
|
||||
row_1 = Columnizer()
|
||||
# Add today
|
||||
|
@ -54,8 +53,42 @@ def weather_to_text(forecast, kind, unit):
|
|||
# today += ["", forecast["currently"]["summary"]]
|
||||
row_0.add_column(today)
|
||||
count = 1
|
||||
|
||||
for i in forecast["daily"]["data"]:
|
||||
if kind == "daily":
|
||||
diff = 1
|
||||
elif kind == "hourly":
|
||||
diff = 3
|
||||
for j in range(0, len(forecast[kind]["data"]), diff):
|
||||
i = forecast[kind]["data"][j]
|
||||
x = []
|
||||
date = datetime.fromtimestamp(i["time"],
|
||||
timezone(forecast["timezone"]))
|
||||
if kind == "daily":
|
||||
x += date.strftime("%d %B\n%A").split("\n")
|
||||
elif kind == "hourly":
|
||||
x += [date.strftime("%H:%M"), ""]
|
||||
x += [*arts[i["icon"]]]
|
||||
x += [""]
|
||||
if unit == "c":
|
||||
if kind == "daily":
|
||||
x += ["Max: {0:.1f}°C".format(5*(i["temperatureMax"]-32)/9)]
|
||||
x += ["Min: {0:.1f}°C".format(5*(i["temperatureMin"]-32)/9)]
|
||||
elif kind == "hourly":
|
||||
x += ["Temp: {0:.1f}°C".format(5*(i["temperature"]-32)/9)]
|
||||
else:
|
||||
if kind == "daily":
|
||||
x += ["Max: {0:.1f}°F".format(i["temperatureMax"])]
|
||||
x += ["Min: {0:.1f}°F".format(i["temperatureMin"])]
|
||||
elif kind == "hourly":
|
||||
x += ["Temp: {0:.1f}°F".format(i["temperature"])]
|
||||
# x += [i["summary"]]
|
||||
if count < 4:
|
||||
row_0.add_column(x)
|
||||
elif count < 8:
|
||||
row_1.add_column(x)
|
||||
else:
|
||||
break
|
||||
count += 1
|
||||
for i in forecast[kind]["data"]:
|
||||
x = []
|
||||
date = datetime.fromtimestamp(i["time"],
|
||||
timezone(forecast["timezone"]))
|
||||
|
@ -63,11 +96,17 @@ def weather_to_text(forecast, kind, unit):
|
|||
x += [*arts[i["icon"]]]
|
||||
x += [""]
|
||||
if unit == "c":
|
||||
if kind == "daily":
|
||||
x += ["Max: {0:.1f}°C".format(5*(i["temperatureMax"]-32)/9)]
|
||||
x += ["Min: {0:.1f}°C".format(5*(i["temperatureMin"]-32)/9)]
|
||||
elif kind == "hourly":
|
||||
x += ["Temp: {0:.1f}°C".format(5*(i["temperature"]-32)/9)]
|
||||
else:
|
||||
if kind == "daily":
|
||||
x += ["Max: {0:.1f}°F".format(i["temperatureMax"])]
|
||||
x += ["Min: {0:.1f}°F".format(i["temperatureMin"])]
|
||||
elif kind == "hourly":
|
||||
x += ["Temp: {0:.1f}°F".format(i["temperature"])]
|
||||
# x += [i["summary"]]
|
||||
if count < 4:
|
||||
row_0.add_column(x)
|
||||
|
@ -98,11 +137,17 @@ def main(location, geocoder, cache_db, kind, unit):
|
|||
app.add_url_rule('/<location>', 'location', lambda location:
|
||||
main(location, geocoder, cache_db, "daily", "c"))
|
||||
|
||||
app.add_url_rule('/<location>/', 'location/', lambda location:
|
||||
main(location, geocoder, cache_db, "daily", "c"))
|
||||
|
||||
app.add_url_rule('/<location>/t', 'today_location', lambda location:
|
||||
main(location, geocoder, cache_db, "hourly", "c"))
|
||||
|
||||
app.add_url_rule('/f/<location>', 'location_f', lambda location:
|
||||
main(location, geocoder, cache_db, "daily", "f"))
|
||||
|
||||
app.add_url_rule('/f/<location>/', 'location_f/', lambda location:
|
||||
main(location, geocoder, cache_db, "daily", "f"))
|
||||
|
||||
app.add_url_rule('/f/<location>/t', 'today_location_f', lambda location:
|
||||
main(location, geocoder, cache_db, "hourly", "f"))
|
||||
|
|
Loading…
Reference in New Issue