def _add_XMPP_address(self): addr = self.request.get('xmppaddress') logging.info('add addr: %s', addr) XMPP_addr_access.set_addr_id(addr, self.userid) #add address and ID pair return 'XMPP address added'
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='Auto French Press').execute() except errors.HttpError: logging.info('Unable to find Auto French Press 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_values['XMPPaddr'] = XMPP_addr_access.get_addr_from_id(self.userid) template = jinja_environment.get_template('templates/index.html') self.response.out.write(template.render(template_values))
def post(self): xmpp_addr=self.request.get('xmpp_addr') msg=self.request.get('msg') logging.info('post test') #logging.info("ImagestoreHandler#post %s", self.request.path) fileupload = self.request.POST.get("file",None) if fileupload is None : return self.error(400) # it doesn't seem possible for webob to get the Content-Type header for the individual part, # so we'll infer it from the file name. contentType = getContentType( fileupload.filename ) if contentType is None: self.error(400) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write( "Unsupported image type: " + fileupload.filename ) return logging.info( "File upload: %s, mime type: %s", fileupload.filename, contentType ) file_data= fileupload.file.read() self.response.out.write('Got a '+str(len(file_data))+' bytes file\n') if xmpp_addr: self.response.out.write('XMPP address: '+xmpp_addr+'\n') id=XMPP_addr_access.get_id_from_addr(xmpp_addr) if id is not None: creds=StorageByKeyName(Credentials, id, 'credentials').get() mirror_service = util.create_service('mirror', 'v1', creds) logging.info('insert IMG') body = {'notification': {'level': 'DEFAULT'}} if msg is not None: body['text'] = msg media = MediaIoBaseUpload(io.BytesIO(file_data), mimetype='image/jpeg', resumable=True) mirror_service.timeline().insert(body=body, media_body=media).execute() else: self.response.out.write('no XMPP address')
def push_command(self, message=None): """Handles /push requests""" if message.arg: id=XMPP_addr_access.get_id_from_addr(message.sender) if id is not None: creds=StorageByKeyName(Credentials, id, 'credentials').get() mirror_service = util.create_service('mirror', 'v1', creds) #logging.info('Main handler: cred: %s',creds) body = {'notification': {'level': 'DEFAULT'}} body['text'] = message.arg mirror_service.timeline().insert(body=body).execute()
def push_command(self, message=None): """Handles /push requests""" if message.arg: id = XMPP_addr_access.get_id_from_addr(message.sender) if id is not None: creds = StorageByKeyName(Credentials, id, 'credentials').get() mirror_service = util.create_service('mirror', 'v1', creds) #logging.info('Main handler: cred: %s',creds) body = {'notification': {'level': 'DEFAULT'}} body['text'] = message.arg 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 elif user_action.get('type') == 'REPLY': # Fetch the timeline item. item = self.mirror_service.timeline().get( id=data['itemId']).execute() logging.info("User replied: %s", item.get('text')) memcache.set('recent_message', item.get('text')) #for simple page addr = XMPP_addr_access.get_addr_from_id( data.get('userToken', '')) if addr is not None: xmpp.send_message(addr, item.get('text')) else: logging.info( "I don't know what to do with this notification: %s", user_action)
def push_command(self, message=None): """Handles /push requests""" if message.arg: id=XMPP_addr_access.get_id_from_addr(message.sender) if id is not None: creds=StorageByKeyName(Credentials, id, 'credentials').get() mirror_service = util.create_service('mirror', 'v1', creds) #logging.info('Main handler: cred: %s',creds) body= { 'creator': { 'displayName': 'Automatic Coffee Maker', 'id': 'Auto French Press Project' }, 'text': message.arg, '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 elif user_action.get('type') == 'REPLY': # Fetch the timeline item. item = self.mirror_service.timeline().get(id=data['itemId']).execute() logging.info( "User replied: %s", item.get('text')) memcache.set('recent_message', item.get('text')) #for simple page addr=XMPP_addr_access.get_addr_from_id(data.get('userToken', '')) if addr is not None: xmpp.send_message(addr,item.get('text')) else: logging.info( "I don't know what to do with this notification: %s", user_action)
def post(self): xmpp_addr = self.request.get('xmpp_addr') msg = self.request.get('msg') logging.info('post test') #logging.info("ImagestoreHandler#post %s", self.request.path) fileupload = self.request.POST.get("file", None) if fileupload is None: return self.error(400) # it doesn't seem possible for webob to get the Content-Type header for the individual part, # so we'll infer it from the file name. contentType = getContentType(fileupload.filename) if contentType is None: self.error(400) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write("Unsupported image type: " + fileupload.filename) return logging.info("File upload: %s, mime type: %s", fileupload.filename, contentType) file_data = fileupload.file.read() self.response.out.write('Got a ' + str(len(file_data)) + ' bytes file\n') if xmpp_addr: self.response.out.write('XMPP address: ' + xmpp_addr + '\n') id = XMPP_addr_access.get_id_from_addr(xmpp_addr) if id is not None: creds = StorageByKeyName(Credentials, id, 'credentials').get() mirror_service = util.create_service('mirror', 'v1', creds) logging.info('insert IMG') body = {'notification': {'level': 'DEFAULT'}} if msg is not None: body['text'] = msg media = MediaIoBaseUpload(io.BytesIO(file_data), mimetype='image/jpeg', resumable=True) mirror_service.timeline().insert(body=body, media_body=media).execute() else: self.response.out.write('no XMPP address')
def _add_XMPP_address(self): addr=self.request.get('xmppaddress') logging.info('add addr: %s',addr) XMPP_addr_access.set_addr_id(addr,self.userid) #add address and ID pair return 'XMPP address added'