In [7]:
import requests
key = 'b2e7648e58673111d1f5e5fa41e8fbaf'
base = 'https://api.forecast.io/forecast/' + key + "/"
Graded = 6/7
In [8]:
# 1) Make a request from the Forecast.io API for where you were born (or lived, or want to visit!).
# https://developer.forecast.io/docs/v2
url = base + '46.229058,6.077243'
response = requests.get(url)
data1 = response.json()
print("Check: lat/long =", str(data1['latitude']) + "/" + str(data1['longitude']) + ',', "timezone =", data1['timezone'])
# 2) What's the current wind speed? How much warmer does it feel than it actually is?
print("The current wind speed is", data1['currently']['windSpeed'])
deltaWarm = data1['currently']['temperature'] - data1['currently']['apparentTemperature']
if deltaWarm > 0:
print("It feels", deltaWarm, "°F warmer than it is.")
elif deltaWarm == 0:
print("It feels exactly as warm as it is.")
else:
print("It feels", -deltaWarm, "°F colder than it is.")
# 3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
todayforecast = data1['daily']['data'][0]
print(str(todayforecast['moonPhase']*100) +"%", "of the moon is visible today.")
# 4) What's the difference between the high and low temperatures for today?
print("The difference between maximal and minimal temperatures is", "%.2f °F" % (todayforecast['temperatureMax'] - todayforecast['temperatureMin']), "today.")
In [9]:
# 5) Loop through the daily forecast, printing out the next week's worth of predictions.
# I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
import time
forecast = data1['daily']['data']
for item in forecast:
temperatureString = ''
if item['temperatureMax'] > 80:
temperatureString = 'hot'
elif item['temperatureMax'] > 50:
temperatureString = 'warm'
else:
temperatureString = 'cold'
temperatureString += ", " + str(item['temperatureMax']) + ' °F'
print(time.strftime('%A, %B %d, %Y:', time.localtime(item['time'])), temperatureString)
In [13]:
# 6) What's the weather looking like for the rest of today in Miami, Florida?
# I'd like to know the temperature for every hour
# if it's going to have cloud cover of more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
#TA-Stephan: Code doesn't run
url_miami = base + '25.761283,-80.190227'
response_miami = requests.get(url_miami)
data6 = response_miami.json()
miamitoday = data6['hourly']['data']
todayNb = time.strftime('%d', time.localtime(miamitoday[0]['time']))
for item in miamitoday:
if item['cloudCover'] > 0.5:
cloudy = 'and cloudy'
timeString = time.strftime('%B %d, %l %p:', time.localtime(item['time']))
print(timeString, "%.2f" % (item['temperature']), "°F", cloudy)
if int(time.strftime('%H', time.localtime(item['time']))) == 0: # we stop at midnight
break
In [11]:
# 7) What was the temperature in Central Park on Christmas Day, 1980? How about 1990? 2000?
import datetime, time
def get_CentralPark_temperature (year):
time = datetime.datetime(year,12,25,0,0).timestamp()
url_park = base + '40.782881,-73.965269,' + str(int(time))
response_park = requests.get(url_park)
data7 = response_park.json()
theString = "On Christmas Day, " + str(year) + ", the temperature in Central park was " + str(data7['currently']['temperature']) + "°F."
print(theString)
return
get_CentralPark_temperature(1980)
get_CentralPark_temperature(1990)
get_CentralPark_temperature(2000)
# 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 [ ]: