-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbtc2fiat.py
executable file
·70 lines (54 loc) · 2.16 KB
/
btc2fiat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import abc
import sys
import traceback
import click
from requests import Request, Session
class Exchange(abc.ABC):
@abc.abstractmethod
def get_url(self, fiat_symbol):
pass
def build_request(self, fiat_symbol):
return Request('GET', self.get_url(fiat_symbol))
@abc.abstractmethod
def get_value(self, response, fiat_symbol):
pass
class Binance(Exchange):
def get_url(self, fiat_symbol):
shitcoin_symbol = (fiat_symbol + 'T') if fiat_symbol == 'USD' else fiat_symbol
return f'https://api.binance.com/api/v3/avgPrice?symbol=BTC{shitcoin_symbol}'
def get_value(self, response, fiat_symbol):
return float(response.json()['price'])
class Coinbase(Exchange):
def get_url(self, fiat_symbol):
return f"https://api.coinbase.com/v2/prices/spot?currency={fiat_symbol}"
def get_value(self, response, fiat_symbol):
return float(response.json()['data']['amount'])
class Kraken(Exchange):
def get_url(self, fiat_symbol):
return f'https://api.kraken.com/0/public/Ticker?pair=XBT{fiat_symbol}'
def get_value(self, response, fiat_symbol):
return float(response.json()['result'][f'XXBTZ{fiat_symbol}']['c'][0])
EXCHANGES = {'binance': Binance, 'coinbase': Coinbase, 'kraken': Kraken}
def get_value(exchange=None, fiat_symbol=None):
if exchange is None:
exchange = 'kraken'
if fiat_symbol is None:
fiat_symbol = 'USD'
exchange = EXCHANGES[exchange]()
session = Session()
request = exchange.build_request(fiat_symbol)
prepared_request = session.prepare_request(request)
response = session.send(prepared_request)
return exchange.get_value(response, fiat_symbol)
@click.command()
@click.option('--exchange', type=click.Choice(EXCHANGES.keys(), case_sensitive=False), default='kraken')
@click.option('--fiat-symbol', default='USD')
@click.option('--debug', is_flag=True, default=False)
def get_value_command(exchange, fiat_symbol, debug):
try:
print(get_value(exchange, fiat_symbol))
except Exception as e:
if debug:
traceback.print_exception(type(e), e, e.__traceback__)
sys.exit(1)