summaryrefslogtreecommitdiff
path: root/.config/polybar/pcrypto.py
blob: 3d9548a2a35e530a365d9a53f0981c893b443a45 (plain)
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
#!/usr/bin/env python3.6

import requests
import argparse
import json
import sys
import os

parser = argparse.ArgumentParser(description="Display currencies on polybar")
parser.add_argument("--coins", type=str,
                    nargs="+", help="Select coins to display")
parser.add_argument("--base", type=str,
                    nargs="?", default="USD", help="Currency base to convert against")
parser.add_argument("--decimals", type=int,
                    nargs="?", default=2, help="How many decimals to show")
parser.add_argument("--display", type=str,
                    nargs="?", default="price", choices=["price", "percentage", "both"], help="Display mode")

args = parser.parse_args()
home = os.path.expanduser("~/")

unicode_dict = {}
with open(f"{home}.config/polybar/coins.svg", "r", encoding="utf-8") as icons:
    for line in icons:
        unicode, coin = line.strip().split(":")
        unicode_dict[unicode] = coin


for coin in args.coins:
    get = requests.get(
        f"https://api.coinranking.com/v1/public/coins?prefix={coin}&base={args.base}").json()["data"]
    price_float = round(float(get["coins"][0]["price"]), args.decimals)
    current_price = get["base"]["sign"] + str(price_float)
    change = get["coins"][0]["change"]

    for _unicode, _coin in unicode_dict.items():
        if _coin == coin:
            icon = chr(int(_unicode, 16)) if len(_unicode) > 1 else _unicode
            if args.display == "price":
                if change > 0:
                    sys.stdout.write("%{F#00BB00}₿: " + current_price + "%{F-}")
                else:
                    sys.stdout.write("%{F#BB0000}₿: " + current_price + "%{F-}")
            if args.display == "percentage":
                sys.stdout.write(f"  {icon} {change:+}% ")
            if args.display == "both":
                sys.stdout.write(f" {icon} {current_price} | {change:+}% ")