Created
April 23, 2019 18:55
-
-
Save graham-thomson/7c4e9937ed1223a89f93d6ff982cf844 to your computer and use it in GitHub Desktop.
This file contains 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 datetime as dt | |
import urllib.request | |
import json | |
import pandas as pd | |
def boston_hourly_weather(start_time, end_time): | |
lat, long = 42.3603, -71.0583 | |
key = '' | |
temp_data = [] | |
while start_time <= end_time: | |
start_time_str = int(start_time.timestamp()) | |
resp = json.loads(urllib.request.urlopen( | |
"https://api.darksky.net/forecast/{key}/{lat},{long},{time}".format( | |
key=key, lat=lat, long=long, time=start_time_str | |
) | |
).read()) | |
temp_data += [(dt.datetime.fromtimestamp(h["time"]), h["temperature"]) | |
for h in resp.get("hourly").get("data")] | |
start_time = start_time + dt.timedelta(days=1) | |
return temp_data | |
boston_weather = pd.DataFrame(boston_hourly_weather( | |
dt.datetime(2019, 4, 14, 0, 0), | |
dt.datetime(2019, 4, 22, 0, 0) | |
), columns=["timestamp", "temp"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment