Added error messages to coins plugin

This commit is contained in:
mrhanky 2017-08-22 14:52:07 +02:00
parent 87a0111e74
commit b970f38344
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8

View File

@ -26,7 +26,7 @@ class Coins(Plugin):
%%btc [<currency>]
"""
return self.cryptowat_summary('btc', args.get('<currency>', 'usd'))
return self.cryptowat_summary('btc', args)
@command
def eth(self, mask: IrcString, target: IrcString, args: DocOptDict):
@ -34,27 +34,29 @@ class Coins(Plugin):
%%eth [<currency>]
"""
return self.cryptowat_summary('eth', args.get('<currency>', 'usd'))
return self.cryptowat_summary('eth', args)
def cryptowat_summary(self, crypto: str, args: DocOptDict):
currency = args.get('<currency>', 'usd')
def cryptowat_summary(self, crypto: str, currency: str = 'usd'):
# Check if valid currency + crypto2currency
if currency not in self.CURRENCIES or crypto == currency:
return
return '\x02[{}]\x02 Can\'t convert or invalid currency: {}'.format(crypto.upper(), currency)
# Send request to api
data = requests.get(self.API_URL.format(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'])
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'])