<s> import os . path <EOL> import tornado . auth <EOL> import tornado . escape <EOL> import tornado . httpserver <EOL> import tornado . ioloop <EOL> import tornado . options <EOL> import tornado . web <EOL> from tornado . options import define , options <EOL> define ( "<STR_LIT:port>" , default = <NUM_LIT> , help = "<STR_LIT>" , type = int ) <EOL> define ( "<STR_LIT>" , help = "<STR_LIT>" , type = str ) <EOL> define ( "<STR_LIT>" , help = "<STR_LIT>" , type = str ) <EOL> class Application ( tornado . web . Application ) : <EOL> def __init__ ( self ) : <EOL> handlers = [ <EOL> ( r"<STR_LIT:/>" , MainHandler ) , <EOL> ( r"<STR_LIT>" , AuthLoginHandler ) , <EOL> ( r"<STR_LIT>" , AuthLogoutHandler ) , <EOL> ] <EOL> settings = dict ( <EOL> cookie_secret = "<STR_LIT>" , <EOL> login_url = "<STR_LIT>" , <EOL> template_path = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" ) , <EOL> static_path = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" ) , <EOL> xsrf_cookies = True , <EOL> facebook_api_key = options . facebook_api_key , <EOL> facebook_secret = options . facebook_secret , <EOL> ui_modules = { "<STR_LIT>" : PostModule } , <EOL> debug = True , <EOL> autoescape = None , <EOL> ) <EOL> tornado . web . Application . __init__ ( self , handlers , ** settings ) <EOL> class BaseHandler ( tornado . web . RequestHandler ) : <EOL> def get_current_user ( self ) : <EOL> user_json = self . get_secure_cookie ( "<STR_LIT>" ) <EOL> if not user_json : return None <EOL> return tornado . escape . json_decode ( user_json ) <EOL> class MainHandler ( BaseHandler , tornado . auth . FacebookGraphMixin ) : <EOL> @ tornado . web . authenticated <EOL> @ tornado . web . asynchronous <EOL> def get ( self ) : <EOL> self . facebook_request ( "<STR_LIT>" , self . _on_stream , <EOL> access_token = self . current_user [ "<STR_LIT>" ] ) <EOL> def _on_stream ( self , stream ) : <EOL> if stream is None : <EOL> self . redirect ( "<STR_LIT>" ) <EOL> return <EOL> self . render ( "<STR_LIT>" , stream = stream ) <EOL> class AuthLoginHandler ( BaseHandler , tornado . auth . FacebookGraphMixin ) : <EOL> @ tornado . web . asynchronous <EOL> def get ( self ) : <EOL> my_url = ( self . request . protocol + "<STR_LIT>" + self . request . host + <EOL> "<STR_LIT>" + <EOL> tornado . escape . url_escape ( self . get_argument ( "<STR_LIT>" , "<STR_LIT:/>" ) ) ) <EOL> if self . get_argument ( "<STR_LIT:code>" , False ) : <EOL> self . get_authenticated_user ( <EOL> redirect_uri = my_url , <EOL> client_id = self . settings [ "<STR_LIT>" ] , <EOL> client_secret = self . settings [ "<STR_LIT>" ] , <EOL> code = self . get_argument ( "<STR_LIT:code>" ) , <EOL> callback = self . _on_auth ) <EOL> return <EOL> self . authorize_redirect ( redirect_uri = my_url , <EOL> client_id = self . settings [ "<STR_LIT>" ] , <EOL> extra_params = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def _on_auth ( self , user ) : <EOL> if not user : <EOL> raise tornado . web . HTTPError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> self . set_secure_cookie ( "<STR_LIT>" , tornado . escape . json_encode ( user ) ) <EOL> self . redirect ( self . get_argument ( "<STR_LIT>" , "<STR_LIT:/>" ) ) <EOL> class AuthLogoutHandler ( BaseHandler , tornado . auth . FacebookGraphMixin ) : <EOL> def get ( self ) : <EOL> self . clear_cookie ( "<STR_LIT>" ) <EOL> self . redirect ( self . get_argument ( "<STR_LIT>" , "<STR_LIT:/>" ) ) <EOL> class PostModule ( tornado . web . UIModule ) : <EOL> def render ( self , post ) : <EOL> return self . render_string ( "<STR_LIT>" , post = post ) <EOL> def main ( ) : <EOL> tornado . options . parse_command_line ( ) <EOL> if not ( options . facebook_api_key and options . facebook_secret ) : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> http_server = tornado . httpserver . HTTPServer ( Application ( ) ) <EOL> http_server . listen ( options . port ) <EOL> tornado . ioloop . IOLoop . current ( ) . start ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
