Created
May 9, 2016 02:50
-
-
Save matmoody/b17f8ec3b9da985fc1c45434fb368733 to your computer and use it in GitHub Desktop.
Request and store the "Maximum Temperature" for 5 cities over 30 day period.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import sqlite3 as lite | |
import datetime | |
api_key = 'API_KEY' | |
url = 'https://api.forecast.io/forecast/' + api_key | |
cities = { "Wamego": '39.201941,-96.304998', | |
"Grinnell": '39.127230,-100.629031', | |
"Eldorado": '37.817240,-96.862252', | |
"Salina": '38.840280,-97.611424', | |
"Denver": '39.739236,-104.990251' | |
} | |
end_date = datetime.datetime.now() | |
con = lite.connect('weather.db') | |
cur = con.cursor() | |
cities.keys() | |
with con: | |
cur.execute('CREATE TABLE daily_temp ( day_of_reading INT, Wamego REAL, Grinnell REAL, Eldorado REAL, Salina REAL, Denver REAL);') | |
query_date = end_date - datetime.timedelta(days=30) | |
with con: | |
while query_date < end_date: | |
cur.execute("INSERT INTO daily_temp(day_of_reading) VALUES (?)", (int(query_date.strftime('%s')),)) | |
query_date += datetime.timedelta(days=1) | |
for k,v in cities.iteritems(): | |
query_date = end_date - datetime.timedelta(days=30) | |
while query_date < end_date: | |
r = requests.get(url + '/' + v + ',' + query_date.strftime('%Y-%m-%dT12:00:00')) | |
with con: | |
# insert temperatureMax into db | |
cur.execute('UPDATE daily_temp SET ' + k + ' = ' + str(r.json()['daily']['data'][0]['temperatureMax']) + ' WHERE day_of_reading = ' + query_date.strftime('%s')) | |
query_date += datetime.timedelta(days=1) | |
con.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment