-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrain_alert.py
44 lines (34 loc) · 1.26 KB
/
rain_alert.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
import requests
import datetime
from iMessage import send_imessage_text
from config import OPENWEATHERMAP_API_KEY, RAIN_ALERT_MESSAGE
def call_weather_api(city):
# Set the API endpoint URL
url = "https://api.openweathermap.org/data/2.5/forecast"
# Set the API parameters
params = {
"q": city, # replace with the name of the desired location
"appid": OPENWEATHERMAP_API_KEY, # replace with your OpenWeatherMap API key
"units": "imperial", # change to 'imperial' for Fahrenheit
}
# Send the API request and get the response
response = requests.get(url, params=params)
data = response.json()
return data
def will_rain(city) -> bool:
daily_forecasts = call_weather_api(city)
# Check if there is rain in weather forecast
for forecast in daily_forecasts["list"]:
forecast_time = datetime.datetime.fromtimestamp(forecast["dt"])
if (
forecast_time.date() == datetime.date.today()
and forecast["weather"][0]["main"] == "Rain"
):
return True
return False
def send_rain_alert(recipient_number, city):
if will_rain(city):
send_imessage_text(
recipient_number=recipient_number,
message=RAIN_ALERT_MESSAGE,
)