forked from mendhak/waveshare-epaper-display
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen-weather-get.py
192 lines (149 loc) · 7.44 KB
/
screen-weather-get.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/python
import datetime
import sys
import os
import logging
from weather_providers import climacell, openweathermap, metofficedatahub, metno, meteireann, accuweather, visualcrossing, weathergov, smhi
from alert_providers import metofficerssfeed, weathergovalerts
from alert_providers import meteireann as meteireannalertprovider
from utility import get_formatted_time, update_svg, configure_logging, configure_locale
import textwrap
import html
configure_locale()
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 get_weather(location_lat, location_long, units):
# gather relevant environment configs
climacell_apikey = os.getenv("CLIMACELL_APIKEY")
openweathermap_apikey = os.getenv("OPENWEATHERMAP_APIKEY")
metoffice_apikey = os.getenv("METOFFICEDATAHUB_API_KEY")
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")
use_met_eireann = os.getenv("WEATHER_MET_EIREANN")
weathergov_self_id = os.getenv("WEATHERGOV_SELF_IDENTIFICATION")
smhi_self_id = os.getenv("SMHI_SELF_IDENTIFICATION")
if (
not climacell_apikey
and not openweathermap_apikey
and not metoffice_apikey
and not accuweather_apikey
and not metno_self_id
and not visualcrossing_apikey
and not use_met_eireann
and not weathergov_self_id
and not smhi_self_id
):
logging.error("No weather provider has been configured (Climacell, OpenWeatherMap, Weather.gov, MetOffice, AccuWeather, Met.no, Met Eireann, VisualCrossing...)")
sys.exit(1)
if visualcrossing_apikey:
logging.info("Getting weather from Visual Crossing")
weather_provider = visualcrossing.VisualCrossing(visualcrossing_apikey, location_lat, location_long, units)
elif use_met_eireann:
logging.info("Getting weather from Met Eireann")
weather_provider = meteireann.MetEireann(location_lat, location_long, units)
elif weathergov_self_id:
logging.info("Getting weather from Weather.gov")
weather_provider = weathergov.WeatherGov(weathergov_self_id, 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_apikey:
logging.info("Getting weather from Met Office Weather Datahub")
weather_provider = metofficedatahub.MetOffice(metoffice_apikey,
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)
elif smhi_self_id:
logging.info("Getting weather from SMHI")
weather_provider = smhi.SMHI(smhi_self_id, location_lat, location_long, units)
weather = weather_provider.get_weather()
logging.info("weather - {}".format(weather))
return weather
def format_alert_description(alert_message):
return html.escape(alert_message)
def get_alert_message(location_lat, location_long):
alert_message = ""
alert_metoffice_feed_url = os.getenv("ALERT_METOFFICE_FEED_URL")
alert_weathergov_self_id = os.getenv("ALERT_WEATHERGOV_SELF_IDENTIFICATION")
alert_meteireann_feed_url = os.getenv("ALERT_MET_EIREANN_FEED_URL")
if alert_weathergov_self_id:
logging.info("Getting weather alert from Weather.gov API")
alert_provider = weathergovalerts.WeatherGovAlerts(location_lat, location_long, alert_weathergov_self_id)
alert_message = alert_provider.get_alert()
elif alert_metoffice_feed_url:
logging.info("Getting weather alert from Met Office RSS Feed")
alert_provider = metofficerssfeed.MetOfficeRssFeed(alert_metoffice_feed_url)
alert_message = alert_provider.get_alert()
elif alert_meteireann_feed_url:
logging.info("Getting weather alert from Met Eireann")
alert_provider = meteireannalertprovider.MetEireannAlertProvider(alert_meteireann_feed_url)
alert_message = alert_provider.get_alert()
logging.info("alert - {}".format(alert_message))
return alert_message
def main():
template_name = os.getenv("SCREEN_LAYOUT", "1")
location_lat = os.getenv("WEATHER_LATITUDE", "51.5077")
location_long = os.getenv("WEATHER_LONGITUDE", "-0.1277")
weather_format = os.getenv("WEATHER_FORMAT", "CELSIUS")
if (weather_format == "CELSIUS"):
units = "metric"
degrees = "°C"
else:
units = "imperial"
degrees = "°F"
weather = get_weather(location_lat, location_long, units)
if not weather:
logging.error("Unable to fetch weather payload. SVG will not be updated.")
return
weather_desc = format_weather_description(weather["description"])
alert_message = get_alert_message(location_lat, location_long)
alert_message = format_alert_description(alert_message)
time_now = get_formatted_time(datetime.datetime.now())
time_now_font_size = "100px"
if len(time_now) > 6:
time_now_font_size = str(100 - (len(time_now)-5) * 5) + "px"
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_FONT_SIZE': time_now_font_size,
'TIME_NOW': time_now,
'HOUR_NOW': datetime.datetime.now().strftime("%-I %p"),
'DAY_ONE': datetime.datetime.now().strftime("%b %-d, %Y"),
'DAY_NAME': datetime.datetime.now().strftime("%A"),
'ALERT_MESSAGE_VISIBILITY': "visible" if alert_message else "hidden",
'ALERT_MESSAGE': alert_message
}
logging.info(output_dict)
logging.info("Updating SVG")
template_svg_filename = f'screen-template.{template_name}.svg'
output_svg_filename = 'screen-output-weather.svg'
update_svg(template_svg_filename, output_svg_filename, output_dict)
if __name__ == "__main__":
main()