forked from mendhak/waveshare-epaper-display
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen-weather-get.py
More file actions
124 lines (97 loc) · 4.81 KB
/
screen-weather-get.py
File metadata and controls
124 lines (97 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python
import datetime
import sys
import os
import logging
from weather_providers import climacell, openweathermap, metofficedatahub, metno, accuweather, visualcrossing
from utility import update_svg, configure_logging
import textwrap
configure_logging()
def format_weather_description(weather_description):
if len(weather_description) < 20:
return {1: weather_description, 2: ''}
splits = textwrap.fill(weather_description, 20, break_long_words=False,
max_lines=2, placeholder='...').split('\n')
weather_dict = {1: splits[0]}
weather_dict[2] = splits[1] if len(splits) > 1 else ''
return weather_dict
def main():
# gather relevant environment configs
climacell_apikey = os.getenv("CLIMACELL_APIKEY")
openweathermap_apikey = os.getenv("OPENWEATHERMAP_APIKEY")
metoffice_clientid = os.getenv("METOFFICEDATAHUB_CLIENT_ID")
metoffice_clientsecret = os.getenv("METOFFICEDATAHUB_CLIENT_SECRET")
accuweather_apikey = os.getenv("ACCUWEATHER_APIKEY")
accuweather_locationkey = os.getenv("ACCUWEATHER_LOCATIONKEY")
metno_self_id = os.getenv("METNO_SELF_IDENTIFICATION")
visualcrossing_apikey = os.getenv("VISUALCROSSING_APIKEY")
location_lat = os.getenv("WEATHER_LATITUDE", "51.3656")
location_long = os.getenv("WEATHER_LONGITUDE", "-0.1963")
weather_format = os.getenv("WEATHER_FORMAT", "CELSIUS")
if (
not climacell_apikey
and not openweathermap_apikey
and not metoffice_clientid
and not accuweather_apikey
and not metno_self_id
and not visualcrossing_apikey
):
logging.error("No weather provider has been configured (Climacell, OpenWeatherMap, MetOffice, AccuWeather, Met.no, VisualCrossing...)")
sys.exit(1)
if (weather_format == "CELSIUS"):
units = "metric"
else:
units = "imperial"
if visualcrossing_apikey:
logging.info("Getting weather from Visual Crossing")
weather_provider = visualcrossing.VisualCrossing(visualcrossing_apikey, location_lat, location_long, units)
elif metno_self_id:
logging.info("Getting weather from Met.no")
weather_provider = metno.MetNo(metno_self_id, location_lat, location_long, units)
elif accuweather_apikey:
logging.info("Getting weather from Accuweather")
weather_provider = accuweather.AccuWeather(accuweather_apikey, location_lat,
location_long,
accuweather_locationkey,
units)
elif metoffice_clientid:
logging.info("Getting weather from Met Office Weather Datahub")
weather_provider = metofficedatahub.MetOffice(metoffice_clientid,
metoffice_clientsecret,
location_lat,
location_long,
units)
elif openweathermap_apikey:
logging.info("Getting weather from OpenWeatherMap")
weather_provider = openweathermap.OpenWeatherMap(openweathermap_apikey,
location_lat,
location_long,
units)
elif climacell_apikey:
logging.info("Getting weather from Climacell")
weather_provider = climacell.Climacell(climacell_apikey, location_lat, location_long, units)
weather = weather_provider.get_weather()
logging.info("weather - {}".format(weather))
if not weather:
logging.error("Unable to fetch weather payload. SVG will not be updated.")
return
degrees = "°C" if units == "metric" else "°F"
weather_desc = format_weather_description(weather["description"])
output_dict = {
'LOW_ONE': "{}{}".format(str(round(weather['temperatureMin'])), degrees),
'HIGH_ONE': "{}{}".format(str(round(weather['temperatureMax'])), degrees),
'ICON_ONE': weather["icon"],
'WEATHER_DESC_1': weather_desc[1],
'WEATHER_DESC_2': weather_desc[2],
'TIME_NOW': datetime.datetime.now().strftime("%-I:%M %p"),
'DAY_ONE': datetime.datetime.now().strftime("%b %-d, %Y"),
'DAY_NAME': datetime.datetime.now().strftime("%A"),
'ALERT_MESSAGE': "" # unused, see: https://github.com/mendhak/waveshare-epaper-display/issues/13
}
logging.debug("main() - {}".format(output_dict))
logging.info("Updating SVG")
template_svg_filename = 'screen-template.svg'
output_svg_filename = 'screen-output-weather.svg'
update_svg(template_svg_filename, output_svg_filename, output_dict)
if __name__ == "__main__":
main()