-
Notifications
You must be signed in to change notification settings - Fork 0
/
openweathermap.py
167 lines (154 loc) · 5.25 KB
/
openweathermap.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
import os
import requests
import logging
from weatherprovider import WeatherProvider
class OpenWeatherMap(WeatherProvider):
def __init__(self):
logging.info("Loading OpenWeatherMap provider")
self.api_key = os.environ["OPEN_WEATHER_MAP_API_KEY"]
self.zip_code = os.environ["WEATHER_ZIP_CODE"]
self.endpoint = "http://api.openweathermap.org/data/2.5/"
def _get(self):
params = (
('zip', f'{self.zip_code},us'),
('appid', self.api_key),
('units', 'imperial'),
)
response = requests.get(f'{self.endpoint}weather', params = params)
return response.json()
# Get the current weather conditions at given zip code
def get_weather(self):
d = self._get()
w = {}
w['zip_code'] = self.zip_code
w['temp'] = d['main']['temp']
w['pressure'] = d['main']['pressure']
w['pressure_unit'] = 'mb'
w['humidity'] = d['main']['humidity']
if 'rain' in d:
w['rain'] = {
'rate': d['rain']['1h']
}
w['wind'] = {
'speed': d['wind']['speed'],
'degree': d['wind']['deg'],
'direction': self.cardinal_direction(d['wind']['deg'])
}
# Unique keys to Open Weather Map
w['description'] = d['weather'][0]['description']
w['category'] = d['weather'][0]['main']
w['city'] = d['name']
w['coord'] = d['coord']
w['sunrise'] = d['sys']['sunrise']
w['sunset'] = d['sys']['sunrise']
w['timezone'] = d['timezone']
return w
# TODO: added stub to document. Replace current 2 calls with one call to get current and forecast data
def get_weather_one_call(self):
print('https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude={part}&appid={API key}')
# Get the 5 day forecast from openweathermap.org
def get_forecast(self, days):
cnt = days * 8 # API returns 8 data points for each day (3 hour intervals)
params = (
('zip', f'{self.zip_code},us'),
('appid', self.api_key),
('cnt', cnt),
('units', 'imperial'),
)
response = requests.get(f'{self.endpoint}forecast', params = params)
return response.json()
# Get the air pollution data for the lat/lon
def get_air_pollution(self, coords):
lat, lon = coords
params = (
('lat', lat),
('lon', lon),
('appid', self.api_key),
)
response = requests.get(f'{self.endpoint}air_pollution', params = params)
return response.json()
def sample_data_weather(self):
w = {
'coord': {
'lon': -122.1121,
'lat': 48.1829
},
'weather': [{
'id': 800,
'main': 'Clear',
'description': 'clear sky',
'icon': '01d'
}],
'base': 'stations',
'main': {
'temp': 40.68,
'feels_like': 35.33,
'temp_min': 39,
'temp_max': 42.01,
'pressure': 1021,
'humidity': 87
},
'visibility': 10000,
'wind': {
'speed': 4.61,
'deg': 80},
'clouds': {
'all': 1
},
'dt': 1612379637,
'sys': {
'type': 1,
'id': 3363,
'country': 'US',
'sunrise': 1612366406,
'sunset': 1612401070
},
'timezone': -28800,
'id': 0,
'name': 'Arlington',
'cod': 200
}
return w
def sample_data_forecast(self):
w = {
'city': {
'coord': {'lat': 48.1829, 'lon': -122.1121},
'country': 'US',
'id': 0,
'name': 'Arlington',
'population': 0,
'sunrise': 1612366406,
'sunset': 1612401070,
'timezone': -28800
},
'cnt': 8,
'cod': '200',
'list': [{
'clouds': {'all': 1},
'dt': 1612386000,
'dt_txt': '2021-02-03 21:00:00',
'main': {
'feels_like': 39.34,
'grnd_level': 1010,
'humidity': 70,
'pressure': 1021,
'sea_level': 1021,
'temp': 44.02,
'temp_kf': -0.23,
'temp_max': 44.44,
'temp_min': 44.02
},
'pop': 0,
'sys': {'pod': 'd'},
'visibility': 10000,
'weather': [{
'description': 'clear sky',
'icon': '01d',
'id': 800,
'main': 'Clear'
}],
'wind': {'deg': 275, 'speed': 2.75}
}],
'message': 0
}
return w