pyircbot/pyircbot/modules/BitcoinPrice.py

43 lines
1.5 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-11-27 18:58:20 -08:00
from pyircbot.modulebase import ModuleBase, command
from pyircbot.modules.ModInfo import info
from decimal import Decimal
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
2017-11-27 18:58:20 -08:00
@info("btc retrieve the current price of bitcoin", cmds=["btc"])
@command("btc", "bitcoin")
def btc(self, msg, cmd):
replyTo = msg.prefix.nick if "#" not in msg.args[0] else msg.args[0]
data = self.getApi()
self.bot.act_PRIVMSG(replyTo, "%s: %s" % (
msg.prefix.nick,
"\x02\x0307Bitcoin:\x03\x02 \x0307${price:.2f}\x0f - "
"24h change: \x0307${change:.2f}\x0f - "
"24h volume: \x0307${volume:.0f}M\x0f".format(price=Decimal(data["price_usd"]),
change=Decimal(data["percent_change_24h"]),
volume=Decimal(data["24h_volume_usd"]) / 10**6)
))
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"]:
2017-11-27 18:58:20 -08:00
self.cache = get("https://api.coinmarketcap.com/v1/ticker/bitcoin/").json()[0]
2015-11-01 18:03:11 -08:00
self.cacheAge = time()
return self.cache