bitbot-3.11-fork/modules/bitcoin.py

27 lines
917 B
Python
Raw Permalink Normal View History

#--depends-on commands
from src import ModuleManager, utils
2016-05-03 10:58:42 +00:00
class Module(ModuleManager.BaseModule):
2016-05-03 10:58:42 +00:00
_name = "BTC"
@utils.hook("received.command.btc")
2016-05-03 10:58:42 +00:00
def btc(self, event):
"""
:help: Get the exchange rate of bitcoins
:usage: [currency]
"""
2016-05-03 10:58:42 +00:00
currency = (event["args"] or "USD").upper()
page = utils.http.request("https://blockchain.info/ticker").json()
2016-05-03 10:58:42 +00:00
if page:
if currency in page:
conversion = page[currency]
2016-05-03 10:58:42 +00:00
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()))
2016-05-03 10:58:42 +00:00
else:
raise utils.EventResultsError()