Forecasts_API_Homework


In [1]:
import requests
import datetime
import time

#human readable time
localtime = time.asctime(time.localtime(time.time()))
#UNIX-Time
current_UNIX_Time = int(time.time())
current_UNIX_Time = str(current_UNIX_Time)

In [2]:
x = ("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/47.407875,9.464714," + current_UNIX_Time)

In [3]:
#What I need:
#Temperature x
#Summary
#High-Temp Feeling in words for the day
#Time of High Temperature
#High-Temp for the day
#Low-Temp for the day
#Rain-Warning

In [4]:
response = requests.get(x)
weather_forecast_trogen = response.json()

In [5]:
#Temperature:

In [6]:
def celsius(x):
    return round((x -32) /1.8)

In [7]:
weather_forecast_trogen['currently'].keys()


Out[7]:
dict_keys(['humidity', 'windSpeed', 'time', 'summary', 'temperature', 'ozone', 'dewPoint', 'apparentTemperature', 'precipIntensity', 'cloudCover', 'windBearing', 'pressure', 'precipProbability', 'icon', 'visibility'])

In [8]:
Current_Temperature = celsius(weather_forecast_trogen['currently']['apparentTemperature'])
Current_Temperature


Out[8]:
16

In [9]:
#Summary

In [10]:
Summary = weather_forecast_trogen['currently']['summary']

In [11]:
#weather_forecast_trogen['daily']['data']

In [12]:
#High-Temp Feeling in words for the day

In [13]:
for TemperatureMax in weather_forecast_trogen['daily']['data']:
    apparentTemperatureMax_tomorrow = TemperatureMax['apparentTemperatureMax']
    break

apparentTemperatureMax_tomorrow = celsius(apparentTemperatureMax_tomorrow)

if apparentTemperatureMax_tomorrow > 30:
    apparentTemperatureMax_tomorrow = "hot"
elif apparentTemperatureMax_tomorrow > 25:
    apparentTemperatureMax_tomorrow = "warm"
elif apparentTemperatureMax_tomorrow > 18:
    apparentTemperatureMax_tomorrow = "warmish"
elif apparentTemperatureMax_tomorrow > 8:
    apparentTemperatureMax_tomorrow = "cool"
elif apparentTemperatureMax_tomorrow > 0:
    apparentTemperatureMax_tomorrow = "nearly freezing"
else: 
    apparentTemperatureMax_tomorrow = "icey cold"

In [14]:
#High-Temp for the day

In [15]:
for TemperatureMax in weather_forecast_trogen['daily']['data']:
    TemperatureMax_tomorrow = TemperatureMax['apparentTemperatureMax']
    break

celsius(TemperatureMax_tomorrow)


Out[15]:
29

In [16]:
#Time of High Temperature

In [17]:
for TemperatureMaxTime in weather_forecast_trogen['daily']['data']:
    TemperatureMaxtime_tomorrow = TemperatureMaxTime['apparentTemperatureMaxTime']
    break

import datetime
TemperatureMaxTime = (
    datetime.datetime.fromtimestamp(
        int(TemperatureMaxtime_tomorrow)
    ).strftime('%H:%M')
)

In [18]:
#Min-Temp of the day

In [19]:
for TemperatureMin in weather_forecast_trogen['daily']['data']:
    TemperatureMin_tomorrow = TemperatureMin['apparentTemperatureMin']
    break

celsius(TemperatureMin_tomorrow)


Out[19]:
16

In [20]:
#Time of Min of the day

In [21]:
for TemperatureMinTime in weather_forecast_trogen['daily']['data']:
    TemperatureMintime_tomorrow = TemperatureMinTime['apparentTemperatureMinTime']
    break

import datetime
TemperatureMinTime = (
    datetime.datetime.fromtimestamp(
        int(TemperatureMintime_tomorrow)
    ).strftime('%H:%M')
)

In [22]:
#Rain-Warning

In [23]:
icon = weather_forecast_trogen['currently']['icon']

In [24]:
if icon == 'clear-day':
    icon = "clear"
elif icon == 'clear-night':
    icon = "clear"
elif icon == 'rain':
    icon = "rainy"
elif icon == 'snow':
    icon = "snowy"
elif icon == 'sleet':
    icon = "sleety"
elif icon == 'wind':
    icon = "windy"
elif icon == 'foggy':
    icon = "foggy"
elif icon == 'cloudy':
    icon = 'cloudy'
elif icon == 'partly-cloudy day.':
    icon = 'partly-cloudy'
elif icon == 'partly-cloudy-night':
    icon = 'partly-cloudy'

print(icon)


clear

In [25]:
print("The current temperature is " + str(Current_Temperature) + " °C. It is " + Summary.lower() \
      + ". Tomorrow, a " + str(apparentTemperatureMax_tomorrow) + " day awaits you, with a maximum temperature of " + str(celsius(TemperatureMax_tomorrow)) + " °C, peaking at " + \
      str(TemperatureMaxTime) + ". The coldest hour of day is " + str(TemperatureMinTime) + " reaching a low of " \
      + str(celsius(TemperatureMin_tomorrow)) + " °C. All in all it will be a " + icon + " day.")


The current temperature is 16 °C. It is clear. Tomorrow, a warm day awaits you, with a maximum temperature of 29 °C, peaking at 09:00. The coldest hour of day is 20:00 reaching a low of 16 °C. All in all it will be a clear day.

Mailing


In [31]:
key = 'key-Xxxxxxxxxxxxxx'
sandbox = 'sandboxXxxxxxxxxxxxxx.mailgun.org'
recipient = 'barnaby.skinner@sonntagszeitung'

request_url = 'https://api.mailgun.net/v2/{0}/messages'.format(sandbox)
request = requests.post(request_url,
    auth=('api', key),
    data={
        'from': 'barnaby.skinner@gmail.com',
        'to': recipient, 
        'subject': 'Weather Report For Tomorrow',
        'text': ("The current temperature is " + str(Current_Temperature) + " °C. It is " + Summary.lower() \
      + ". Tomorrow, a " + str(apparentTemperatureMax_tomorrow) + " day awaits you, with a maximum temperature of " + str(celsius(TemperatureMax_tomorrow)) + " °C, peaking at " + \
      str(TemperatureMaxTime) + ". The coldest hour of day is " + str(TemperatureMinTime) + " reaching a low of " \
      + str(celsius(TemperatureMin_tomorrow)) + " °C. All in all it will be a " + icon + " day.")
})

print('Status: {0}'.format(request.status_code))
print('Body:   {0}'.format(request.text))


Status: 400
Body:   {
  "message": "Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in domain settings."
}

In [ ]: