67 lines
1.5 KiB
Python
Executable file
67 lines
1.5 KiB
Python
Executable file
#!/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 to display
|
|
"""
|
|
|
|
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:
|
|
"""Send a request to the API with the given arguments."""
|
|
|
|
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={}&timePeriod=30d".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"])
|
|
history = coin_data["history"]
|
|
print(history)
|
|
|
|
output = "{} = {}{}".format(coin, base_sign, round(price, round_nb))
|
|
|
|
return output
|
|
|
|
|
|
print(compute(parse_arguments()))
|