from urllib.request import urlopen
import json

YourKey = ''


def weatherForecast(YourKey):
    try:
        weatherUrl = urlopen('http://api.wunderground.com/api/' + YourKey + '/geolookup/forecast/q/NL/Twenthe%20Air%20Base.json')
        json_string = weatherUrl.read()
        forecast_json = json.loads(json_string.decode('utf-8'))

#       with open('forecast.txt', 'w') as jsonfile:
#           json.dump(forecast_json, jsonfile)
#       jsonfile.close()

        temp_high0 = forecast_json['forecast']['simpleforecast']['forecastday'][0]['high']['celsius']
        temp_low0  = forecast_json['forecast']['simpleforecast']['forecastday'][0]['low']['celsius']
        temp_high1 = forecast_json['forecast']['simpleforecast']['forecastday'][1]['high']['celsius']
        temp_low1  = forecast_json['forecast']['simpleforecast']['forecastday'][1]['low']['celsius']
        weatherUrl.close

    except Exception as diag:
        temp_high0 = 40
        temp_low0 = -20
        temp_high1 = 40
        temp_low1 = -20

    return min(temp_low0, temp_low1), max(temp_high0, temp_high1)

def weatherConditions(YourKey):
    try:
        weatherUrl = urlopen('http://api.wunderground.com/api/' + YourKey + '/geolookup/conditions/q/NL/Twenthe%20Air%20Base.json')
        json_string = weatherUrl.read()
        conditions_json = json.loads(json_string.decode('utf-8'))

#       with open('conditions.txt', 'w') as jsonfile:
#           json.dump(conditions_json, jsonfile)
#       jsonfile.close()

        temp_now = conditions_json['current_observation']['temp_c']
        wind_dir = conditions_json['current_observation']['wind_dir']
        wind_kph = conditions_json['current_observation']['wind_kph']
        real_rain = conditions_json['current_observation']['precip_1hr_metric'] # actual rain

        weatherUrl.close

    except Exception as diag:
        temp_now = 0
        wind_dir = N
        wind_kph = 0
        real_rain = 0

    return temp_now, wind_kph, wind_dir, real_rain


def weatherHourly(YourKey):
    try:
        weatherUrl = urlopen('http://api.wunderground.com/api/' + YourKey + '/geolookup/hourly/q/NL/Twenthe%20Air%20Base.json')
        json_string = weatherUrl.read()
        hourly_json = json.loads(json_string.decode('utf-8'))

#       with open('hourly.txt', 'w') as jsonfile:
#           json.dump(hourly_json, jsonfile)
#       jsonfile.close()

        hour_rain = hourly_json['hourly_forecast'][0]['qpf']['metric'] # Quantitative Precipitation Forecasts
        hour_snow = hourly_json['hourly_forecast'][0]['snow']['metric']

        weatherUrl.close()

    except Exception as diag:
        hour_rain = 0
        hour_snow = 0

    return hour_rain, hour_snow

temp_low,temp_high = weatherForecast(YourKey)
print (temp_low)
print (temp_high)

temp_now, wind_dir, wind_kph, real_rain = weatherConditions(YourKey);
print (temp_now)
print (wind_dir)
print (wind_kph)
print (real_rain)

hour_rain, hour_snow = weatherHourly(YourKey)
print (hour_rain)
print (hour_snow)
