nxy/bot/plugins/coins.py

63 lines
2.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import irc3
import requests
from docopt import Dict as DocOptDict
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import Plugin
@irc3.plugin
class Coins(Plugin):
requires = ['irc3.plugins.command']
2017-07-07 00:11:20 +00:00
API_URL = 'https://api.cryptowat.ch/markets/coinbase/{crypto}{currency}/summary'
2017-06-30 17:33:26 +00:00
CURRENCIES = {
'usd': '$',
'eur': '',
'eth': 'Ξ',
'btc': '฿',
}
@command
def btc(self, mask: IrcString, target: IrcString, args: DocOptDict):
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-08-22 12:52:07 +00:00
return self.cryptowat_summary('btc', args)
@command
def eth(self, mask: IrcString, target: IrcString, args: DocOptDict):
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-08-22 12:52:07 +00:00
return self.cryptowat_summary('eth', args)
def cryptowat_summary(self, crypto: str, args: DocOptDict):
currency = args.get('<currency>', 'usd')
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
2017-07-07 00:11:20 +00:00
data = requests.get(self.API_URL.format(crypto=crypto, currency=currency))
2017-08-22 12:52:07 +00:00
if not data:
return '\x02[{}]\x02 No data received'.format(crypto)
result = data.json()['result']
return '\x02[{crypto}]\x02 ' \
'Current: \x02\x0307{currency}{last:,.2f}\x0F - ' \
'High: \x02\x0303{currency}{high:,.2f}\x0F - ' \
'Low: \x02\x0304{currency}{low:,.2f}\x0F - ' \
'Change: \x02\x0307{change:,.2f}%\x0F - ' \
'Volume: \x02\x0307{volume}\x0F' \
.format(crypto=crypto.upper(),
currency=self.CURRENCIES[currency],
last=result['price']['last'],
high=result['price']['high'],
low=result['price']['low'],
change=result['price']['change']['percentage'] * 100,
volume=result['volume'])