Created
December 28, 2017 15:04
-
-
Save BOSSoNe0013/b6a1a7087a24b57cf62d7f345bd156f0 to your computer and use it in GitHub Desktop.
Fetch user's trading balance and deposit address from HitBTC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import os | |
import requests | |
import argparse | |
import configparser | |
from art import * | |
argument_parser = argparse.ArgumentParser() | |
argument_parser.add_argument( | |
'--address', | |
'-a', | |
type=str, | |
default=None, | |
help='Get wallet deposit addres for currency') | |
argument_parser.add_argument('--setup', default=False, action='store_true') | |
flags = argument_parser.parse_args() | |
config_folder = os.path.join(os.path.expanduser("~"), '.config', 'trading_balance') | |
if not os.path.exists(config_folder): | |
os.makedirs(config_folder) | |
config_file = os.path.join(config_folder, 'trading_balance.conf') | |
config = configparser.ConfigParser() | |
config['DEFAULT'] = { | |
'public_key': '', | |
'secret': '' | |
} | |
public_key = '' | |
secret = '' | |
api_root = 'https://api.hitbtc.com/api/2/' | |
# bitcoin = u'\u20bf' | |
def get_address(currency): | |
tprint(currency) | |
try: | |
r = session.get('%saccount/crypto/address/%s' % (api_root, currency)) | |
item = r.json() | |
print('Address: %s' % item['address']) | |
except Exception as error: | |
print('Can\'t get address from HitBTC') | |
print(error) | |
print() | |
def get_balance(): | |
tprint('Trading Balance') | |
try: | |
r = session.get('%strading/balance' % api_root) | |
btc = 0.0 | |
total_btc = 0.0 | |
for o in r.json(): | |
if float(o['available']) > 0: | |
currency = o['currency'] | |
balance = float(o['available']) | |
if currency != 'BTC': | |
symbol = '%sBTC' % currency | |
r2 = session.get('%spublic/trades/%s' % (api_root, symbol)) | |
price = 1.0 | |
for item in r2.json(): | |
if item['side'] == 'sell': | |
price = float(item['price']) | |
break | |
print('%s: %.9f (%.9fɃ)' % (currency, balance, price*balance)) | |
total_btc += price*balance | |
else: | |
btc = balance | |
total_btc += btc | |
print('BTC: %.9f' % btc) | |
print('==========================================') | |
r3 = session.get('%spublic/trades/BTCUSD' % api_root) | |
btc_usd = 1.0 | |
for item in r3.json(): | |
if item['side'] == 'sell': | |
btc_usd = float(item['price']) | |
break | |
print('Total trading balance: %.9fɃ (%.4f$)' % (total_btc, total_btc * btc_usd)) | |
except Exception as error: | |
print('Can\'t get balance from HitBTC') | |
print(error) | |
def setup(): | |
global config, public_key, secret | |
config['DEFAULT']['public_key'] = public_key = input('Public key: ') | |
config['DEFAULT']['secret'] = secret = input('Secret: ') | |
with open(config_file, 'w') as config_file: | |
config.write(config_file) | |
if __name__ == "__main__": | |
config.read(config_file) | |
public_key = config['DEFAULT']['public_key'] | |
secret = config['DEFAULT']['secret'] | |
if public_key == '': | |
print('First launch, starting configuration') | |
setup() | |
elif flags.setup: | |
setup() | |
session = requests.session() | |
session.auth = (public_key, secret) | |
if flags.address is not None: | |
get_address(flags.address) | |
else: | |
get_balance() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment