pyircbot/pyircbot/modules/BitcoinPrice.py

43 lines
1.3 KiB
Python
Raw Normal View History

2014-10-12 01:12:34 -07:00
"""
.. module:: BitcoinPrice
2015-11-01 18:03:11 -08:00
:synopsis: Provides .bitcoin to show price indexes
2014-10-12 01:12:34 -07:00
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
2017-01-01 14:59:01 -08:00
from pyircbot.modulebase import ModuleBase, ModuleHook
2014-10-12 01:12:34 -07:00
from requests import get
from time import time
2017-01-01 14:59:01 -08:00
2014-10-12 01:12:34 -07:00
class BitcoinPrice(ModuleBase):
2015-11-01 18:03:11 -08:00
def __init__(self, bot, moduleName):
2017-01-01 14:59:01 -08:00
ModuleBase.__init__(self, bot, moduleName)
2015-11-01 18:03:11 -08:00
self.cache = None
self.cacheAge = 0
2017-01-01 14:59:01 -08:00
self.hooks = [
2015-11-01 18:03:11 -08:00
ModuleHook(["PRIVMSG"], self.btc)
]
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def btc(self, args, prefix, trailing):
prefix = self.bot.decodePrefix(prefix)
2017-01-01 14:59:01 -08:00
replyTo = prefix.nick if "#" not in args[0] else args[0]
2015-11-01 18:03:11 -08:00
cmd = self.bot.messageHasCommand([".btc", ".bitcoin"], trailing)
if cmd:
data = self.getApi()
self.bot.act_PRIVMSG(replyTo, "%s: %s" % (
prefix.nick,
2017-01-01 14:59:01 -08:00
"\x02\x0307Bitcoin:\x03\x02 \x0307{buy:.0f}\x0f$ - High: \x0307{high:.0f}\x0f$ - "
"Low: \x0307{low:.0f}\x0f$ - Volume: \x0307{vol_cur:.0f}\x03฿".format(**data['ticker'])
2015-11-01 18:03:11 -08:00
))
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def getApi(self):
2017-01-01 14:59:01 -08:00
if self.cache is None or time() - self.cacheAge > self.config["cache"]:
2015-11-01 18:03:11 -08:00
self.cache = get("https://btc-e.com/api/2/btc_usd/ticker").json()
self.cacheAge = time()
return self.cache