Repository navigation
Expand file tree
/
Copy pathhandler.py
More file actions
99 lines (86 loc) · 3.51 KB
/
Copy pathhandler.py
File metadata and controls
99 lines (86 loc) · 3.51 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
# 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 /notify endpoint."""
__author__ = 'alainv@google.com (Alain Vongsouvanh)'
import io
import json
import logging
import webapp2
from apiclient.http import MediaIoBaseUpload
from oauth2client.appengine import StorageByKeyName
from model import Credentials
import util
class NotifyHandler(webapp2.RequestHandler):
"""Request Handler for notification pings."""
def post(self):
"""Handles notification pings."""
logging.info('Got a notification with payload %s', self.request.body)
data = json.loads(self.request.body)
userid = data['userToken']
# TODO: Check that the userToken is a valid userToken.
self.mirror_service = util.create_service(
'mirror', 'v1',
StorageByKeyName(Credentials, userid, 'credentials').get())
if data.get('collection') == 'locations':
self._handle_locations_notification(data)
elif data.get('collection') == 'timeline':
self._handle_timeline_notification(data)
def _handle_locations_notification(self, data):
"""Handle locations notification."""
location = self.mirror_service.locations().get(id=data['itemId']).execute()
text = 'New location is %s, %s' % (location.get('latitude'),
location.get('longitude'))
logging.info(text)
body = {
'text': text,
'location': location,
'menuItems': [{'action': 'NAVIGATE'}],
'notification': {'level': 'DEFAULT'}
}
self.mirror_service.timeline().insert(body=body).execute()
def _handle_timeline_notification(self, data):
"""Handle timeline notification."""
for user_action in data.get('userActions', []):
if user_action.get('type') == 'SHARE':
# Fetch the timeline item.
item = self.mirror_service.timeline().get(id=data['itemId']).execute()
attachments = item.get('attachments', [])
media = None
if attachments:
# Get the first attachment on that timeline item and do stuff with it.
attachment = self.mirror_service.timeline().attachments().get(
itemId=data['itemId'],
attachmentId=attachments[0]['id']).execute()
resp, content = self.mirror_service._http.request(
attachment['contentUrl'])
if resp.status == 200:
media = MediaIoBaseUpload(
io.BytesIO(content), attachment['contentType'],
resumable=True)
else:
logging.info('Unable to retrieve attachment: %s', resp.status)
body = {
'text': 'Echoing your shared item: %s' % item.get('text', ''),
'notification': {'level': 'DEFAULT'}
}
self.mirror_service.timeline().insert(
body=body, media_body=media).execute()
# Only handle the first successful action.
break
else:
logging.info(
"I don't know what to do with this notification: %s", user_action)
NOTIFY_ROUTES = [
('/notify', NotifyHandler)
]