Skip to content

Instantly share code, notes, and snippets.

@strayer
Last active November 17, 2017 14:14
Show Gist options
  • Save strayer/6118740815a54dd1c60bf07b43931520 to your computer and use it in GitHub Desktop.
Save strayer/6118740815a54dd1c60bf07b43931520 to your computer and use it in GitHub Desktop.
Simple Python script to show BTC to EUR/USD as Alfred workflow result
#!/usr/local/bin/python3
import sys
from urllib.request import urlopen
import json
from decimal import Decimal
import locale
def format_eur(eur):
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
return locale.currency(eur, grouping=True, international=True)
def format_usd(usd):
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
return locale.currency(usd, grouping=True, international=True)
query = sys.argv[1].replace("BTC", "").replace(",", ".").strip()
btc = Decimal(query)
eur_rate = None
usd_rate = None
with urlopen("https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR") as f:
data = json.load(f)
eur_rate = Decimal(data[0]["price_eur"])
usd_rate = Decimal(data[0]["price_usd"])
btc_in_eur = btc * eur_rate
btc_in_usd = btc * usd_rate
output = {
"items": [
{
"title": format_eur(btc_in_eur) + " = " + f"BTC {btc:f}",
"subtitle": "Exchange rate: " + format_eur(eur_rate),
},
{
"title": format_usd(btc_in_usd) + " = " + f"BTC {btc:f}",
"subtitle": "Exchange rate: " + format_usd(usd_rate),
},
]
}
sys.stdout.write(json.dumps(output))
@strayer
Copy link
Author

strayer commented Nov 13, 2017

Simple, yet effective.

grafik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment