Proper pre- or suffix for currencies
This commit is contained in:
parent
e00636de02
commit
6a508420ed
31
bot/coins.py
31
bot/coins.py
|
@ -6,15 +6,21 @@ from irc3.utils import IrcString
|
||||||
|
|
||||||
from . import Plugin
|
from . import Plugin
|
||||||
|
|
||||||
|
def __currency(name=None, prefix=None, suffix=None):
|
||||||
|
return lambda value:
|
||||||
|
return '{prefix}{value:,.2f}{suffix}'.format(
|
||||||
|
prefix=prefix or '',
|
||||||
|
suffix=suffix or (' ' + name if name else ''),
|
||||||
|
value=value)
|
||||||
|
|
||||||
class Coins(Plugin):
|
class Coins(Plugin):
|
||||||
API_URL = 'https://api.cryptowat.ch/markets/{market}/{crypto}{currency}/summary'
|
API_URL = 'https://api.cryptowat.ch/markets/{market}/{crypto}{currency}/summary'
|
||||||
CURRENCIES = {
|
CURRENCIES = {
|
||||||
'usd': '$',
|
'usd': __currency(prefix='$'),
|
||||||
'eur': '€',
|
'eur': __currency(suffix='€'),
|
||||||
'eth': 'Ξ',
|
'eth': __currency(suffix='Ξ'),
|
||||||
'btc': '฿',
|
'btc': __currency(suffix='฿'),
|
||||||
'xmr': 'xmr',
|
'xmr': __currency(name='xmr'),
|
||||||
}
|
}
|
||||||
|
|
||||||
@command
|
@command
|
||||||
|
@ -47,21 +53,22 @@ class Coins(Plugin):
|
||||||
if currency not in self.CURRENCIES or crypto == currency:
|
if currency not in self.CURRENCIES or crypto == currency:
|
||||||
return '\x02[{}]\x02 Can\'t convert or invalid currency: {}'.format(crypto.upper(), currency)
|
return '\x02[{}]\x02 Can\'t convert or invalid currency: {}'.format(crypto.upper(), currency)
|
||||||
|
|
||||||
|
currency = self.CURRENCIES[currency]
|
||||||
|
|
||||||
data = requests.get(self.API_URL.format(crypto=crypto, market=market, currency=currency))
|
data = requests.get(self.API_URL.format(crypto=crypto, market=market, currency=currency))
|
||||||
if not data:
|
if not data:
|
||||||
return '\x02[{}]\x02 No data received'.format(crypto)
|
return '\x02[{}]\x02 No data received'.format(crypto)
|
||||||
|
|
||||||
result = data.json()['result']
|
result = data.json()['result']
|
||||||
return '\x02[{crypto}]\x02 ' \
|
return '\x02[{crypto}]\x02 ' \
|
||||||
'Current: \x02\x0307{currency}{last:,.2f}\x0F - ' \
|
'Current: \x02\x0307{last}\x0F - ' \
|
||||||
'High: \x02\x0303{currency}{high:,.2f}\x0F - ' \
|
'High: \x02\x0303{high}\x0F - ' \
|
||||||
'Low: \x02\x0304{currency}{low:,.2f}\x0F - ' \
|
'Low: \x02\x0304{low}\x0F - ' \
|
||||||
'Change: \x02\x0307{change:,.2f}%\x0F - ' \
|
'Change: \x02\x0307{change:,.2f}%\x0F - ' \
|
||||||
'Volume: \x02\x0307{volume}\x0F' \
|
'Volume: \x02\x0307{volume}\x0F' \
|
||||||
.format(crypto=crypto.upper(),
|
.format(crypto=crypto.upper(),
|
||||||
currency=self.CURRENCIES[currency],
|
last=currency(result['price']['last']),
|
||||||
last=result['price']['last'],
|
high=currency(result['price']['high']),
|
||||||
high=result['price']['high'],
|
low=currency(result['price']['low']),
|
||||||
low=result['price']['low'],
|
|
||||||
change=result['price']['change']['percentage'] * 100,
|
change=result['price']['change']['percentage'] * 100,
|
||||||
volume=result['volume'])
|
volume=result['volume'])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user