-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduleweathertext.py
80 lines (65 loc) · 3.03 KB
/
scheduleweathertext.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import time
import schedule
import smtplib
from email.message import EmailMessage
import ssl
import datetime as dt
import requests
# Define the function that you want to execute
def my_function():
email_password = "password"
email_receiver = "[email protected]" # The text message address of the phone number, ex. [email protected]
# Replace YOUR_API_KEY with your actual API key
API_KEY = "00000000000000000000000000000"
# Set the API endpoint URL and parameters
BASE_url = "http://api.openweathermap.org/data/2.5/weather?"
lat = "42.3601"
lon = "71.0589"
city = "Boston"
url = BASE_url + "lat=" + lat + "&lon=" + lon + "&appid=" + API_KEY + "&units=imperial"
# Send a GET request to the API endpoint
response = requests.get(url).json()
temp_fahrenheit = response['main']['temp']
temp_celsius = (temp_fahrenheit - 32) * 5 / 9
feels_like_fahrenheit = response['main']['feels_like']
feels_like_celsius = (feels_like_fahrenheit - 32) * 5 / 9
wind_speed = response['wind']['speed']
wind_gust = response['wind']['gust']
humidity = response['main']['humidity']
description = response['weather'][0]['description']
sunrise_time = dt.datetime.utcfromtimestamp(response['sys']['sunrise'] + response['timezone']).strftime('%H:%M:%S')
sunset_time = dt.datetime.utcfromtimestamp(response['sys']['sunset'] + response['timezone']).strftime('%H:%M:%S')
subject = "Current weather in Winter Garden: "
body = (f"Temperature in {city}: {temp_celsius:.2f}C or {temp_fahrenheit:.2f}F" +
f"\nTemperature feels like: {feels_like_celsius:.2f}C or {feels_like_fahrenheit:.2f}F" +
f"\nHumidity: {humidity}%" +
f"\nWind speed: {wind_speed}mph | Gusts: {wind_gust}mph" +
f"\nGeneral weather: {description}" +
f"\nSun rise: {sunrise_time}" +
f"\nSun set: {sunset_time}")
em = EmailMessage()
em["From"] = email_sender
em["To"] = email_receiver
em["Subject"] = subject
em.set_content(body)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as smtp:
smtp.login(email_sender, email_password)
smtp.sendmail(email_sender, email_receiver, em.as_string())
# Schedule the function to run on weekdays at 6 am
schedule.every().monday.at("06:00").do(my_function)
schedule.every().tuesday.at("06:00").do(my_function)
schedule.every().wednesday.at("06:00").do(my_function)
schedule.every().thursday.at("06:00").do(my_function)
schedule.every().friday.at("06:00").do(my_function)
# Schedule the function to run on Saturdays at 8 am
schedule.every().saturday.at("08:00").do(my_function)
# Schedule the function to run on Sundays at 8 am
schedule.every().sunday.at("08:00").do(my_function)
# Keep the program running indefinitely
while True:
print("My Weather Text Alert Program:")
print("Checking the time and waiting for scheduled texts...")
schedule.run_pending()
time.sleep(60)