from urllib.request import urlopen
import json

f = urlopen('http://api.wunderground.com/api/YourKey/geolookup/forecast/q/NL/Twenthe%20Air%20Base.json')
json_string = f.read()
forecast_json = json.loads(json_string.decode('utf-8'))

temp_high0 = forecast_json['forecast']['simpleforecast']['forecastday'][0]['high']['celsius']
temp_low0  = forecast_json['forecast']['simpleforecast']['forecastday'][0]['low']['celsius']
print ('Temperature today between {0} and {1}'.format(temp_low0, temp_high0))

temp_high1 = forecast_json['forecast']['simpleforecast']['forecastday'][1]['high']['celsius']
temp_low1  = forecast_json['forecast']['simpleforecast']['forecastday'][1]['low']['celsius']
print ('Temperature tomorrow between {0} and {1}'.format(temp_low1, temp_high1))

f.close

f = urlopen('http://api.wunderground.com/api/YourKey/geolookup/conditions/q/NL/Twenthe%20Air%20Base.json')
json_string = f.read()
conditions_json = json.loads(json_string.decode('utf-8'))

temp_now = conditions_json['current_observation']['temp_c']
print ('Current temperature is {0}'.format(temp_now))

wind_dir = conditions_json['current_observation']['wind_dir']
wind_kph = conditions_json['current_observation']['wind_kph']
print ('Wind speed is {0} kph from the {1}'.format(wind_kph, wind_dir))

# actual rain
real_rain = conditions_json['current_observation']['precip_1hr_metric']
print ('Rain past hour {0}'.format(real_rain))

f.close

f = urlopen('http://api.wunderground.com/api/YourKey/geolookup/hourly/q/NL/Twenthe%20Air%20Base.json')
json_string = f.read()
hourly_json = json.loads(json_string.decode('utf-8'))

# Quantitative Precipitation Forecasts
hour_rain = hourly_json['hourly_forecast'][0]['qpf']['metric']
print ('Expected rain next hour {0}'.format(hour_rain))

hour_snow = hourly_json['hourly_forecast'][0]['snow']['metric']
print ('Expected snow next hour {0}'.format(hour_snow))

f.close()
