-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
47 lines (39 loc) · 1.18 KB
/
weather.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
import smtplib
from email.message import EmailMessage
import ssl
import os
from dotenv import load_dotenv
import requests
import json
load_dotenv()
sender_email = 'nicholasmendoza140@gmail.com'
sender_password = os.getenv("EMAIL_PASSWORD")
receiver_email = 'nikkomdoza@gmail.com'
api_key = os.getenv("API_KEY")
api_url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q=94533&aqi=no"
temperature = ''
condition = ''
response = requests.get(api_url)
if response.status_code == 200:
data = json.loads(response.text)
temperature = data['current']['temp_f']
condition = data['current']['condition']['text']
else:
print(f"Request failed: {response.status_code}")
subject = "Today's weather"
body = f"""
The weather today in Fairfield is {temperature}\u00b0F and {condition}.
"""
em = EmailMessage()
em['From'] = sender_email
em['To'] = receiver_email
em['Subject'] = subject
em.set_content(body)
context = ssl.create_default_context()
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(sender_email, sender_password)
server.sendmail(sender_email, receiver_email, em.as_string())
server.quit()
except Exception as e:
print(f"Error: {str(e)}")