Skip to content

Instantly share code, notes, and snippets.

@benwrk
Created November 27, 2016 12:49
Show Gist options
  • Save benwrk/d83e6aac7a60478026e724ba760f1a62 to your computer and use it in GitHub Desktop.
Save benwrk/d83e6aac7a60478026e724ba760f1a62 to your computer and use it in GitHub Desktop.
import urllib
import urllib2
import json
API_ENDPOINT = 'http://api.openweathermap.org/data/2.5/forecast/daily'
API_KEY = 'PUT API KEY HERE'
DAYS = 13
THRESHOLD = 70
def is_dangerous(province, date=0):
if date >= 7:
raise ValueError('The parameter [date] should less than 7')
params = {
'q': province,
'mode': 'json',
'cnt': DAYS,
'units': 'metric',
'APPID': API_KEY
}
url = API_ENDPOINT + '?' + urllib.urlencode(params)
res_json = json.load(urllib2.urlopen(url))
forecast = res_json['list'][date:date + 7]
millimeter_sum = 0
for day in forecast:
millimeter_sum = millimeter_sum + float(day.get('rain', '0'))
print millimeter_sum
return millimeter_sum >= THRESHOLD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment