Last active
March 7, 2018 09:37
-
-
Save mesuutt/44414082236196cab377cdf811c11100 to your computer and use it in GitHub Desktop.
Show BTC,ETH and LTC exchange rates without leaving commandline.
This file contains hidden or 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
import re | |
import sys | |
import getopt | |
from datetime import datetime, timedelta | |
from decimal import Decimal | |
import requests | |
GREEN = '\033[0;32m' | |
RED = '\033[0;31m' | |
MAGENTA = '\033[0;35m' | |
NC = '\033[0m' | |
def get_response(url): | |
return requests.get(url) | |
def print_rates(from_currency, to_currency, price_type, compage_days, amount): | |
from_currency = from_currency | |
to_currency = to_currency | |
currency_pair = '{}-{}'.format(from_currency, to_currency) | |
price_url = 'https://api.coinbase.com/v2/prices/{}/{}'.format(currency_pair, price_type) | |
response = get_response('https://api.coinbase.com/v2/time') | |
if response.status_code != 200: | |
return | |
response = response.json() | |
server_time = datetime.fromtimestamp(response['data']['epoch']) | |
response = get_response(price_url).json() | |
current_amount = Decimal(response['data']['amount']) | |
print("{color}{from_amount} {from_currency} = {to_amount} {to_currency}{nc} \t({price_type})".format( | |
color=MAGENTA, | |
from_amount=amount, | |
to_amount=format(current_amount * amount, '.2f'), | |
from_currency=from_currency, | |
to_currency=to_currency, | |
nc=NC, | |
price_type="{} Price".format(price_type.capitalize()) | |
)) | |
if compage_days: | |
compare_date = server_time - timedelta(days=compage_days) | |
prev_rate_url = price_url + '?date={}'.format(compare_date.strftime('%Y-%m-%d')) | |
response = get_response(prev_rate_url).json() | |
prev_amount = Decimal(response['data']['amount']) | |
diff_amount = current_amount - prev_amount | |
print("{color}{arrow}{diff} {to_currency}{nc}".format( | |
color=GREEN if current_amount > prev_amount else RED, | |
diff=format(diff_amount * amount, '.2f'), | |
to_currency=to_currency, | |
arrow='↑' if current_amount > prev_amount else '↓', | |
nc=NC, | |
)) | |
def print_usage(): | |
print('{} -f <BTC|ETH|LTC> -t <currency> -p [spot|buy|sell] [-a AMOUNT] [-d DAYS]'.format(sys.argv[0])) | |
def main(argv): | |
from_currency = '' | |
to_currency = '' | |
amount = 1 | |
days = None | |
price_type = 'spot' | |
try: | |
opts, args = getopt.getopt(argv, "hf:t:d:a:p:", ['price=']) | |
except getopt.GetoptError: | |
print_usage() | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print_usage() | |
sys.exit() | |
elif opt == "-f": | |
from_currency = arg | |
elif opt == "-a": | |
amount = Decimal(arg) | |
elif opt == "-d": | |
days = int(arg) | |
elif opt == "-t": | |
to_currency = arg | |
elif opt in ['-p', '--price']: | |
price_type = arg | |
if not all([from_currency, to_currency]): | |
print_usage() | |
sys.exit(2) | |
if from_currency not in ['BTC', 'ETH', 'LTC']: | |
print('Supported currencies are BTC, ETH and LTC') | |
print_usage() | |
sys.exit(2) | |
if price_type not in ['spot', 'sell', 'buy']: | |
print("Unsupported price. Choices are 'spot', 'buy' and 'sell'. Default: 'spot'") | |
print_usage() | |
sys.exit(2) | |
print_rates(from_currency, to_currency, price_type, days, amount) | |
# show_rate() | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage examples: