<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import io <EOL> import jinja2 <EOL> import logging <EOL> import os <EOL> import webapp2 <EOL> from google . appengine . api import memcache <EOL> from google . appengine . api import urlfetch <EOL> import httplib2 <EOL> from apiclient import errors <EOL> from apiclient . http import MediaIoBaseUpload <EOL> from apiclient . http import BatchHttpRequest <EOL> from oauth2client . appengine import StorageByKeyName <EOL> from model import Credentials <EOL> import util <EOL> jinja_environment = jinja2 . Environment ( <EOL> loader = jinja2 . FileSystemLoader ( os . path . dirname ( __file__ ) ) ) <EOL> PAGINATED_HTML = """<STR_LIT>""" <EOL> class _BatchCallback ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . success = <NUM_LIT:0> <EOL> self . failure = <NUM_LIT:0> <EOL> def callback ( self , request_id , response , exception ) : <EOL> """<STR_LIT>""" <EOL> if exception is None : <EOL> self . success += <NUM_LIT:1> <EOL> else : <EOL> self . failure += <NUM_LIT:1> <EOL> logging . error ( <EOL> '<STR_LIT>' , request_id , exception ) <EOL> class MainHandler ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def _render_template ( self , message = None ) : <EOL> """<STR_LIT>""" <EOL> template_values = { '<STR_LIT>' : self . userid } <EOL> if message : <EOL> template_values [ '<STR_LIT:message>' ] = message <EOL> try : <EOL> template_values [ '<STR_LIT>' ] = self . mirror_service . contacts ( ) . get ( <EOL> id = '<STR_LIT>' ) . execute ( ) <EOL> except errors . HttpError : <EOL> logging . info ( '<STR_LIT>' ) <EOL> timeline_items = self . mirror_service . timeline ( ) . list ( maxResults = <NUM_LIT:3> ) . execute ( ) <EOL> template_values [ '<STR_LIT>' ] = timeline_items . get ( '<STR_LIT>' , [ ] ) <EOL> subscriptions = self . mirror_service . subscriptions ( ) . list ( ) . execute ( ) <EOL> for subscription in subscriptions . get ( '<STR_LIT>' , [ ] ) : <EOL> collection = subscription . get ( '<STR_LIT>' ) <EOL> if collection == '<STR_LIT>' : <EOL> template_values [ '<STR_LIT>' ] = True <EOL> elif collection == '<STR_LIT>' : <EOL> template_values [ '<STR_LIT>' ] = True <EOL> template = jinja_environment . get_template ( '<STR_LIT>' ) <EOL> self . response . out . write ( template . render ( template_values ) ) <EOL> @ util . auth_required <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> message = memcache . get ( key = self . userid ) <EOL> memcache . delete ( key = self . userid ) <EOL> self . _render_template ( message ) <EOL> @ util . auth_required <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> operation = self . request . get ( '<STR_LIT>' ) <EOL> operations = { <EOL> '<STR_LIT>' : self . _insert_subscription , <EOL> '<STR_LIT>' : self . _delete_subscription , <EOL> '<STR_LIT>' : self . _insert_item , <EOL> '<STR_LIT>' : self . _insert_paginated_item , <EOL> '<STR_LIT>' : self . _insert_item_with_action , <EOL> '<STR_LIT>' : self . _insert_item_all_users , <EOL> '<STR_LIT>' : self . _insert_contact , <EOL> '<STR_LIT>' : self . _delete_contact , <EOL> '<STR_LIT>' : self . _delete_timeline_item <EOL> } <EOL> if operation in operations : <EOL> message = operations [ operation ] ( ) <EOL> else : <EOL> message = "<STR_LIT>" + operation <EOL> memcache . set ( key = self . userid , value = message , time = <NUM_LIT:5> ) <EOL> self . redirect ( '<STR_LIT:/>' ) <EOL> def _insert_subscription ( self ) : <EOL> """<STR_LIT>""" <EOL> body = { <EOL> '<STR_LIT>' : self . request . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . userid , <EOL> '<STR_LIT>' : util . get_full_url ( self , '<STR_LIT>' ) <EOL> } <EOL> self . mirror_service . subscriptions ( ) . insert ( body = body ) . execute ( ) <EOL> return '<STR_LIT>' <EOL> def _delete_subscription ( self ) : <EOL> """<STR_LIT>""" <EOL> collection = self . request . get ( '<STR_LIT>' ) <EOL> self . mirror_service . subscriptions ( ) . delete ( id = collection ) . execute ( ) <EOL> return '<STR_LIT>' <EOL> def _insert_item ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( '<STR_LIT>' ) <EOL> body = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } <EOL> } <EOL> if self . request . get ( '<STR_LIT:html>' ) == '<STR_LIT>' : <EOL> body [ '<STR_LIT:html>' ] = [ self . request . get ( '<STR_LIT:message>' ) ] <EOL> else : <EOL> body [ '<STR_LIT:text>' ] = self . request . get ( '<STR_LIT:message>' ) <EOL> media_link = self . request . get ( '<STR_LIT>' ) <EOL> if media_link : <EOL> if media_link . startswith ( '<STR_LIT:/>' ) : <EOL> media_link = util . get_full_url ( self , media_link ) <EOL> resp = urlfetch . fetch ( media_link , deadline = <NUM_LIT:20> ) <EOL> media = MediaIoBaseUpload ( <EOL> io . BytesIO ( resp . content ) , mimetype = '<STR_LIT>' , resumable = True ) <EOL> else : <EOL> media = None <EOL> self . mirror_service . timeline ( ) . insert ( body = body , media_body = media ) . execute ( ) <EOL> return '<STR_LIT>' <EOL> def _insert_paginated_item ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( '<STR_LIT>' ) <EOL> body = { <EOL> '<STR_LIT:html>' : PAGINATED_HTML , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ] <EOL> } <EOL> self . mirror_service . timeline ( ) . insert ( body = body ) . execute ( ) <EOL> return '<STR_LIT>' <EOL> def _insert_item_with_action ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( '<STR_LIT>' ) <EOL> body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : [ { '<STR_LIT:action>' : '<STR_LIT>' } ] <EOL> } <EOL> self . mirror_service . timeline ( ) . insert ( body = body ) . execute ( ) <EOL> return '<STR_LIT>' <EOL> def _insert_item_all_users ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( '<STR_LIT>' ) <EOL> users = Credentials . all ( ) <EOL> total_users = users . count ( ) <EOL> if total_users > <NUM_LIT:10> : <EOL> return '<STR_LIT>' % ( <EOL> total_users ) <EOL> body = { <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } <EOL> } <EOL> batch_responses = _BatchCallback ( ) <EOL> batch = BatchHttpRequest ( callback = batch_responses . callback ) <EOL> for user in users : <EOL> creds = StorageByKeyName ( <EOL> Credentials , user . key ( ) . name ( ) , '<STR_LIT>' ) . get ( ) <EOL> mirror_service = util . create_service ( '<STR_LIT>' , '<STR_LIT>' , creds ) <EOL> batch . add ( <EOL> mirror_service . timeline ( ) . insert ( body = body ) , <EOL> request_id = user . key ( ) . name ( ) ) <EOL> batch . execute ( httplib2 . Http ( ) ) <EOL> return '<STR_LIT>' % ( <EOL> batch_responses . success , batch_responses . failure ) <EOL> def _insert_contact ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( '<STR_LIT>' ) <EOL> id = self . request . get ( '<STR_LIT:id>' ) <EOL> name = self . request . get ( '<STR_LIT:name>' ) <EOL> image_url = self . request . get ( '<STR_LIT>' ) <EOL> if not name or not image_url : <EOL> return '<STR_LIT>' <EOL> else : <EOL> if image_url . startswith ( '<STR_LIT:/>' ) : <EOL> image_url = util . get_full_url ( self , image_url ) <EOL> body = { <EOL> '<STR_LIT:id>' : id , <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : [ image_url ] , <EOL> '<STR_LIT>' : [ { '<STR_LIT:type>' : '<STR_LIT>' } ] <EOL> } <EOL> self . mirror_service . contacts ( ) . insert ( body = body ) . execute ( ) <EOL> return '<STR_LIT>' + name <EOL> def _delete_contact ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mirror_service . contacts ( ) . delete ( <EOL> id = self . request . get ( '<STR_LIT:id>' ) ) . execute ( ) <EOL> return '<STR_LIT>' <EOL> def _delete_timeline_item ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( '<STR_LIT>' ) <EOL> self . mirror_service . timeline ( ) . delete ( id = self . request . get ( '<STR_LIT>' ) ) . execute ( ) <EOL> return '<STR_LIT>' <EOL> MAIN_ROUTES = [ <EOL> ( '<STR_LIT:/>' , MainHandler ) <EOL> ] </s>
