nxy/bot/coins.py

83 lines
2.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import requests
2017-08-22 13:57:57 +00:00
from docopt import Dict
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import Plugin
2017-10-15 01:49:08 +00:00
def _currency(name, prefix=None, suffix=None):
def tostr(value):
return '{prefix}{value:,.2f}{suffix}'.format(
prefix=prefix or '',
suffix=suffix or ('' if (prefix or suffix) else (' ' + name)),
value=value)
return tostr
class Coins(Plugin):
2017-09-21 04:08:50 +00:00
API_URL = 'https://api.cryptowat.ch/markets/{market}/{crypto}{currency}/summary'
2017-10-15 01:37:49 +00:00
2017-06-30 17:33:26 +00:00
CURRENCIES = {
2017-10-15 01:49:08 +00:00
'usd': _currency('usd', prefix='$'),
'eur': _currency('eur', suffix=''),
'eth': _currency('eth', suffix='Ξ'),
'btc': _currency('btc', suffix='฿'),
'xmr': _currency('xmr'),
2017-06-30 17:33:26 +00:00
}
@command
2017-08-22 13:57:57 +00:00
def btc(self, mask: IrcString, target: IrcString, args: Dict):
2017-06-30 18:14:02 +00:00
"""Gets the Bitcoin values from cryptowatch.
2017-06-30 17:33:26 +00:00
%%btc [<currency>]
"""
2017-09-21 04:08:50 +00:00
return self.cryptowat_summary('btc', 'coinbase', args)
2017-10-15 01:42:18 +00:00
@command
2017-08-22 13:57:57 +00:00
def eth(self, mask: IrcString, target: IrcString, args: Dict):
2017-06-30 18:14:02 +00:00
"""Gets the Ethereum values from cryptowatch.
2017-06-30 17:33:26 +00:00
%%eth [<currency>]
"""
2017-09-21 04:08:50 +00:00
return self.cryptowat_summary('eth', 'coinbase', args)
2017-10-15 01:42:18 +00:00
2017-09-21 04:08:50 +00:00
@command
def xmr(self, mask: IrcString, target: IrcString, args: Dict):
2017-09-21 15:15:23 +00:00
"""Gets the Monero values from cryptowatch.
2017-09-21 04:08:50 +00:00
%%xmr [<currency>]
"""
2017-09-21 15:15:23 +00:00
return self.cryptowat_summary('xmr', 'bitfinex', args)
2017-08-22 12:52:07 +00:00
2017-09-21 09:39:01 +00:00
def cryptowat_summary(self, crypto: str, market: str, args: Dict):
currency = args.get('<currency>', 'eur').lower()
2017-06-30 17:33:26 +00:00
if currency not in self.CURRENCIES or crypto == currency:
2017-08-22 12:52:07 +00:00
return '\x02[{}]\x02 Can\'t convert or invalid currency: {}'.format(crypto.upper(), currency)
2017-06-30 17:33:26 +00:00
def get_data(cur):
return requests.get(self.API_URL.format(crypto=crypto, market=market, currency=cur))
data = get_data(currency)
if not data and currency != 'usd':
data = get_data('usd')
currency = 'usd'
2017-08-22 12:52:07 +00:00
if not data:
return '\x02[{}]\x02 No data received'.format(crypto)
2017-08-22 12:52:07 +00:00
2017-10-15 01:51:12 +00:00
to_currency = self.CURRENCIES[currency]
2017-08-22 12:52:07 +00:00
result = data.json()['result']
return '\x02[{crypto}]\x02 ' \
2017-10-15 01:23:31 +00:00
'Current: \x02\x0307{last}\x0F - ' \
'High: \x02\x0303{high}\x0F - ' \
'Low: \x02\x0304{low}\x0F - ' \
2017-08-22 12:52:07 +00:00
'Change: \x02\x0307{change:,.2f}%\x0F - ' \
'Volume: \x02\x0307{volume}\x0F' \
.format(crypto=crypto.upper(),
2017-10-15 01:51:12 +00:00
last=to_currency(result['price']['last']),
high=to_currency(result['price']['high']),
low=to_currency(result['price']['low']),
2017-08-22 12:52:07 +00:00
change=result['price']['change']['percentage'] * 100,
volume=result['volume'])