dotfiles/config/polybar/scripts/btc (view raw)
| 1 | #!/usr/bin/python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | import json,sys,urllib.request,time |
| 5 | |
| 6 | if len(sys.argv) != 3: |
| 7 | print("Usage: btc usd,eur,btc nok") |
| 8 | sys.exit() |
| 9 | |
| 10 | currencies = sys.argv[1] |
| 11 | basecurrency = sys.argv[2] |
| 12 | |
| 13 | currencyurl = "http://freecurrencyrates.com/api/action.php?do=cvals&iso=" + currencies.replace(',','') + "&f=" + basecurrency + "&v=1&s=cbr" |
| 14 | f = urllib.request.urlopen(currencyurl) |
| 15 | obj = json.loads(f.read()) |
| 16 | res=""; |
| 17 | for c in currencies.split(','): |
| 18 | res += c.upper() + ":{:,.2f}".format(1/obj[c.upper()]).replace(',',' ') |
| 19 | |
| 20 | # some unicode currency code replacement (optional) |
| 21 | res = res.replace("USD", " $") |
| 22 | res = res.replace("EUR", " €") |
| 23 | res = res.replace("BTC", " ") |
| 24 | |
| 25 | print(res); |