This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
63 lines (54 loc) · 2.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import smtplib
from urllib.request import urlopen
import json
import settings
import time
def get_weather_data(weather, day, indicator, unit=""):
""" Gets weather data from parsed json
Parameters:
weather (dict): Weather data
day (int): Day number (0 = Today, 1 = Tomorrow, etc.)
indicator (str): Weather indicator (ie. 'high', 'maxwind', etc.)
unit (str): Unit of measurement (it 'in', 'mph', etc.)
Returns:
(str): Value of weather data
"""
if not(unit):
return weather["forecast"]["simpleforecast"]["forecastday"][day][indicator]
else:
return weather["forecast"]["simpleforecast"]["forecastday"][day][indicator][unit]
response = urlopen(settings.weather_url)
weather = json.loads(response.read())
smtp_server = smtplib.SMTP(host=settings.smtp_server, port=settings.smtp_port)
smtp_server.ehlo()
smtp_server.starttls()
smtp_server.ehlo()
smtp_server.login(settings.from_email, settings.from_email_password)
for day in settings.forecast_days:
max_temp = get_weather_data(weather, day, "high", "fahrenheit")
min_temp = get_weather_data(weather, day, "low", "fahrenheit")
max_wind = get_weather_data(weather, day, "maxwind", "mph")
wind_dir = get_weather_data(weather, day, "maxwind", "dir")
avg_humidity = get_weather_data(weather, day, "avehumidity")
precip_percent = get_weather_data(weather, day, "pop")
precip_amt = get_weather_data(weather, day, "qpf_allday", "in")
subject = "Weather"
weather_day = "Today" if day == 0 else "Tomorrow" if day == 1 else ""
msg = "Good Morning Dad! Here's {weather_day}'s Weather: MaxTemp:{max_temp} MinTemp:{min_temp} MaxWind:{max_wind} Humidity:{avg_humidity} Precip%:{precip_percent} PrecipAmt:{precip_amt}".format(
weather_day=weather_day,
max_temp=max_temp,
min_temp=min_temp,
max_wind=str(max_wind) + wind_dir,
avg_humidity=avg_humidity,
precip_percent=precip_percent,
precip_amt=precip_amt
)
smtp_server.sendmail(
settings.from_email,
settings.to_email,
subject + ":" + "\n\n" + msg
)
smtp_server.quit()
# Log Process #
with open(settings.log_file, "a") as f:
f.write(f"{time.strftime('%Y-%m-%d-%X')}: Email sent to {settings.to_email} from {settings.from_email}\n")