forked from shahabkamali/forecastGTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_data.py
38 lines (27 loc) · 990 Bytes
/
fetch_data.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
import requests
def fetch_data(url):
try:
response = requests.get(url)
ret = response.json()
response.raise_for_status()
return ret
except (ValueError, requests.exceptions.RequestException) as e:
print({'error': e, 'url': url})
def fetch_forecast(city_id='2643743'):
url = "http://api.openweathermap.org/data/2.5/forecast?id=%s&APPID=34892dee8e50ad1f088d6d9a6271c58b" % city_id
ret = fetch_data(url)
if 'cod' in ret and ret['cod'] == '200':
return ret['list']
return None
def get_24h_forecast_temps(city_id='2643743'):
forecasts = fetch_forecast(city_id)
if forecasts is None:
return None
# api gives us every 3 hour until 5 days
forecasts = forecasts[:9]
ret = {}
for forecast in forecasts:
# convert to Celsius
forecast['main']['cel_temp'] = round((float(forecast['main']['temp']) - 273.15), 2)
ret[forecast['dt']] = forecast
return ret