-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathALTweather.py
More file actions
424 lines (339 loc) · 12 KB
/
ALTweather.py
File metadata and controls
424 lines (339 loc) · 12 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from m5stack import rtc
import ALTutils as uti
from ALTconnection import *
""" TIME """
_NULL_TIME = '00:00'
_NULL_DATE_TIME = '01 Jan 00:00'
def _get_month(month):
"""
Returns the appropriate 3 letters of a given number of a month.
Parameters
----------
month : int
A number between 1-12.
Returns
-------
: str
3 letters describing a month if 'month' is between 1-12 and None otherwise.
"""
dictionary = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct',
11: 'Nov', 12: 'Dec'}
return dictionary.get(month, None)
# Fetches the exact time from host: time.google.com
def fetch_time(timezone=3, host='time.google.com'):
"""
Fetches the time from a given host.
Parameters
----------
timezone : int
Needed timezone.
host : str
The host ntp server used.
Returns
-------
: str
The string of the current time.
The format of the output is : XX:XX.
Where X is a digit.
"""
try:
check_connection()
except Exception as e:
return _NULL_TIME
rtc.settime('ntp', host=host, tzone=timezone)
time_info = rtc.datetime()
return uti.add_left_zero(str(time_info[4])) + str(":") + uti.add_left_zero(str(time_info[5]))
def fetch_date_time(timezone=3, host='time.google.com'):
"""
Fetches the time and date from a given host.
Parameters
----------
timezone : int
Needed timezone.
host : str
The host ntp server used.
Returns
-------
: str
The string of the current time and date.
The format of the output is : XX LLL XX:XX.
Where X is a digit and LLL are the three letters that define a month.
"""
try:
check_connection()
except Exception as e:
return _NULL_DATE_TIME
rtc.settime('ntp', host=host, tzone=timezone)
time_info = rtc.datetime()
res = uti.add_left_zero(str(time_info[2])) + str(' ') + _get_month(time_info[1]) + str(' ') + uti.add_left_zero(
str(time_info[4])) + str(":") + uti.add_left_zero(str(time_info[5]))
return res
import urequests
import ujson
def C_to_F(degree):
"""
Converts temperature degrees in Celsius to Fahrenheit
Parameters
----------
degree : int
The degree in Celsius.
Returns
-------
: float
The degree in Fahrenheit.
"""
return float(degree) * 1.8 + 35
def F_to_C(degree):
"""
Converts temperature degrees in Fahrenheit to Celsius
Parameters
----------
degree : int
The degree in Fahrenheit.
Returns
-------
: float
The degree in Celsius.
"""
return (float(degree) - 35) / 1.8
def hPa_to_kPa(degree):
"""
Converts pressure degrees in hPa to kPa
Parameters
----------
degree : int
The degree in hPa.
Returns
-------
: float
The degree in kPa.
"""
return float(degree / 10)
def Pa_to_hPa(degree):
"""
Converts pressure degrees in hPa to kPa
Parameters
----------
degree : int
The degree in Pa.
Returns
-------
: float
The degree in hPa.
"""
return float(degree / 100)
""" WEATHER FROM API """
# Fetches weather from: https://openweathermap.org
def _get_curr_ip():
"""
Returns
-------
ip : str
The current device's IP address.
Raises
-------
Exception in case of connection failure to the site: https://api.ipify.org
"""
try:
ip = urequests.get('https://api.ipify.org').content.decode('utf8')
except Exception as e:
raise Exception(__name__ + ': ' + str(e))
return ip
def _get_lat_long_from_curr_ip():
"""
Returns
-------
latlong : str list
A list that contains the current longtitude and latitude of the device,
according to the device's IP address.
The format: ['latitude', 'longtitude']
Raises:
-------
Exception in case of connection failure to the site: https://ipapi.co
"""
ip = _get_curr_ip()
try:
latlong = urequests.get('https://ipapi.co/{}/latlong/'.format(ip)).text.split(',')
except Exception as e:
raise Exception(__name__ + ': ' + str(e))
return latlong
def _get_city_from_curr_ip():
"""
Returns
-------
city : str
The city according to the device's IP address.
Raises
-------
Exception in case of connection failure to the site: https://ipapi.co
"""
ip = _get_curr_ip()
try:
city = urequests.get('https://ipapi.co/{}/city/'.format(ip)).text
except Exception as e:
raise Exception(__name__ + ': ' + str(e))
return city
def _parse_units(units):
"""
In the site we fetch the weather from, the dictionary is:
'standard' = 'Kelvin'
'imperial' = 'Fahrenheit'
'metric' = 'Celsius'
Parameters
-------
units
units are either {K, C, F}
Returns
-------
: str
the appropriate units string to be used in the site
"""
dictionary = {'K': 'standard', 'C': 'metric', 'F': 'imperial'}
return dictionary.get(units, None)
def _get_weather_json_from_api(apikey, units_string):
"""
Gets all weather information from API in JSON format.
Parameters
-------
apikey
The API key which the user gets after creating an account in https://openweathermap.org.
units_string
The temperature units. Could be either K,C,F
Returns
-------
json data : JSON
All the needed weather in JSON format.
Raises
-------
Exception in case of failure in retrieving JSON data.
"""
check_connection()
latlong = _get_lat_long_from_curr_ip()
try:
req = urequests.get(
'https://api.openweathermap.org/data/2.5/forecast?lat={}&lon={}&appid={}&units={}&cnt=1'.format(latlong[0],
latlong[1],
apikey,
units_string)).text
except Exception as e:
raise Exception(__name__ + ": Error connecting to https://api.openweathermap.org")
json_data = ujson.loads((req))
# If error code is 200, it means that the request succeeded
if json_data['cod'] == '200' and json_data['message'] == 0:
return json_data
else:
raise Exception(__name__ + ': Error code ' + str(json_data['cod']) + ': ' + str(json_data['message']))
def _fetch_icon_url_from_id(icon_id):
"""
Parameters
-------
icon_id : int
The ID of the weather icon.
Returns
-------
A full url of a weather icon.
The format: https://openweathermap.org/img/wn/{id}@2x.png
"""
return 'https://openweathermap.org/img/wn/{}@2x.png'.format(icon_id)
def fetch_local_weather_from_api(apikey, units):
"""
Parameters
-------
apikey
The API key which the user gets after creating an account in https://openweathermap.org.
units_string
The temperature units. Could be either K,C,F
Returns
-------
A dictionary that contains:
[city, date, pressure (hPa units), temperature, humidity (%), wind speed (if imperial: miles/hour else meter/second), discription, icon-id]
With valid values, and in case of failure in retrieving the information, a dictionary with 'N/A' values will be returned.
"""
units_string = _parse_units(units)
if units_string is None:
raise Exception(__name__ + ': Units must either be : K, F, C')
try:
json_data = _get_weather_json_from_api(apikey, units_string)
except Exception as e:
return {'city': 'N/A', 'date': 'N/A', 'pressure': '0000', 'temperature': '00', 'humidity': '00', 'wind': '00',
'description': 'N/A', 'icon-url': 'Assets/Icons/error.png', 'error-msg': str(e)}
return {'city': json_data["city"]["name"], 'date': str(json_data["list"][0]["dt_txt"]),
'pressure': str(json_data["list"][0]["main"]["pressure"]),
'temperature': str(round(json_data["list"][0]["main"]["temp"])),
'humidity': str(json_data["list"][0]["main"]["humidity"]),
'wind': str(json_data["list"][0]["wind"]["speed"]),
'description': uti.capitalize_first_letter(str(json_data["list"][0]["weather"][0]["description"])),
'icon-url': _fetch_icon_url_from_id(json_data["list"][0]["weather"][0]["icon"]), 'error-msg': ''}
""" WEATHER FROM wttr.in """
# Fetches the weather from : https://wttr.in/{city}
def _get_weather_json_from_web(city):
"""
Gets all weather information from API in JSON format.
Parameters
-------
city
The city of which we want to get the weather information.
Returns
-------
json data : JSON
All the needed weather of the city in JSON format.
Raises
-------
Exception in case of failure in retrieving JSON data.
"""
# Spaces are replaced by '+' to make the url legal according to the websites' rules.
city_m = city.replace(' ', '+')
try:
req_text = urequests.get("https://wttr.in/{}?format=j2".format(city_m)).text
json_data = ujson.loads((req_text))
except Exception as e:
raise Exception(__name__ + ": https://wttr.in is currently down, can't fetch weather data.")
return json_data
def _get_temp_id(units):
"""
In the wesite we fetch the weather from, the dictionary is:
'temp_C' = 'Celsius'
'temp_F' = 'Fahrenheit'
Parameters
-------
units
units are either {C, F}
Returns
-------
: str
the appropriate units string to be used in the website
"""
dictionary = {'C': 'temp_C', 'F': 'temp_F'}
return dictionary.get(units, None)
def fetch_local_weather_from_web(units):
"""
Parameters
-------
units
The temperature units. Could be either C,F.
Returns
-------
A dictionary that contains:
[city, date, pressure (hPa units), temperature, humidity (%), wind speed (meter/second), description, icon-id, uv-index]
With valid values, and in case of failure in retrieving the information, a dictionary with 'N/A' values will be returned.
Assumes:
-------
/flash/Icons/no_weather.png exists
"""
temp_id = _get_temp_id(units)
if temp_id is None:
raise Exception(__name__ + ': Units must either be : F, C')
try:
check_connection()
city = _get_city_from_curr_ip()
json_data = _get_weather_json_from_web(city)
except Exception as e:
return {'city': 'N/A', 'date': 'N/A', 'pressure': '0000', 'temperature': '00', 'humidity': '00', 'wind': '00',
'description': 'N/A', 'icon-url': 'Assets/Icons/error.png', 'uv-index': '0', 'error-msg': str(e)}
weather = json_data['weather'][0]
json_data = json_data['current_condition'][0]
return {'city': city, 'date': json_data["localObsDateTime"], 'pressure': str(json_data["pressure"]),
'temperature': str(json_data[temp_id]), 'humidity': str(json_data["humidity"]),
'wind': str(round(float(json_data["windspeedKmph"]) * 0.277778, 2)),
'description': uti.capitalize_first_letter(str(json_data["weatherDesc"][0]["value"])),
'icon-url': '/flash/Assets/Icons/globe.png', 'uv-index': str(weather["uvIndex"]), 'error-msg': ''}