Skip to content

Instantly share code, notes, and snippets.

@graham-thomson
Created April 23, 2019 18:55
Show Gist options
  • Save graham-thomson/7c4e9937ed1223a89f93d6ff982cf844 to your computer and use it in GitHub Desktop.
Save graham-thomson/7c4e9937ed1223a89f93d6ff982cf844 to your computer and use it in GitHub Desktop.
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