<s> import json <EOL> import requests <EOL> import urllib <EOL> class Robinhood : <EOL> endpoints = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:user>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> session = None <EOL> username = None <EOL> password = None <EOL> headers = None <EOL> auth_token = None <EOL> def __init__ ( self ) : <EOL> self . session = requests . session ( ) <EOL> self . session . proxies = urllib . getproxies ( ) <EOL> self . headers = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:Content-Type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> self . session . headers = self . headers <EOL> def login ( self , username , password ) : <EOL> self . username = username <EOL> self . password = password <EOL> data = "<STR_LIT>" % ( self . password , self . username ) <EOL> res = self . session . post ( self . endpoints [ '<STR_LIT>' ] , data = data ) <EOL> res = res . json ( ) <EOL> try : <EOL> self . auth_token = res [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> return False <EOL> self . headers [ '<STR_LIT>' ] = '<STR_LIT>' + self . auth_token <EOL> return True <EOL> def investment_profile ( self ) : <EOL> self . session . get ( self . endpoints [ '<STR_LIT>' ] ) <EOL> def instruments ( self , stock = None ) : <EOL> res = self . session . get ( self . endpoints [ '<STR_LIT>' ] , params = { '<STR_LIT>' : stock . upper ( ) } ) <EOL> res = res . json ( ) <EOL> return res [ '<STR_LIT>' ] <EOL> def quote_data ( self , stock = None ) : <EOL> if stock is None : <EOL> stock = raw_input ( "<STR_LIT>" ) ; <EOL> url = str ( self . endpoints [ '<STR_LIT>' ] ) + str ( stock ) + "<STR_LIT:/>" <EOL> try : <EOL> res = json . loads ( ( urllib . urlopen ( url ) ) . read ( ) ) ; <EOL> if len ( res ) > <NUM_LIT:0> : <EOL> return res ; <EOL> else : <EOL> raise NameError ( "<STR_LIT>" + stock ) ; <EOL> except ( ValueError ) : <EOL> raise NameError ( "<STR_LIT>" + stock ) ; <EOL> def get_quote ( self , stock = None ) : <EOL> data = self . quote_data ( stock ) <EOL> return data [ "<STR_LIT>" ] <EOL> def print_quote ( self , stock = None ) : <EOL> data = self . quote_data ( stock ) <EOL> print ( data [ "<STR_LIT>" ] + "<STR_LIT>" + data [ "<STR_LIT>" ] ) ; <EOL> def print_quotes ( self , stocks ) : <EOL> for i in range ( len ( stocks ) ) : <EOL> self . print_quote ( stocks [ i ] ) ; <EOL> def ask_price ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def ask_size ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def bid_price ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def bid_size ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def last_trade_price ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def last_trade_price ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def previous_close ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def previous_close_date ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def adjusted_previous_close ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def symbol ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def last_updated_at ( self , stock = None ) : <EOL> return self . quote_data ( stock ) [ '<STR_LIT>' ] ; <EOL> def get_account ( self ) : <EOL> res = self . session . get ( self . endpoints [ '<STR_LIT>' ] ) <EOL> res = res . json ( ) <EOL> return res [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> def place_order ( self , instrument , quantity = <NUM_LIT:1> , bid_price = None , transaction = None ) : <EOL> if bid_price == None : <EOL> bid_price = self . quote_data ( instrument [ '<STR_LIT>' ] ) [ '<STR_LIT>' ] <EOL> data = '<STR_LIT>' % ( <EOL> self . get_account ( ) [ '<STR_LIT:url>' ] , <EOL> urllib . unquote ( instrument [ '<STR_LIT:url>' ] ) , <EOL> float ( bid_price ) , <EOL> quantity , <EOL> transaction , <EOL> instrument [ '<STR_LIT>' ] <EOL> ) <EOL> res = self . session . post ( self . endpoints [ '<STR_LIT>' ] , data = data ) <EOL> return res <EOL> def place_buy_order ( self , instrument , quantity , bid_price = None ) : <EOL> transaction = "<STR_LIT>" <EOL> return self . place_order ( instrument , quantity , bid_price , transaction ) <EOL> def place_sell_order ( self , instrument , quantity , bid_price = None ) : <EOL> transaction = "<STR_LIT>" <EOL> return self . place_order ( instrument , quantity , bid_price , transaction ) </s>
