From b970f383444d24c291472e82cec01b2762cb9326 Mon Sep 17 00:00:00 2001 From: mrhanky Date: Tue, 22 Aug 2017 14:52:07 +0200 Subject: [PATCH] Added error messages to coins plugin --- bot/plugins/coins.py | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/bot/plugins/coins.py b/bot/plugins/coins.py index d3cb926..f006726 100644 --- a/bot/plugins/coins.py +++ b/bot/plugins/coins.py @@ -26,7 +26,7 @@ class Coins(Plugin): %%btc [] """ - return self.cryptowat_summary('btc', args.get('', '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 [] """ - return self.cryptowat_summary('eth', args.get('', 'usd')) + return self.cryptowat_summary('eth', args) + + def cryptowat_summary(self, crypto: str, args: DocOptDict): + currency = args.get('', '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'])