In [166]:
import requests
import datetime
In [167]:
def rain_warning(precip_chance, precip_type):
warning=''
if precip_chance >= 0.9:
warning="Whoa, it's almost certainly "+precip_type+"ing today. Bring an umbrella or something."
elif precip_chance >= 0.5:
warning="Better than even chance of "+precip_type+"ing today. Might want to look out for that"
elif precip_chance >= 0.1:
warning="Non-zero chance of "+precip_type+"ing today. Just sayin'."
else:
warning="Pretty much nothing going on today. Might see some "+ precip_type+", but don't count on it."
return(warning)
In [168]:
def send_mail(message_text, subject):
return requests.post(
"https://api.mailgun.net/v3/sandbox5375a212cf1648e59f6321df8fdb88da.mailgun.org/messages",
auth=("api", "key-983839b29e1a96ab85533d0097cf5ba5"),
data={"from": "weather bot <mailgun@mg.seanmoriarty.com>",
"to": "moriarty.22@gmail.com",
"subject": subject,
"text": message_text})
In [169]:
url = 'https://api.forecast.io/forecast/'
apikey = '53343f8442d30d598f50f5910124610a'
latlong = '40.8117150,-73.9578630'
response = requests.get(url+apikey+'/'+latlong)
forecast_complete = response.json()
In [170]:
current_conditions=forecast_complete['currently']
forecast_today=forecast_complete['daily']['data'][0]
In [171]:
forecast_email=["Right now it is ", current_conditions['summary'].lower(), " out and feels like ", str(current_conditions['temperature']),
" degrees. Today it will be ", forecast_today['summary'][:-1].lower()+", with a high of ",
str(forecast_today['temperatureMax']), " and a low of ", str(forecast_today['temperatureMin'])," degrees. "]
if 'precipType' in forecast_today:
forecast_email.append(rain_warning(forecast_today['precipProbability'], forecast_today['precipType']))
else:
forecast_email.append("Literally no chance of precipitation today. ")
In [172]:
today_raw = datetime.datetime.today()
today = today_raw.strftime("%A %B %d %Y").split(' ')
msg_subject=('8AM Weather forecast for ', today[0]+', ', today[1], ' ', today[2]+', ', today[3])
In [173]:
send_mail(''.join(forecast_email), ''.join(msg_subject))
Out[173]:
In [ ]: