2018-09-24 14:13:27 +00:00
|
|
|
from src import Utils
|
2016-05-03 10:58:42 +00:00
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
_name = "BTC"
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2016-05-03 10:58:42 +00:00
|
|
|
self.bot = bot
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("received.command.btc").hook(self.btc,
|
|
|
|
help="Get the exchange rate of bitcoins", usage="[currency]")
|
2016-05-03 10:58:42 +00:00
|
|
|
|
|
|
|
def btc(self, event):
|
|
|
|
currency = (event["args"] or "USD").upper()
|
|
|
|
page = Utils.get_url("https://blockchain.info/ticker",
|
|
|
|
json=True)
|
|
|
|
if page:
|
|
|
|
if currency in page:
|
|
|
|
conversion = page[currency]
|
|
|
|
buy, sell = conversion["buy"], conversion["sell"]
|
|
|
|
event["stdout"].write("1 BTC = %.2f %s (buy) %.2f %s "
|
|
|
|
"(sell)" % (buy, currency, sell, currency))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown currency, available "
|
|
|
|
"currencies: %s" % ", ".join(page.keys()))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Failed to load results")
|