From e93382569d046d4e966a731bc48809ecbc8c9c3a Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 12 Oct 2014 01:12:34 -0700 Subject: [PATCH] Added bitcoin price module --- data/config/BitcoinPrice.yml | 1 + pyircbot/modules/BitcoinPrice.py | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 data/config/BitcoinPrice.yml create mode 100644 pyircbot/modules/BitcoinPrice.py diff --git a/data/config/BitcoinPrice.yml b/data/config/BitcoinPrice.yml new file mode 100644 index 0000000..d7dc8a8 --- /dev/null +++ b/data/config/BitcoinPrice.yml @@ -0,0 +1 @@ +cache: 300 \ No newline at end of file diff --git a/pyircbot/modules/BitcoinPrice.py b/pyircbot/modules/BitcoinPrice.py new file mode 100644 index 0000000..916ec7b --- /dev/null +++ b/pyircbot/modules/BitcoinPrice.py @@ -0,0 +1,42 @@ +""" +.. module:: BitcoinPrice + :synopsis: Provides .bitcoin to show price indexes + +.. moduleauthor:: Dave Pedu + +""" + +from modulebase import ModuleBase,ModuleHook +from requests import get +from time import time + +class BitcoinPrice(ModuleBase): + def __init__(self, bot, moduleName): + ModuleBase.__init__(self, bot, moduleName); + + self.cache = None + self.cacheAge = 0 + + self.hooks=[ + ModuleHook(["PRIVMSG"], self.btc) + ] + + def btc(self, args, prefix, trailing): + prefix = self.bot.decodePrefix(prefix) + replyTo = prefix.nick if not "#" in args[0] else args[0] + + cmd = self.bot.messageHasCommand([".btc", ".bitcoin"], trailing) + if cmd: + data = self.getApi() + self.bot.act_PRIVMSG(replyTo, "%s: %s" % ( + prefix.nick, + "\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']) + )) + + def getApi(self): + if self.cache == None or time()-self.cacheAge>self.config["cache"]: + self.cache = get("https://btc-e.com/api/2/btc_usd/ticker").json() + self.cacheAge = time() + return self.cache + + \ No newline at end of file