<s> """<STR_LIT>""" <EOL> import cgi <EOL> import hashlib <EOL> import time <EOL> import urllib <EOL> try : <EOL> import json <EOL> _parse_json = lambda s : json . loads ( s ) <EOL> except ImportError : <EOL> try : <EOL> import simplejson <EOL> _parse_json = lambda s : simplejson . loads ( s ) <EOL> except ImportError : <EOL> from django . utils import simplejson <EOL> _parse_json = lambda s : simplejson . loads ( s ) <EOL> class GraphAPI ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , access_token = None ) : <EOL> self . access_token = access_token <EOL> def get_object ( self , id , ** args ) : <EOL> """<STR_LIT>""" <EOL> return self . request ( id , args ) <EOL> def get_objects ( self , ids , ** args ) : <EOL> """<STR_LIT>""" <EOL> args [ "<STR_LIT>" ] = "<STR_LIT:U+002C>" . join ( ids ) <EOL> return self . request ( "<STR_LIT>" , args ) <EOL> def get_connections ( self , id , connection_name , ** args ) : <EOL> """<STR_LIT>""" <EOL> return self . request ( id + "<STR_LIT:/>" + connection_name , args ) <EOL> def put_object ( self , parent_object , connection_name , ** data ) : <EOL> """<STR_LIT>""" <EOL> assert self . access_token , "<STR_LIT>" <EOL> return self . request ( parent_object + "<STR_LIT:/>" + connection_name , post_args = data ) <EOL> def put_wall_post ( self , message , attachment = { } , profile_id = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return self . put_object ( profile_id , "<STR_LIT>" , message = message , ** attachment ) <EOL> def put_comment ( self , object_id , message ) : <EOL> """<STR_LIT>""" <EOL> return self . put_object ( object_id , "<STR_LIT>" , message = message ) <EOL> def put_like ( self , object_id ) : <EOL> """<STR_LIT>""" <EOL> return self . put_object ( object_id , "<STR_LIT>" ) <EOL> def delete_object ( self , id ) : <EOL> """<STR_LIT>""" <EOL> self . request ( id , post_args = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def request ( self , path , args = None , post_args = None ) : <EOL> """<STR_LIT>""" <EOL> if not args : args = { } <EOL> if self . access_token : <EOL> if post_args is not None : <EOL> post_args [ "<STR_LIT>" ] = self . access_token <EOL> else : <EOL> args [ "<STR_LIT>" ] = self . access_token <EOL> post_data = None if post_args is None else urllib . urlencode ( post_args ) <EOL> file = urllib . urlopen ( "<STR_LIT>" + path + "<STR_LIT:?>" + <EOL> urllib . urlencode ( args ) , post_data ) <EOL> try : <EOL> response = _parse_json ( file . read ( ) ) <EOL> finally : <EOL> file . close ( ) <EOL> if response . get ( "<STR_LIT:error>" ) : <EOL> raise GraphAPIError ( response [ "<STR_LIT:error>" ] [ "<STR_LIT:type>" ] , <EOL> response [ "<STR_LIT:error>" ] [ "<STR_LIT:message>" ] ) <EOL> return response <EOL> class GraphAPIError ( Exception ) : <EOL> def __init__ ( self , type , message ) : <EOL> Exception . __init__ ( self , message ) <EOL> self . type = type <EOL> def get_user_from_cookie ( cookies , app_id , app_secret ) : <EOL> """<STR_LIT>""" <EOL> cookie = cookies . get ( "<STR_LIT>" + app_id , "<STR_LIT>" ) <EOL> if not cookie : return None <EOL> args = dict ( ( k , v [ - <NUM_LIT:1> ] ) for k , v in cgi . parse_qs ( cookie . strip ( '<STR_LIT:">' ) ) . items ( ) ) <EOL> payload = "<STR_LIT>" . join ( k + "<STR_LIT:=>" + args [ k ] for k in sorted ( args . keys ( ) ) <EOL> if k != "<STR_LIT>" ) <EOL> sig = hashlib . md5 ( payload + app_secret ) . hexdigest ( ) <EOL> expires = int ( args [ "<STR_LIT>" ] ) <EOL> if sig == args . get ( "<STR_LIT>" ) and ( expires == <NUM_LIT:0> or time . time ( ) < expires ) : <EOL> return args <EOL> else : <EOL> return None </s>
