From 2841348d618abcc7e693b04204547951ef718b20 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 22 Jul 2017 12:13:20 +0100 Subject: [PATCH] Support all currencies that preev supports --- dave/modules/btc.py | 77 ----------------------------------------------------------------------------- dave/modules/cryptocurrency.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 77 deletions(-) diff --git a/dave/modules/btc.py b/dave/modules/btc.py deleted file mode 100644 index 6697e60..0000000 100644 --- a/dave/modules/btc.py +++ /dev/null @@ -1,77 +1,0 @@ -# -*- coding: utf-8 -*- -"""Get the average BTC price from preev for bitfinex, bitstamp and btce""" -from __future__ import division - -import pickle - -import dave.module -import dave.modules -import dave.config -import requests -import socket -import babel.numbers -import decimal - -@dave.module.help("Syntax: btc. Get the current BTC prices from preev.") -@dave.module.command(["btc"], '?([a-zA-Z]*)?') -@dave.module.priority(dave.module.Priority.HIGHEST) -def btc(bot, args, sender, source): - currency = (args[0] or 'usd').upper() - currencyKey = currency.lower() - - p = preev(currency) - - if p == 'Not found.': - # Invalid currency. - bot.reply(source, sender, "Preev doesn't support that currency.") - return - - if 'usd' in p['btc'] and currencyKey not in p['btc'] and 'usd' in p[currencyKey]: - # We were given the exchange rate for currency -> usd but not currency -> btc - multiplier = 1 / decimal.Decimal(p[currencyKey]['usd']['other']['last']) - currencyKey = 'usd' - else: - multiplier = decimal.Decimal(1) - - total_volume = sum( - float(market['volume']) - for market in p['btc'][currencyKey].itervalues() - ) - - avg = sum( - float(market['last']) * float(market['volume']) / total_volume - for market in p['btc'][currencyKey].itervalues() - ) - - prices = ', '.join([ - u'{}: {}'.format( - market, - babel.numbers.format_currency(decimal.Decimal(data['last']) * multiplier, - currency) - ).encode('utf-8') for market, data in p['btc'][currencyKey].iteritems() - ]) - - prices += u'. average: {}'.format( - babel.numbers.format_currency(decimal.Decimal(avg) * multiplier, - currency) - ).encode('utf-8') - - bot.reply(source, sender, prices.decode('utf-8')) - -def preev(currency): - """Contact the preev api and get the latest prices and cache for 10 seconds""" - key = "btc:{}".format(currency) - - if not dave.config.redis.exists(key): - r = requests.get( - "http://preev.com/pulse/units:btc+{}/sources:bitfinex+bitstamp+btce".format( - currency - )) - - json = r.json() - - dave.config.redis.setex(key, 10, pickle.dumps(json)) - else: - json = pickle.loads(dave.config.redis.get(key)) - - return json diff --git a/dave/modules/cryptocurrency.py b/dave/modules/cryptocurrency.py new file mode 100644 index 0000000..b7477b7 100644 --- /dev/null +++ a/dave/modules/cryptocurrency.py @@ -1,0 +1,96 @@ +# -*- coding: utf-8 -*- +"""Get the average BTC price from preev for bitfinex, bitstamp and btce""" +from __future__ import division + +import pickle + +import dave.module +import dave.modules +import dave.config +import requests +import babel.numbers +import decimal +from twisted.words.protocols.irc import assembleFormattedText, attributes as A + +@dave.module.help("Syntax: btc. Get the current BTC prices from preev.") +@dave.module.command(["btc"], '?([a-zA-Z]*)?') +def btc(bot, args, sender, source): + crypto(bot, ['btc', args[0]], sender, source) + + +@dave.module.help("Syntax: ltc. Get the current LTC prices from preev.") +@dave.module.command(["ltc"], '?([a-zA-Z]*)?') +def btc(bot, args, sender, source): + crypto(bot, ['ltc', args[0]], sender, source) + +@dave.module.help("Syntax: crypto [btc/ltc/etc] (usd/gbp/btc/etc). Get current prices " + "from preev. You can also convert cryptocurrency to cryptocurrency.") +@dave.module.command(["crypto", "cryptocurrency"], '([a-zA-Z]*)(?: ([a-zA-Z]*))?') +def crypto(bot, args, sender, source): + cryptocurrency = args[0].upper() + cryptoKey = cryptocurrency.lower() + + currency = (args[1] or 'usd').upper() + currencyKey = currency.lower() + + p = preev(cryptocurrency, currency) + + if p == 'Not found.': + # Invalid currency. + bot.reply(source, sender, "Preev doesn't support that currency.") + return + + if 'usd' in p[cryptoKey] \ + and currencyKey not in p[cryptoKey] and 'usd' in p[currencyKey]: + # We were given the exchange rate for currency -> usd but not currency -> crypto + multiplier = 1 / decimal.Decimal(list(p[currencyKey]['usd'].values())[0]['last']) + currencyKey = 'usd' + else: + multiplier = decimal.Decimal(1) + + total_volume = sum( + float(market['volume']) for market in p[cryptoKey][currencyKey].itervalues() + ) + + avg = sum( + float(market['last']) * float(market['volume']) / total_volume + for market in p[cryptoKey][currencyKey].itervalues() + ) + + prices = assembleFormattedText(A.normal[ + A.bold['{} -> {}'.format(cryptocurrency, currency)], + ': ', + ', '.join([ + u'{}: {}'.format( + market, + babel.numbers.format_currency(decimal.Decimal(data['last']) * multiplier, + currency) + ).encode('utf-8') for market, data in p[cryptoKey][currencyKey].iteritems() + ]) + ]) + + prices += u'. average: {}'.format( + babel.numbers.format_currency(decimal.Decimal(avg) * multiplier, + currency) + ).encode('utf-8') + + bot.reply(source, sender, prices.decode('utf-8')) + +def preev(cryptocurrency, currency): + """Contact the preev api and get the latest prices and cache for 10 seconds""" + key = "crypto:{}:{}".format(cryptocurrency, currency) + + if not dave.config.redis.exists(key): + r = requests.get( + "http://preev.com/pulse/units:{}+{}/sources:bitfinex+bitstamp+btce".format( + cryptocurrency, + currency + )) + + json = r.json() + + dave.config.redis.setex(key, 10, pickle.dumps(json)) + else: + json = pickle.loads(dave.config.redis.get(key)) + + return json -- rgit 0.1.3