-
Notifications
You must be signed in to change notification settings - Fork 2
/
reminders.py
271 lines (229 loc) · 11.5 KB
/
reminders.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import requests
from datetime import datetime
import datetime as dt
from twilio.rest import Client
from twilio.twiml.messaging_response import MessagingResponse
import time
import threading
import sqlite3
import time
def check_reminders():
mydb = sqlite3.connect('userdata.db')
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM reminders")
output = mycursor.fetchall()
current_day = datetime.today().day
current_month = datetime.today().month
current_hour = datetime.today().hour
weather_conditions = {1: "temp", 2: "pop", 3: "clouds", 4: "wind_spd", 5: "uv", 6: "vis", 7: "rh"}
weather_units = {1: "°C", 2: "%", 3: "%", 4: "m/s", 5: "", 6: "km", 7: "%"}
for reminder in output:
if reminder[5] == "daily" and int(reminder[3]) == current_hour and int(reminder[4]) == current_day:
options = reminder[2]
options = [int(d) for d in str(options)]
mymsg = ""
for option in options:
if option == 1:
val = get_weather_condition(weather_conditions[option], reminder[1])
desc = ""
if val <= -4:
desc = "Take a heavy jacket with you. It's cold!"
elif -3 <= val <= 6:
desc = "Take a jacket with you!"
elif 7 <= val <= 17:
desc = "Consider taking a light jacket. It's a bit chilly!"
elif 18 <= val <= 26:
desc = "Wear lighter clothes. It's pretty warm!"
elif 27 <= val:
desc = "Wear shorts. It's hot today!"
mymsg += "It's " + str(val) + weather_units[option] + ". " + desc + "\n\n"
elif option == 2:
val = get_weather_condition(weather_conditions[option], reminder[1])
desc = ""
if val < 30:
desc = " Clear day out, huh?"
elif val > 60:
desc = " Don't forget an umbrella!"
mymsg += "There's a " + str(val) + weather_units[option] + " chance of rain." + desc + "\n\n"
elif option == 3:
val = get_weather_condition(weather_conditions[option], reminder[1])
desc = ""
if val < 30:
desc = " Take some sunglasses with you. It's pretty sunny out!"
mymsg += "The cloudiness is " + str(val) + weather_units[option] + "." + desc + "\n\n"
elif option == 4:
val = get_weather_condition(weather_conditions[option], reminder[1])
desc = ""
if val > 10:
desc = " Grab a wind breaker. It's windy!"
mymsg += "The wind is " + str(val) + weather_units[option] + "." + desc + "\n\n"
elif option == 5:
val = get_weather_condition(weather_conditions[option], reminder[1])
desc = ""
high = ""
val = round(val, 1)
if 0 <= val <= 2:
desc = "Low"
elif 3 <= val <= 5:
desc = "Medium"
elif 6 <= val <= 7:
desc = "High"
high = " Don't forget sunscreen!"
elif 8 <= val <= 10:
desc = "Very High"
high = " Don't forget sunscreen!"
elif 11 <= val:
desc = "Extremely High"
high = " Don't forget sunscreen!"
mymsg += "The UV index is " + desc + " today." + high + "\n\n"
elif option == 6:
val = get_weather_condition(weather_conditions[option], reminder[1])
desc = ""
if val < 20:
desc = " Low visibility out there today. Be careful driving!"
mymsg += "The visibility is " + str(val) + weather_units[option] + "." + desc + "\n\n"
elif option == 7:
val = get_weather_condition(weather_conditions[option], reminder[1])
desc = ""
if val > 70:
desc = " High humidity today. It's might be hotter than normal!"
mymsg += "The humidity is " + str(val) + weather_units[option] + "." + desc + "\n\n"
elif option == 8:
val = get_air_quality(reminder[1])
desc = ""
high = ""
if 0 <= val <= 50:
desc = "Good"
elif 51 <= val <= 100:
desc = "Moderate"
elif 101 <= val <= 200:
desc = "Unhealthy"
high = " Take a mask with you!"
elif 201 <= val <= 300:
desc = "Very Unhealthy"
high = " Take a mask with you!"
elif 301 <= val:
desc = "Hazardous"
high = " Air is not looking good. Take a mask with you!"
mymsg += "The air quality today is " + desc + high + "\n\n"
send_sms_message(reminder[0], mymsg)
next_day = (datetime.today() + dt.timedelta(days=1)).day
sql = "UPDATE reminders SET day = ? WHERE phone = ? AND type = ?"
val = (str(next_day), reminder[0], reminder[5])
mycursor.execute(sql, val)
mydb.commit()
elif reminder[5] == "alerts" and int(reminder[3]) == current_hour and int(reminder[4]) < 24:
print('passed alerts')
options = reminder[2]
options = [int(d) for d in str(options)]
mymsg = ""
for option in options:
if option == 1:
val = get_weather_condition(weather_conditions[option], reminder[1])
if val <= 0:
desc = "Below freezing temperatures!"
mymsg += "It's " + str(val) + weather_units[option] + ". " + desc + "\n\n"
elif val >= 38:
desc = "It's scalding hot!"
mymsg += "It's " + str(val) + weather_units[option] + ". " + desc + "\n\n"
elif option == 2:
val = get_weather_condition(weather_conditions[option], reminder[1])
if val >= 60:
desc = " It's going to rain soon!"
mymsg += "There's a " + str(val) + weather_units[option] + " chance of rain." + desc + "\n\n"
# elif option == 3:
# val = get_weather_condition(weather_conditions[option], reminder[1])
# desc = ""
# if val < 30:
# desc = " Take some sunglasses with you. It's pretty sunny out!"
# mymsg += "The cloudiness is " + str(val) + weather_units[option] + "." + desc + "\n\n"
elif option == 4:
val = get_weather_condition(weather_conditions[option], reminder[1])
if val > 10:
desc = " It's getting very windy!"
mymsg += "The wind is " + str(val) + weather_units[option] + "." + desc + "\n\n"
elif option == 5:
val = get_weather_condition(weather_conditions[option], reminder[1])
desc = ""
high = ""
val = round(val, 1)
if 6 <= val <= 7:
desc = "High"
high = " Find some shade and reapply sunscreen!"
mymsg += "The UV index is " + desc + " right now." + high + "\n\n"
elif 8 <= val <= 10:
desc = "Very High"
high = " Get under some shade. UV index is very high!"
mymsg += "The UV index is " + desc + " right now." + high + "\n\n"
elif 11 <= val:
desc = "Extremely High"
high = " Get under some shade. UV index is very high!"
mymsg += "The UV index is " + desc + " right now." + high + "\n\n"
# elif option == 6:
# val = get_weather_condition(weather_conditions[option], reminder[1])
# desc = ""
# if val < 20:
# desc = " Low visibility out there today. Be careful driving!"
# mymsg += "The visibility is " + str(val) + weather_units[option] + "." + desc + "\n\n"
elif option == 7:
val = get_weather_condition(weather_conditions[option], reminder[1])
if val <= 20:
desc = " Low humidity! Apply some lotion."
mymsg += "The humidity is " + str(val) + weather_units[option] + "." + desc + "\n\n"
elif option == 8:
val = get_air_quality(reminder[1])
desc = ""
high = ""
if 101 <= val <= 200:
desc = "Unhealthy"
high = " Move indoors into a well-ventilated environment!"
mymsg += "The air quality right now is " + desc + high + "\n\n"
elif 201 <= val <= 300:
desc = "Very Unhealthy"
high = " Move indoors into a well-ventilated environment!"
mymsg += "The air quality right now is " + desc + high + "\n\n"
elif 301 <= val:
desc = "Hazardous"
high = " Move indoors into a well-ventilated environment!"
mymsg += "The air quality right now is " + desc + high + "\n\n"
if mymsg != "":
send_sms_message(reminder[0], mymsg)
next_day = int(reminder[4]) + 1
sql = "UPDATE reminders SET day = ? WHERE phone = ? AND type = ?"
val = (str(next_day), reminder[0], reminder[5])
mycursor.execute(sql, val)
mydb.commit()
next_hour = (int(reminder[3]) + 1) % 23
sql = "UPDATE reminders SET hour = ? WHERE phone = ? AND type = ?"
val = (str(next_hour), reminder[0], reminder[5])
mycursor.execute(sql, val)
mydb.commit()
def get_air_quality(location):
weatherBitKey = "key"
city, state = location.split(",")
result = requests.get(
"https://api.weatherbit.io/v2.0/forecast/airquality?city=" + city + "," + state + "&hours=24&key=" + weatherBitKey)
formatted = result.json()
return formatted["data"][0]["aqi"]
def get_weather_condition(type, location):
weatherBitKey = "key"
city, state = location.split(",")
result = requests.get(
"https://api.weatherbit.io/v2.0/forecast/hourly/?city=" + city + "," + state + "&hours=24&key=" + weatherBitKey)
formatted = result.json()
return formatted["data"][0][type]
def send_sms_message(number, message):
account_sid = 'sid'
auth_token = 'token'
client = Client(account_sid, auth_token)
message = client.messages \
.create(
body=message,
from_='+18013370504',
to=number
)
print(message.sid)
while True:
check_reminders()
print("called")
time.sleep(5)