APIs/Data Structures

Right now it is TEMPERATURE degrees out and SUMMARY. Today will be TEMP_FEELING with a high of HIGH_TEMP and a low of LOW_TEMP. RAIN_WARNING.

Forecasts Part Two: Sending data

Using a mailgun.com account and their API, send yourself an email every morning at 8AM telling you the above sentence. The subject line of the email should be something like "8AM Weather forecast: January 1, 1970"


In [63]:
import requests
response = requests.get('https://api.forecast.io/forecast/a69e446312b60b9982fba351359a544f/40.7128,74.0059')
data = response.json()

In [84]:
today = data['daily']['data'][0]
    
def temp_feeling(today):
    if today['temperatureMax'] >= 38:
        return "wretched"
    elif 27 < today['temperatureMax'] < 38:
        return "glorious"
    elif 10 < today['temperatureMax'] <= 27:
        return "tolerable"
    elif today['temperatureMax'] <= 10:
        return "frigid"

def rain_warning(today):
    if today['precipProbability'] > .5:
        return "Tut tut, looks like rain."
    else:
        return "Leave the rainboots at home today."

In [85]:
print("Right now it is", (data['currently']['temperature']), "degrees out and", (data['currently']['summary'].lower())+". Today it will be", temp_feeling(today),
      "with a high of", (today['temperatureMax']),
      "and a low of", (today['temperatureMin']), ".", rain_warning(today))


Right now it is 39.42 degrees out and mostly cloudy. Today it will be wretched with a high of 55.51 and a low of 35.27 . Leave the rainboots at home today.

In [3]:
#pubkey-415d5fcce3927c6eea8567f2ab78e8f1
    #https://documentation.mailgun.com/quickstart-sending.html#send-via-api
    
import time
      
def send_simple_message():
    return requests.post(
        "https://api.mailgun.net/v3/Sandbox54ef43a44a8a4e788eda99ba38bc4500/messages",
        auth=("api", "pubkey-415d5fcce3927c6eea8567f2ab78e8f1"),
        data={"from": "Excited User <mailgun@YOUR_DOMAIN_NAME>",
              "to": ["bar@example.com", "YOU@YOUR_DOMAIN_NAME"],
              "subject": "8 AM Weather Forecast:", dt.datetime.today().strftime("%B/%d/%Y")
              "text": "Right now it is", (data['currently']['temperature']), "degrees out and", (data['currently']['summary'].lower())+ ". Today it will be", temp_feeling(today),
      "with a high of", (today['temperatureMax']),
      "and a low of", (today['temperatureMin']), ".", rain_warning(today)})


        #get all in one file
        #export to atom for py
        #upload py file onto the server
        #then use crontab to tell it to run every day at 8 a.m.


  File "<ipython-input-3-4e1cb9c3efdc>", line 17
    "text": "Right now it is", (data['currently']['temperature']), "degrees out and", (data['currently']['summary'].lower())+ ". Today it will be", temp_feeling(today),
         ^
SyntaxError: invalid syntax

In [ ]: