#!/usr/bin/env python3 """Simple script for i3blocks to fetch crypto values from coinranking API. Author: flyingscorpio """ import argparse import requests def parse_arguments() -> argparse.Namespace: """Arguments consist of: * a coin to look up * the currency to set the value on (defaults to USD) * the number of decimals """ parser = argparse.ArgumentParser() parser.add_argument("coin") parser.add_argument("-b", "--base", default="USD") parser.add_argument("-r", "--round", type=int, default=2) args = parser.parse_args() return args def compute(args: argparse.Namespace) -> str: """h""" symbols = { "GBP": "£", "EUR": "€", "USD": "$", "BTC": "", "LTC": "Ł", "ETH": "Ξ", } coin = args.coin.upper() base = args.base.upper() round_nb = args.round req = requests.get( "https://api.coinranking.com/v1/public/coins?base={}&symbols={}".format( base, coin ) ) try: coin = symbols[coin] except KeyError: pass req_j = req.json() data = req_j["data"] base_sign = data["base"]["sign"] coin_data = data["coins"][0] price = float(coin_data["price"]) output = "{} = {}{}".format(coin, base_sign, round(price, round_nb)) return output print(compute(parse_arguments()))