Tip: Once you've imported the JSON into a variable, check the timezone's name to make sure it seems like it got the right part of the world! Tip 2: How is north vs. south and east vs. west latitude/longitude represented? Is it the normal North/South/East/West?
In [11]:
import requests
# api request for bethesda, maryland
response = requests.get('https://api.forecast.io/forecast/a197f06e1906b1a937ad31d4378b8939/38.9847, -77.0947')
data = response.json()
current = data['currently']
current
Out[11]:
In [14]:
diff = current['apparentTemperature'] - current['temperature']
print('The current windspeed is', current['windSpeed'],"miles per hour.")
print('It feels', diff, 'degrees warmer than it actually is.')
In [42]:
daily = data['daily']['data']
today = daily[0]
today_moonphase = today['moonPhase']
if today_moonphase < .25:
moonphase = 'new moon'
elif today_moonphase < 0.5:
moonphase = 'first quarter moon'
elif today_moonphase < 0.75:
moonphase = 'full moon'
else:
moonphase = 'last quarter moon'
print('Reported moon phase from Dark Sky forecast is', today_moonphase)
print('So, currently, the', moonphase, 'is visible.')
In [43]:
highlow_tempdiff = today['temperatureMax'] - today['temperatureMin']
print('The difference between the high and low temperatures for today is', highlow_tempdiff, 'degrees.')
In [62]:
for item in daily:
daily_hightemp = item['temperatureMax']
if daily_hightemp < 70:
tempdescription = 'COLD'
elif daily_hightemp < 85:
tempdescription = 'WARM'
else:
tempdescription = 'HOT'
print('The high temp is', daily_hightemp, 'so it is', tempdescription)
In [74]:
response = requests.get('https://api.forecast.io/forecast/a197f06e1906b1a937ad31d4378b8939/25.7617,-80.1918')
data = response.json()
hourly = data['hourly']['data']
for item in hourly:
if item['cloudCover'] < .5:
print('It is', item['temperature'], 'degrees and cloudy')
else:
print('It is', item['temperature'], 'degrees')
Tip: You'll need to use UNIX time, which is the number of seconds since January 1, 1970. Google can help you convert a normal date! Tip: You'll want to use Forecast.io's "time machine" API at https://developer.forecast.io/docs/v2
In [121]:
response1980 = requests.get('https://api.forecast.io/forecast/a197f06e1906b1a937ad31d4378b8939/40.7829,-73.9654,346550400')
data1980 = response1980.json()
christmasday1980 = data1980['daily']['data'][0]
print('It was', christmasday1980['temperatureMax'], 'degrees on Christmas Day, 1980.')
response1990 = requests.get('https://api.forecast.io/forecast/a197f06e1906b1a937ad31d4378b8939/40.7829,-73.9654,662083200')
data1990 = response1990.json()
christmasday1990 = data1990['daily']['data'][0]
print('It was', christmasday1990['temperatureMax'], 'degrees on Christmas Day, 1990.')
response2000 = requests.get('https://api.forecast.io/forecast/a197f06e1906b1a937ad31d4378b8939/40.7829,-73.9654,976838400')
data2000 = response2000.json()
christmasday2000 = data2000['daily']['data'][0]
print('It was', christmasday2000['temperatureMax'], 'degrees on Christmas Day, 2000.')