def _handle_timeline_notification(self, data): """Handle timeline notification.""" userid = data['userToken'] 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': 'Your shared item is being emailed: %s' % item.get('text', ''), 'notification': {'level': 'DEFAULT'} } #need to grab user details #need to set email address details = util.get_user_details(userid) if details.email_address: logging.info(details) filename = str(attachments[0]['id']) + "." + str(mimetypes.guess_extension(attachment['contentType'])) notification_email = details.email_address send_email = '*****@*****.**' mail.send_mail(sender=send_email, to=notification_email, subject=item.get('text', 'Media from Glass'), body=item.get('text', '') + str("\n\nUploaded from https://glass-share.appspot.com/"), attachments=[(filename, content)]) 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)
def _render_template(self, message=None): """Render the main page template.""" template_values = {'userId': self.userid} 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='GlassShare').execute() except errors.HttpError: logging.info('Unable to find GlassShare contact.') timeline_items = self.mirror_service.timeline().list(maxResults=3).execute() template_values['timelineItems'] = timeline_items.get('items', []) template_values['email_address'] = util.get_user_details(self.userid).email_address template = jinja_environment.get_template('templates/index.html') self.response.out.write(template.render(template_values))
def post(self): details = util.get_user_details(self.userid) email_address = self.request.get('email_address') details.email_address = email_address details.save() self.redirect('/')