-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
51 lines (42 loc) · 1.43 KB
/
api.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
import requests
class Weather:
def __init__(self, endpoint, key, lat, lon):
self.endpoint = endpoint
self.parameters = {
'lat': lat,
'lon': lon,
'appid': key,
'exclude': 'current,minutely,alerts',
'units': 'metric',
}
def getResponse(self):
res = requests.get(url=self.endpoint, params=self.parameters)
res.raise_for_status()
weather_data = res.json()
hours = weather_data["hourly"][:12]
daily = weather_data["daily"][:3]
# list of dict where every element is an hour
hour_list = [{'dt': item["dt"],
'code': item["weather"][0]["id"],
'icon': item["weather"][0]["icon"]} for item in hours]
# list of dict where every element is a day
daily_list = [{
'dt': item["dt"],
'min': item["temp"]["min"],
'max': item["temp"]["max"],
'code': item["weather"][0]["id"],
'icon': item["weather"][0]["icon"]
} for item in daily]
result = {
'today': {
'morning': hour_list[0],
'afternoon': hour_list[6],
'evening': hour_list[-1]
},
'next':{
'one': daily_list[0],
'two': daily_list[1],
'three': daily_list[2]
}
}
return result