-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_handler.py
More file actions
478 lines (405 loc) · 15.9 KB
/
main_handler.py
File metadata and controls
478 lines (405 loc) · 15.9 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# Copyright (C) 2013 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Request Handler for /main endpoint."""
__author__ = 'alainv@google.com (Alain Vongsouvanh)'
import io
import jinja2
import logging
import os
import webapp2
import math
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.ext import db
from google.appengine.api import users
import httplib2
from apiclient import errors
from apiclient.http import MediaIoBaseUpload
from apiclient.http import BatchHttpRequest
from oauth2client.appengine import StorageByKeyName
from model import Credentials
from model import Reminder
import util
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
PAGINATED_HTML = """
<article>
<figure>
<img src="glass://map?w=240&h=360&marker=0;42.369590,
-71.107132&marker=1;42.36254,-71.08726&polyline=;"
height="360" width="240">
</figure>
<section>
<div class="text-auto-size">
<p class="yellow">12 minutes to home</p><p>Medium traffic on Broadway</p>
</div>
</section>
</article>
"""
class _BatchCallback(object):
"""Class used to track batch request responses."""
def __init__(self):
"""Initialize a new _BatchCallback object."""
self.success = 0
self.failure = 0
def callback(self, request_id, response, exception):
"""Method called on each HTTP Response from a batch request.
For more information, see
https://developers.google.com/api-client-library/python/guide/batch
"""
if exception is None:
self.success += 1
else:
self.failure += 1
logging.error(
'Failed to insert item for user %s: %s', request_id, exception)
class MainHandler(webapp2.RequestHandler):
"""Request Handler for the main endpoint."""
def _render_template(self, message=None):
"""Render the main page template."""
template_values = {'userId': self.userid}
reminders = Reminder.gql("WHERE user = :user_id", user_id = users.get_current_user().user_id())
template_values['reminders'] = reminders
logging.info(reminders)
for reminder in reminders:
logging.info('reminder: ')
logging.info(reminder)
if message:
template_values['message'] = message
# self.mirror_service is initialized in util.auth_required.
try:
template_values['contact'] = self.mirror_service.contacts().get(
id='python-quick-start').execute()
except errors.HttpError:
logging.info('Unable to find Python Quick Start contact.')
timeline_items = self.mirror_service.timeline().list(maxResults=3).execute()
template_values['timelineItems'] = timeline_items.get('items', [])
subscriptions = self.mirror_service.subscriptions().list().execute()
for subscription in subscriptions.get('items', []):
collection = subscription.get('collection')
if collection == 'timeline':
template_values['timelineSubscriptionExists'] = True
elif collection == 'locations':
template_values['locationSubscriptionExists'] = True
template = jinja_environment.get_template('templates/index.html')
self.response.out.write(template.render(template_values))
@util.auth_required
def get(self):
"""Render the main page."""
# Get the flash message and delete it.
message = memcache.get(key=self.userid)
memcache.delete(key=self.userid)
self._render_template(message)
@util.auth_required
def post(self):
"""Execute the request and render the template."""
operation = self.request.get('operation')
# Dict of operations to easily map keys to methods.
operations = {
'insertSubscription': self._insert_subscription,
'deleteSubscription': self._delete_subscription,
'insertItem': self._insert_item,
'insertPaginatedItem': self._insert_paginated_item,
'insertItemWithAction': self._insert_item_with_action,
'insertItemAllUsers': self._insert_item_all_users,
'insertContact': self._insert_contact,
'deleteContact': self._delete_contact,
'deleteTimelineItem': self._delete_timeline_item,
'insertReminder': self._insert_reminder,
'insertReminderHere': self._insert_reminder_here,
'printLocation': self._get_latest_location,
'deleteReminder': self._delete_reminder
}
if operation in operations:
message = operations[operation]()
else:
message = "I don't know how to " + operation
# Store the flash message for 5 seconds.
memcache.set(key=self.userid, value=message, time=5)
self.redirect('/')
def _get_latest_location(self):
"""Print information about the latest known location for the current user.
Args:
service: Authorized Mirror service.
"""
service = self.mirror_service
try:
location = service.locations().get(id='latest').execute()
print 'Location recorded on: %s' % location.get('timestamp')
print ' > Lat: %s' % location.get('latitude')
print ' > Long: %s' % location.get('longitude')
print ' > Accuracy: %s meters' % location.get('accuracy')
MAX_DISTANCE = 200
lat = location.get('latitude')
lng = location.get('longitude')
reminders = Reminder.gql("WHERE user = :user_id", user_id = users.get_current_user().user_id())
logging.info('GETTING REMINDERS WHEN REQUESTING CURRENT LOCATION')
logging.info(reminders)
for reminder in reminders: # for each reminder in the database:
logging.info(reminder.title)
distance = self._get_distance(lat, lng, reminder.latitude, reminder.longitude)
# logging.info('latitude')
# logging.info(lat)
# logging.info(reminder.latitude)
# logging.info('longitude')
# logging.info(lng)
# logging.info(reminder.longitude)
logging.info('Distance from point: ')
logging.info(round(distance))
distance_msg = '%s feet away' % int(round(distance))
# if distance_feet > 5280:
# distance = distance / 5280
# distance_msg = '%.2f miles away' % distance
if distance < MAX_DISTANCE:
map_html = '''
<article>
<figure>
<img src="glass://map?w=240&h=360&marker=0;{0},
{1}&polyline=;"
height="360" width="240">
</figure>
<section>
<div class="text-auto-size">
<p class="yellow">Reminder</p>
<p>{2}</p>
<p style="font-size:.7em;">{3}</p>
</div>
</section>
</article>'''.format(lat, lng, reminder.title, distance_msg)
body = {
'html': map_html,
'text': reminder.title,
'location': location,
'menuItems': [{'action': 'NAVIGATE'}],
'notification': {'level': 'DEFAULT'}
}
self.mirror_service.timeline().insert(body=body).execute()
except errors.HttpError, e:
print 'An error occurred: %s' % e
def _get_distance(self, lat1, long1, lat2, long2):
# Convert latitude and longitude to
# spherical coordinates in radians.
degrees_to_radians = math.pi/180.0
# phi = 90 - latitude
phi1 = (90.0 - lat1)*degrees_to_radians
phi2 = (90.0 - lat2)*degrees_to_radians
# theta = longitude
theta1 = long1*degrees_to_radians
theta2 = long2*degrees_to_radians
# Compute spherical distance from spherical coordinates.
# For two locations in spherical coordinates
# (1, theta, phi) and (1, theta, phi)
# cosine( arc length ) =
# sin phi sin phi' cos(theta-theta') + cos phi cos phi'
# distance = rho * arc length
cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) +
math.cos(phi1)*math.cos(phi2))
arc = math.acos( cos )
# Returns distance in feet (arc * 3960 = dist in miles, * 5280 = dist in feet)
return arc * 3960 * 5280
def _insert_subscription(self):
"""Subscribe the app."""
# self.userid is initialized in util.auth_required.
body = {
'collection': self.request.get('collection', 'timeline'),
'userToken': self.userid,
'callbackUrl': util.get_full_url(self, '/notify')
}
# self.mirror_service is initialized in util.auth_required.
self.mirror_service.subscriptions().insert(body=body).execute()
return 'Application is now subscribed to updates.'
def _delete_subscription(self):
"""Unsubscribe from notifications."""
collection = self.request.get('subscriptionId')
self.mirror_service.subscriptions().delete(id=collection).execute()
return 'Application has been unsubscribed.'
def _insert_item(self):
"""Insert a timeline item."""
logging.info('Inserting timeline item')
body = {
'notification': {'level': 'DEFAULT'}
}
if self.request.get('html') == 'on':
body['html'] = [self.request.get('message')]
else:
body['text'] = self.request.get('message')
media_link = self.request.get('imageUrl')
if media_link:
if media_link.startswith('/'):
media_link = util.get_full_url(self, media_link)
resp = urlfetch.fetch(media_link, deadline=20)
media = MediaIoBaseUpload(
io.BytesIO(resp.content), mimetype='image/jpeg', resumable=True)
else:
media = None
# self.mirror_service is initialized in util.auth_required.
self.mirror_service.timeline().insert(body=body, media_body=media).execute()
return 'A timeline item has been inserted.'
def _insert_reminder_here(self):
"""Insert a reminder at current location."""
logging.info('Inserting reminder here')
rem_id = self.request.get('nearby-reminder-id')
lat = self.request.get('nearby-reminder-lat')
lng = self.request.get('nearby-reminder-lng')
text = self.request.get('sent-nearby-reminder-name')
r = Reminder(reminder_id = int(rem_id), title = text, longitude = float(lng), latitude = float(lat), user = users.get_current_user().user_id())
r.put()
map_html = '''
<article>
<figure>
<img src="glass://map?w=240&h=360&marker=0;{0},
{1}&polyline=;"
height="360" width="240">
</figure>
<section>
<div class="text-auto-size">
<p class="yellow">Reminder</p>
<p>{2}</p>
</div>
</section>
</article>'''.format(lat, lng, text)
body = {
'html': map_html,
'text': text,
'notification': {'level': 'DEFAULT'}
}
self.mirror_service.timeline().insert(body=body).execute()
return 'A timeline reminder has been inserted here.'
def _insert_reminder(self):
"""Insert a reminder."""
logging.info('Inserting reminder')
rem_id = self.request.get('reminder-id')
lat = self.request.get('reminder-lat')
lng = self.request.get('reminder-lng')
text = self.request.get('sent-reminder-name')
r = Reminder(reminder_id = int(rem_id), title = text, longitude = float(lng), latitude = float(lat), user = users.get_current_user().user_id())
r.put()
map_html = '''
<article>
<figure>
<img src="glass://map?w=240&h=360&marker=0;{0},
{1}&polyline=;"
height="360" width="240">
</figure>
<section>
<div class="text-auto-size">
<p class="yellow">Reminder</p>
<p>{2}</p>
</div>
</section>
</article>'''.format(lat, lng, text)
body = {
'html': map_html,
'text': text,
'notification': {'level': 'DEFAULT'}
}
self.mirror_service.timeline().insert(body=body).execute()
return 'A timeline reminder has been inserted.'
def _delete_reminder(self):
'''Delete a reminder.'''
logging.info('Deleting a reminder')
rem_id = self.request.get('reminder-id')
deleteReminder = db.GqlQuery("select * from Reminder where reminder_id = :1", int(rem_id))
result = deleteReminder.get()
logging.info(result)
db.delete(result)
return 'A timeline reminder has been deleted.'
def _insert_paginated_item(self):
"""Insert a paginated timeline item."""
logging.info('Inserting paginated timeline item')
body = {
'html': PAGINATED_HTML,
'notification': {'level': 'DEFAULT'},
'menuItems': [{
'action': 'OPEN_URI',
'payload': 'https://www.google.com/search?q=cat+maintenance+tips'
}]
}
# self.mirror_service is initialized in util.auth_required.
self.mirror_service.timeline().insert(body=body).execute()
return 'A timeline item has been inserted.'
def _insert_item_with_action(self):
"""Insert a timeline item user can reply to."""
logging.info('Inserting timeline item')
body = {
'creator': {
'displayName': 'Python Starter Project',
'id': 'PYTHON_STARTER_PROJECT'
},
'text': 'Tell me what you had for lunch :)',
'notification': {'level': 'DEFAULT'},
'menuItems': [{'action': 'REPLY'}]
}
# self.mirror_service is initialized in util.auth_required.
self.mirror_service.timeline().insert(body=body).execute()
return 'A timeline item with action has been inserted.'
def _insert_item_all_users(self):
"""Insert a timeline item to all authorized users."""
logging.info('Inserting timeline item to all users')
users = Credentials.all()
total_users = users.count()
if total_users > 10:
return 'Total user count is %d. Aborting broadcast to save your quota' % (
total_users)
body = {
'text': 'Hello Everyone!',
'notification': {'level': 'DEFAULT'}
}
batch_responses = _BatchCallback()
batch = BatchHttpRequest(callback=batch_responses.callback)
for user in users:
creds = StorageByKeyName(
Credentials, user.key().name(), 'credentials').get()
mirror_service = util.create_service('mirror', 'v1', creds)
batch.add(
mirror_service.timeline().insert(body=body),
request_id=user.key().name())
batch.execute(httplib2.Http())
return 'Successfully sent cards to %d users (%d failed).' % (
batch_responses.success, batch_responses.failure)
def _insert_contact(self):
"""Insert a new Contact."""
logging.info('Inserting contact')
id = self.request.get('id')
name = self.request.get('name')
image_url = self.request.get('imageUrl')
if not name or not image_url:
return 'Must specify imageUrl and name to insert contact'
else:
if image_url.startswith('/'):
image_url = util.get_full_url(self, image_url)
body = {
'id': id,
'displayName': name,
'imageUrls': [image_url],
'acceptCommands': [{ 'type': 'TAKE_A_NOTE' }]
}
# self.mirror_service is initialized in util.auth_required.
self.mirror_service.contacts().insert(body=body).execute()
return 'Inserted contact: ' + name
def _delete_contact(self):
"""Delete a Contact."""
# self.mirror_service is initialized in util.auth_required.
self.mirror_service.contacts().delete(
id=self.request.get('id')).execute()
return 'Contact has been deleted.'
def _delete_timeline_item(self):
"""Delete a Timeline Item."""
logging.info('Deleting timeline item')
# self.mirror_service is initialized in util.auth_required.
self.mirror_service.timeline().delete(id=self.request.get('itemId')).execute()
return 'A timeline item has been deleted.'
MAIN_ROUTES = [
('/', MainHandler)
]