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.")


Check: lat/long = 46.229058/6.077243, timezone = Europe/Zurich
The current wind speed is 4.09
It feels exactly as warm as it is.
43.0% of the moon is visible today.
The difference between maximal and minimal temperatures is 12.91 °F 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)


Friday, June 17, 2016: warm, 56.71 °F
Saturday, June 18, 2016: warm, 63.96 °F
Sunday, June 19, 2016: warm, 70.36 °F
Monday, June 20, 2016: warm, 77.11 °F
Tuesday, June 21, 2016: hot, 83.45 °F
Wednesday, June 22, 2016: hot, 84.94 °F
Thursday, June 23, 2016: hot, 80.34 °F
Friday, June 24, 2016: warm, 71.97 °F

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


  File "<ipython-input-13-4e7a4eace6b0>", line 16
    timeString = time.strftime('%B %d, %l %p:', time.localtime(item['time']))
             ^
IndentationError: expected an indented block

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


On Christmas Day, 1980, the temperature in Central park was 28.48°F.
On Christmas Day, 1990, the temperature in Central park was 30.16°F.
On Christmas Day, 2000, the temperature in Central park was 27.03°F.

In [ ]: