Skip to content

Commit

Permalink
url handling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pmoulton committed Oct 19, 2013
1 parent 58af78c commit 7ace069
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
80 changes: 79 additions & 1 deletion shard-glass-files/main_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))


PAGINATED_HTML = """
<article class='auto-paginate'>
<h2 class='blue text-large'>Did you know...?</h2>
<p>Cats are <em class='yellow'>solar-powered.</em> The time they spend
napping in direct sunlight is necessary to regenerate their internal
batteries. Cats that do not receive sufficient charge may exhibit the
following symptoms: lethargy, irritability, and disdainful glares. Cats
will reactivate on their own automatically after a complete charge
cycle; it is recommended that they be left undisturbed during this
process to maximize your enjoyment of your cat.</p><br/><p>
For more cat maintenance tips, tap to view the website!</p>
</article>
"""


class _BatchCallback(object):
"""Class used to track batch request responses."""

Expand Down Expand Up @@ -107,9 +122,13 @@ def post(self):
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,
'deleteTimelineItem': self._delete_timeline_item
}
if operation in operations:
message = operations[operation]()
Expand Down Expand Up @@ -162,6 +181,65 @@ def _insert_item(self):
self.mirror_service.timeline().insert(body=body, media_body=media).execute()
return 'A timeline item has been inserted.'

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 shard Contact."""
logging.info('Inserting contact')
Expand Down
2 changes: 1 addition & 1 deletion shard-glass-files/notify/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _handle_timeline_notification(self, data):
attachments = self.mirror_service.timeline().get(id=item_id).execute().get('attachments')
logging.info(str(attachments))
url = attachments[0].get('contentUrl')
new_url = 'url = %s' % url
new_url = 'url = "%s"' % url
url = new_url
logging.info("URL?")
logging.info(url)
Expand Down

0 comments on commit 7ace069

Please sign in to comment.