2017-07-04 15:22:20 +02:00

65 lines
2.3 KiB
Python

# -*- 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']
CRYPTOWAT = 'https://api.cryptowat.ch/markets/{market}/{crypto}{currency}' \
'/summary'
CURRENCIES = {
'usd': '$',
'eur': '',
'eth': 'Ξ',
'btc': '฿',
}
@command
def btc(self, mask: IrcString, target: IrcString, args: DocOptDict):
"""Gets the Bitcoin values from cryptowatch.
%%btc [<currency>]
"""
return self._cryptowat_summary('btc', args.get('<currency>') or 'usd')
@command
def eth(self, mask: IrcString, target: IrcString, args: DocOptDict):
"""Gets the Ethereum values from cryptowatch.
%%eth [<currency>]
"""
return self._cryptowat_summary('eth', args.get('<currency>') or 'usd')
def _cryptowat_summary(self, crypto: str, currency: str = 'usd',
market: str = 'coinbase'):
# Check if valid currency + crypto2currency
if currency not in self.CURRENCIES or crypto == currency:
return
# Send request to api
data = requests.get(self.CRYPTOWAT.format(market=market,
crypto=crypto,
currency=currency))
if data:
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'])