<s> import hmac <EOL> import hashlib <EOL> import time <EOL> import urllib <EOL> import urllib2 <EOL> import json <EOL> class API ( object ) : <EOL> __username = '<STR_LIT>' <EOL> __api_key = '<STR_LIT>' <EOL> __api_secret = '<STR_LIT>' <EOL> __nonce_v = '<STR_LIT>' <EOL> def __init__ ( self , username , api_key , api_secret ) : <EOL> self . __username = username <EOL> self . __api_key = api_key <EOL> self . __api_secret = api_secret <EOL> def __nonce ( self ) : <EOL> self . __nonce_v = '<STR_LIT>' . format ( time . time ( ) * <NUM_LIT:1000> ) . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> def __signature ( self ) : <EOL> string = self . __nonce_v + self . __username + self . __api_key <EOL> signature = hmac . new ( self . __api_secret , string , digestmod = hashlib . sha256 ) . hexdigest ( ) . upper ( ) <EOL> return signature <EOL> def __post ( self , url , param ) : <EOL> params = urllib . urlencode ( param ) <EOL> req = urllib2 . Request ( url , params , { '<STR_LIT>' : '<STR_LIT>' + self . __username } ) <EOL> page = urllib2 . urlopen ( req ) . read ( ) <EOL> return page <EOL> def api_call ( self , method , param = { } , private = <NUM_LIT:0> , couple = '<STR_LIT>' ) : <EOL> url = '<STR_LIT>' + method + '<STR_LIT:/>' <EOL> if couple != '<STR_LIT>' : <EOL> url = url + couple + '<STR_LIT:/>' <EOL> if private == <NUM_LIT:1> : <EOL> self . __nonce ( ) <EOL> param . update ( { <EOL> '<STR_LIT:key>' : self . __api_key , <EOL> '<STR_LIT>' : self . __signature ( ) , <EOL> '<STR_LIT>' : self . __nonce_v } ) <EOL> answer = self . __post ( url , param ) <EOL> return json . loads ( answer ) <EOL> def ticker ( self , couple = '<STR_LIT>' ) : <EOL> return self . api_call ( '<STR_LIT>' , { } , <NUM_LIT:0> , couple ) <EOL> def order_book ( self , couple = '<STR_LIT>' ) : <EOL> return self . api_call ( '<STR_LIT>' , { } , <NUM_LIT:0> , couple ) <EOL> def trade_history ( self , since = <NUM_LIT:1> , couple = '<STR_LIT>' ) : <EOL> return self . api_call ( '<STR_LIT>' , { "<STR_LIT>" : str ( since ) } , <NUM_LIT:0> , couple ) <EOL> def balance ( self ) : <EOL> return self . api_call ( '<STR_LIT>' , { } , <NUM_LIT:1> ) <EOL> def current_orders ( self , couple = '<STR_LIT>' ) : <EOL> return self . api_call ( '<STR_LIT>' , { } , <NUM_LIT:1> , couple ) <EOL> def cancel_order ( self , order_id ) : <EOL> return self . api_call ( '<STR_LIT>' , { "<STR_LIT:id>" : order_id } , <NUM_LIT:1> ) <EOL> def place_order ( self , ptype = '<STR_LIT>' , amount = <NUM_LIT:1> , price = <NUM_LIT:1> , couple = '<STR_LIT>' ) : <EOL> return self . api_call ( '<STR_LIT>' , { "<STR_LIT:type>" : ptype , "<STR_LIT>" : str ( amount ) , "<STR_LIT>" : str ( price ) } , <NUM_LIT:1> , couple ) <EOL> def price_stats ( self , last_hours , max_resp_arr_size , couple = '<STR_LIT>' ) : <EOL> return self . api_call ( <EOL> '<STR_LIT>' , <EOL> { "<STR_LIT>" : last_hours , "<STR_LIT>" : max_resp_arr_size } , <EOL> <NUM_LIT:0> , couple ) </s>
