Skip to content

Instantly share code, notes, and snippets.

@helb
Last active January 3, 2018 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helb/86a7bf0c328255ee9dba97890fd5da0e to your computer and use it in GitHub Desktop.
Save helb/86a7bf0c328255ee9dba97890fd5da0e to your computer and use it in GitHub Desktop.
Convert cryptocurrencies to butter
$ ./butter.py BTC ETH LTC XRP ORE BCN SC DOGE
BTC:   11112.06
ETH:   678.30
LTC:   178.65
XRP:   2.12
ORE:   10.80
BCN:   0.00
SC:    0.02
DOGE:  0.01
#!/usr/bin/env python
from bs4 import BeautifulSoup, element
import requests
import re
from sys import argv
from decimal import Decimal
butter_url = "https://www.akcniceny.cz/zbozi/mlecne-vyrobky/masla/"
bitgup_url = "https://bitgup.com/view_currency/"
fiat_url = "https://api.fixer.io/latest?symbols=CZK&base=USD"
butter_default_price = 30.00
def butter_price():
result = requests.get(butter_url)
html = BeautifulSoup(result.content, "html5lib")
if len(html.findAll("p", {"class": "warningfail"})) > 0:
return butter_default_price
else:
price = html.findAll("p", {"class": "cena"})[0]
price = "".join([t for t in price.contents if type(t) == element.NavigableString])
return float(price.replace(",", "."))
def usd_to_czk():
result = requests.get(fiat_url).json()
return float(result["rates"]["CZK"])
def crypto_price(code):
result = requests.get(f"{bitgup_url}/{code}/")
html = BeautifulSoup(result.content, "html5lib")
price = html.findAll("div", {"class": "price"})[0].text
price = re.sub(r"[\s\$]+", "", price)
return float(price)
def crypto_to_butter(code, butter, czk):
return crypto_price(code) * czk / butter
def butters(code):
amount = crypto_to_butter(code, butter, czk)
return f"{code}:{' ' * (6 - len(code))}{round(Decimal(amount), 2)}"
butter = butter_price()
czk = usd_to_czk()
if len(argv) > 1:
for coin in argv[1:]:
print(butters(coin))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment