-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherbit.py
executable file
·82 lines (59 loc) · 1.89 KB
/
weatherbit.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
#!/usr/bin/env python3
# weatherbit.py
# read forecast from weatherbit.io
import json
import requests
debug=False
def get_weatherbit_forecast(sysapi, latitude, longitude, days, app_log=None):
if sysapi is None:
return
payload = {'key': sysapi,
'units': 'M',
'lang': 'en',
'lat': str(latitude),
'lon': str(longitude),
'days': str(days)
}
if debug:
if app_log is not None:
app_log.info(payload)
url = 'https://api.weatherbit.io/v2.0/forecast/daily'
try:
response = requests.get(url, params=payload)
except requests.exceptions.RequestException as e:
if debug:
if app_log is not None:
app_log.error(e)
if response.status_code == 200:
str_response = response.content.decode("utf-8")
if debug:
if app_log is not None:
app_log.info(str_response)
app_log.info(response.url)
return json.loads(str_response)
def get_weatherbit_current(sysapi, latitude, longitude, app_log=None):
if sysapi is None:
return
payload = {'key': sysapi,
'units': 'M',
'lang': 'en',
'lat': str(latitude),
'lon': str(longitude)
}
if debug:
if app_log is not None:
app_log.info(payload)
url = 'https://api.weatherbit.io/v2.0/current'
try:
response = requests.get(url, params=payload)
except requests.exceptions.RequestException as e:
if debug:
if app_log is not None:
app_log.error(e)
if response.status_code == 200:
str_response = response.content.decode("utf-8")
if debug:
if app_log is not None:
app_log.info(str_response)
app_log.info(response.url)
return json.loads(str_response)