<s> '''<STR_LIT>''' <EOL> import urllib <EOL> import urllib2 <EOL> class Instapaper : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user , password , https = True ) : <EOL> self . user = user <EOL> self . password = password <EOL> if https : <EOL> self . authurl = "<STR_LIT>" <EOL> self . addurl = "<STR_LIT>" <EOL> else : <EOL> self . authurl = "<STR_LIT>" <EOL> self . addurl = "<STR_LIT>" <EOL> self . add_status_codes = { <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" <EOL> } <EOL> self . auth_status_codes = { <EOL> <NUM_LIT:200> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" <EOL> } <EOL> def add_item ( self , url , title = None , selection = None , <EOL> jsonp = None , redirect = None , response_info = False ) : <EOL> """<STR_LIT>""" <EOL> parameters = { <EOL> '<STR_LIT:username>' : self . user , <EOL> '<STR_LIT:password>' : self . password , <EOL> '<STR_LIT:url>' : url , <EOL> } <EOL> if title is not None : <EOL> parameters [ '<STR_LIT:title>' ] = title <EOL> else : <EOL> parameters [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> if selection is not None : <EOL> parameters [ '<STR_LIT>' ] = selection <EOL> if redirect is not None : <EOL> parameters [ '<STR_LIT>' ] = redirect <EOL> if jsonp is not None : <EOL> parameters [ '<STR_LIT>' ] = jsonp <EOL> status , headers = self . _query ( self . addurl , parameters ) <EOL> if jsonp is not None : <EOL> return status <EOL> statustxt = self . add_status_codes [ int ( status ) ] <EOL> if response_info : <EOL> return ( int ( status ) , statustxt , headers [ '<STR_LIT:title>' ] , headers [ '<STR_LIT:location>' ] ) <EOL> else : <EOL> return ( int ( status ) , statustxt ) <EOL> def auth ( self , user = None , password = None , jsonp = None ) : <EOL> """<STR_LIT>""" <EOL> if not user : <EOL> user = self . user <EOL> if not password : <EOL> password = self . password <EOL> parameters = { <EOL> '<STR_LIT:username>' : self . user , <EOL> '<STR_LIT:password>' : self . password <EOL> } <EOL> if jsonp is not None : <EOL> parameters [ '<STR_LIT>' ] = jsonp <EOL> status , headers = self . _query ( self . authurl , parameters ) <EOL> if jsonp is not None : <EOL> return status <EOL> return ( int ( status ) , self . auth_status_codes [ int ( status ) ] ) <EOL> def _query ( self , url = None , params = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if url is None : <EOL> raise NoUrlError ( "<STR_LIT>" ) <EOL> headers = { '<STR_LIT:location>' : None , '<STR_LIT:title>' : None } <EOL> headerdata = urllib . urlencode ( params ) <EOL> try : <EOL> request = urllib2 . Request ( url , headerdata ) <EOL> response = urllib2 . urlopen ( request ) <EOL> if '<STR_LIT>' in params : <EOL> status = response . read ( ) <EOL> else : <EOL> status = response . getcode ( ) <EOL> info = response . info ( ) <EOL> try : <EOL> headers [ '<STR_LIT:location>' ] = info [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> headers [ '<STR_LIT:title>' ] = info [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> return ( status , headers ) <EOL> except urllib2 . HTTPError as exception : <EOL> if '<STR_LIT>' in params : <EOL> return ( '<STR_LIT>' % ( params [ '<STR_LIT>' ] , exception . code ) , headers ) <EOL> else : <EOL> return ( exception . code , headers ) <EOL> except IOError as exception : <EOL> return ( exception . code , headers ) <EOL> class NoUrlError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , arg ) : <EOL> self . arg = arg <EOL> def __str__ ( self ) : <EOL> return repr ( self . arg ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
