-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.py
403 lines (347 loc) · 10.7 KB
/
alerts.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import datetime
import json
import locale
import os
import time
import traceback
from models import *
from jobs import *
import jinja2
import pytz
import urllib2
import webapp2
from google.appengine.ext import ndb
from google.appengine.ext import deferred
from google.appengine.api import mail
from google.appengine.api.taskqueue import Task
########## All the code in this section is for the regularly scheduled alerts ##########
def sendEmail(
to=None,
start_names=None,
start_bikes=None,
start_times=None,
end_names=None,
end_docks=None,
end_times=None,
alert_id=None
):
body_contents = []
if len(start_names) > 0:
body_contents.append('starting stations:\n')
for key in start_names:
start_text = '%(start_name)s at %(start_time)s \n\
bikes: %(start_bikes)d \n' % \
{'start_time': start_times[key], \
'start_bikes': start_bikes[key], \
'start_name': start_names[key]}
body_contents.append(start_text)
if len(end_names) > 0:
body_contents.append('\nending stations:\n')
for key in end_names:
end_text = '%(end_name)s at %(end_time)s \n\
docks: %(end_docks)d \n' % \
{'end_time': end_times[key], \
'end_docks': end_docks[key], \
'end_name': end_names[key]}
body_contents.append(end_text)
body_contents.append('\nenjoy your journey!\n-busybici')
body_contents.append('\n\nto unsubscribe click http://www.busybici.com/cancel/')
body_contents.append(alert_id)
body = ''.join(body_contents)
message = mail.EmailMessage(
sender='busybici <[email protected]>',
subject='busybici update',
to=to,
body=body
)
message.send()
def sendSMS(
to='',
start_names='',
start_bikes='',
start_times='',
end_names='',
end_docks='',
end_times=''
):
body_contents = []
if len(start_names) > 0:
body_contents.append('bikes at\n')
for key in start_names:
start_text = '-%(start_name)s: %(start_bikes)d\n' % \
{'start_bikes': start_bikes[key], \
'start_name': start_names[key]}
body_contents.append(start_text)
if len(end_names) > 0:
body_contents.append('\ndocks at\n')
for key in end_names:
end_text = '-%(end_name)s: %(end_docks)d\n' % \
{'end_docks': end_docks[key], \
'end_name': end_names[key]}
body_contents.append(end_text)
body_contents.append('\nlast update: ')
body_contents.append(start_times['start1'])
body = ''.join(body_contents)
message = mail.EmailMessage(
sender='busybici <[email protected]>',
subject='',
to=to,
body=body
)
message.send()
def distribute(
entity=None,
start_names=None,
start_bikes=None,
start_times=None,
end_names=None,
end_docks=None,
end_times=None
):
if entity.email != None:
sendEmail(
to=entity.email,
start_names=start_names,
start_bikes=start_bikes,
start_times=start_times,
end_names=end_names,
end_docks=end_docks,
end_times=end_times,
alert_id=str(entity.alert_id)
)
if entity.phone != None:
to = str(entity.phone) + carriers[entity.carrier]
sendSMS(
to=to,
start_names=start_names,
start_bikes=start_bikes,
start_times=start_times,
end_names=end_names,
end_docks=end_docks,
end_times=end_times
)
def generate_msg_info(
entity
):
start_names = {}
start_bikes = {}
start_times = {}
end_names = {}
end_docks = {}
end_times = {}
def generate_start_station_info(label='', start_station_id=None):
start_status = StationStatus.query(StationStatus.station_id == start_station_id).order(
-StationStatus.date_time).get()
start_avail_bikes = start_status.availableBikes
start_time = start_status.date_time
start_time = convertTime(start_time)
start_info = StationInfo.query(StationInfo.station_id == start_station_id).get()
start_name = start_info.name
start_bikes.update({label: start_avail_bikes})
start_names.update({label: start_name})
start_times.update({label: start_time})
def generate_end_station_info(label='', end_station_id=None):
end_status = StationStatus.query(StationStatus.station_id == end_station_id).order(
-StationStatus.date_time).get()
end_avail_docks = end_status.availableDocks
end_time = end_status.date_time
end_time = convertTime(end_time)
end_info = StationInfo.query(StationInfo.station_id == end_station_id).get()
end_name = end_info.name
end_docks.update({label: end_avail_docks})
end_names.update({label: end_name})
end_times.update({label: end_time})
if entity.start1 != None:
generate_start_station_info(
label='start1',
start_station_id=entity.start1
)
if entity.start2 != None:
generate_start_station_info(
label='start2',
start_station_id=entity.start2
)
if entity.start3 != None:
generate_start_station_info(
label='start3',
start_station_id=entity.start3
)
if entity.end1 != None:
generate_end_station_info(
label='end1',
end_station_id=entity.end1
)
if entity.end2 != None:
generate_end_station_info(
label='end2',
end_station_id=entity.end2
)
if entity.end3 != None:
generate_end_station_info(
label='end3',
end_station_id=entity.end3
)
distribute(
entity=entity,
start_names=start_names,
start_bikes=start_bikes,
start_times=start_times,
end_names=end_names,
end_docks=end_docks,
end_times=end_times
)
class SendAlerts(webapp2.RequestHandler):
def requestFreshData(self, alert_time):
# first see if the new data is even needed
# by checking when the last update was
last_update_query = StationStatus.query().order(-StationStatus.date_time).get()
naive_last_update_time = last_update_query.date_time
last_update_time = assignTimeZone(naive_last_update_time)
today = datetime.date.today()
year = int(today.strftime('%Y'))
month = int(today.strftime('%m'))
day = int(today.strftime('%d'))
alert_hour = int(alert_time.strftime('%H'))
alert_minute = int(alert_time.strftime('%M'))
datefied_alert_time = datetime.datetime(year, month, day, alert_hour, alert_minute)
datefied_alert_time = assignNYCTimeZone(datefied_alert_time)
gap = datefied_alert_time - last_update_time
gap_seconds = gap.total_seconds()
# then by comparing the last update time to the scheduled alert time
if gap_seconds > 240: # gap greater than 4 minutes, or 240 seconds
logging.debug('Requesting fresh data.')
need_fresh_data = True
else:
need_fresh_data = False
logging.debug('Already have the most current data.')
# then if you need it, try to get it
if need_fresh_data:
try:
update_request = urllib2.Request('http://www.busybici.com/admin/updatestatus')
req_time = datetime.datetime.now()
open_time = datetime.datetime.now() - req_time
logging.debug('Time to open URL: %s', open_time)
logging.debug('Got it.')
except:
logging.debug('Timed out.')
time.sleep(10) # regardless, give the new data a moment to propagate
def send_alerts(self, wait=0):
todays_alerts = AlertLog.query()
todays_alerts_len = todays_alerts.filter(AlertLog.complete == False).count()
if todays_alerts_len == 0:
logging.debug('Done for the day. See you tomorrow.')
else:
while todays_alerts_len > 0:
current_alerts = todays_alerts.filter(AlertLog.complete == False).order(AlertLog.time)
a = current_alerts.get()
now = makeNowTime()
if a.time <= now:
self.requestFreshData(a.time)
generate_msg_info(a)
logging.debug(
'I sent an alert that was scheduled for %s.',
a.time.strftime('%I:%M %p')
)
a.complete = True
a.sent = datetime.datetime.now()
a.put()
todays_alerts_len = todays_alerts_len - 1
time.sleep(1) # give it a second for the new data to take
else:
wait = makeWait(a.time)
logging.debug(
'Going to count down for %d seconds.',
wait
)
break
the_only_other_task = Task(payload=None, url="/admin/sendalerts", countdown=wait)
the_only_other_task.add(queue_name="alertsqueue")
def post(self):
self.response.out.write('SendAlerts successfully initiated.')
self.send_alerts()
########## All the code in this section is to send the confirmation emails ##########
def sendConfirmEmail(
to=None,
alert_id=None,
start_names=None,
start_bikes=None,
start_times=None,
end_names=None,
end_docks=None,
end_times=None
):
body_contents = []
body_contents.append('thanks for signing up! click the confirmation link below to begin receiving your alerts.')
confirm_url = 'http://www.busybici.com/confirm/'+str(alert_id)
body_contents.append('\n')
body_contents.append(confirm_url)
body = ''.join(body_contents)
message = mail.EmailMessage(
sender='busybici <[email protected]>',
subject='confirm your new alert',
to=to,
body=body
)
message.send()
########## Utils ##########
def convertTime(t):
# takes a naive datetime, assigns it the UTC time zone,
# converts to NY time, then returns it formatted as a string
utc = pytz.timezone('UTC')
newyork = pytz.timezone('America/New_York')
t = utc.localize(t)
t = t.astimezone(newyork)
t = t.strftime('%I:%M %p')
return t
def assignTimeZone(t):
# same as convertTime but returns a datetime object instead of a string
utc = pytz.timezone('UTC')
newyork = pytz.timezone('America/New_York')
t = utc.localize(t)
t = t.astimezone(newyork)
return t
def assignNYCTimeZone(t):
newyork = pytz.timezone('America/New_York')
t = newyork.localize(t)
return t
def makeNowTime():
# establish the time zones
utc = pytz.timezone('UTC')
newyork = pytz.timezone('America/New_York')
# make the now, make it aware of its UTC time zone, and convert to NY time
n = datetime.datetime.now()
n = utc.localize(n)
n = n.astimezone(newyork)
# Reduce to just the time in HH:MM:SS.
n = n.time()
n = n.replace(microsecond=0)
return n
def makeWait(x):
now = makeNowTime()
now_seconds = (now.hour * 3600) + (now.minute * 60) + now.second
next_time = (x.hour * 3600) + (x.minute * 60) + x.second
diff = next_time - now_seconds
# Give a few seconds of cushion. This helps make sure that tasks scheduled
# on the hour and the half hour get the most up-to-date status info.
diff += 5
# Disallow negative wait times.
if diff <= 0:
return 0
else:
# 1 hour max wait time
wait = min(diff, 3600)
return wait
carriers = {'AT&T': '@txt.att.net',
'Qwest': '@qwestmp.com',
'T-Mobile': '@tmomail.net',
'Verizon': '@vtext.com',
'Sprint': '@pm.sprint.com',
'Virgin Mobile': '@vmobl.com',
'Nextel': '@messaging.nextel.com',
'Alltel': '@message.alltel.com',
'Metro PCS': '@mymetropcs.com',
'Powertel': '@ptel.com',
'Boost Mobile': '@myboostmobile.com',
'Suncom': '@tms.suncom.com',
'Tracfone': '@mmst5.tracfone.com',
'U.S. Cellular': '@email.uscc.net'}