-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (45 loc) · 1.47 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
import requests
from dotenv import load_dotenv
import os
import smtplib
from email.message import EmailMessage
load_dotenv()
API_KEY = os.environ.get("API_KEY")
RECIPIENT_ADDR = os.environ.get("RECIPIENT_ADDR")
FROM_ADDR = os.environ.get("FROM_ADDR")
LOGIN = os.environ.get("LOGIN")
PASSWORD = os.environ.get("PASSWORD")
MAILHOST = os.environ.get("MAILHOST")
PORT = os.environ.get("PORT")
# This is the 3-hour (for max 5days) forecast API:
API_URL = "https://api.openweathermap.org/data/2.5/forecast"
PERIODS = 4 # 12 hours
params = {
"lat": 47.258949,
"lon": 8.848430,
"appid": API_KEY,
"cnt": PERIODS
}
def send_mail(email_addr, subject, content, bcc=None):
print(f"Sending mail to {email_addr} from {FROM_ADDR}")
with smtplib.SMTP(host=MAILHOST, port=PORT) as connection:
connection.starttls()
connection.login(LOGIN, PASSWORD)
msg = EmailMessage()
msg.set_content(content)
msg["Subject"] = subject
msg["From"] = FROM_ADDR
msg["To"] = email_addr
if bcc:
msg["Bcc"] = bcc
connection.send_message(msg)
response = requests.get(API_URL, params=params)
response.raise_for_status()
weather_data = response.json()
for hour_data in weather_data["list"]:
condition = hour_data['weather'][0]['id']
if condition < 700:
will_rain = True
if will_rain:
send_mail(RECIPIENT_ADDR, subject="Regenschirm mitnehmen!",
content="Es wird heute regnen oder schneien...")