Skip to content

Instantly share code, notes, and snippets.

@m-bo-one
Last active April 5, 2016 23:12
Show Gist options
  • Save m-bo-one/dbb1eb36b4425b94a7c426c138a68cb3 to your computer and use it in GitHub Desktop.
Save m-bo-one/dbb1eb36b4425b94a7c426c138a68cb3 to your computer and use it in GitHub Desktop.
import urllib
import json
import datetime
site_url = 'http://api.openweathermap.org'
path = '/data/2.5/forecast/daily'
APPID = '87d97746bd021478a0222490997c87a5'
def get_full_api_url(url, path, data):
url_values = urllib.urlencode(data)
return url + path + '?' + url_values
def summarise_forecast(city, days=14, units='metric'):
temp_max_list = temp_min_list = []
forecasts = {}
data = {'q': city, 'APPID': APPID, 'cnt': days, 'units': units}
full_url = get_full_api_url(site_url, path, data)
response = urllib.urlopen(full_url).read()
resp_dict = json.loads(response)
for day_list in resp_dict['list']:
current_day = datetime.datetime.fromtimestamp(
day_list['dt']).strftime('%Y-%m-%d')
temp_max_list.append(day_list['temp']['max'])
temp_min_list.append(day_list['temp']['min'])
for weather in day_list['weather']:
w_key = weather['main']
if w_key not in forecasts:
forecasts[w_key] = []
forecasts[w_key].append(current_day)
return {
"city": city,
"max": max(temp_max_list),
"min": min(temp_min_list),
"forecasts": forecasts
}
if __name__ == '__main__':
print summarise_forecast('Kiev')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment