diff --git a/bot/coins.py b/bot/coins.py index e1a0df8..0664a67 100644 --- a/bot/coins.py +++ b/bot/coins.py @@ -15,7 +15,7 @@ def _currency(name, prefix=None, suffix=None): return tostr class Coins(Plugin): - API_URL = 'https://api.cryptowat.ch/markets/{market}/{crypto}{currency}/summary' + API_URL = 'https://api.coingecko.com/api/v3/simple/price' CURRENCIES = { 'usd': _currency('usd', prefix='$'), @@ -23,60 +23,89 @@ class Coins(Plugin): 'eth': _currency('eth', suffix='Ξ'), 'btc': _currency('btc', suffix='฿'), 'xmr': _currency('xmr'), + 'xrp': _currency('xrp') + } + + CRYPTO_IDS = { + 'btc': 'bitcoin', + 'eth': 'ethereum', + 'xmr': 'monero', + 'xrp': 'ripple' } @command def btc(self, mask: IrcString, target: IrcString, args: Dict): - """Gets the Bitcoin values from cryptowatch. + """Gets the Bitcoin values from coingecko. %%btc [] """ - return self.cryptowat_summary('btc', 'coinbase', args) + return self.coingecko_summary('btc', args) @command def eth(self, mask: IrcString, target: IrcString, args: Dict): - """Gets the Ethereum values from cryptowatch. + """Gets the Ethereum values from coingecko. %%eth [] """ - return self.cryptowat_summary('eth', 'coinbase', args) + return self.coingecko_summary('eth', args) @command def xmr(self, mask: IrcString, target: IrcString, args: Dict): - """Gets the Monero values from cryptowatch. + """Gets the Monero values from coingecko. %%xmr [] """ - return self.cryptowat_summary('xmr', 'bitfinex', args) + return self.coingecko_summary('xmr', args) + + @command + def xrp(self, mask: IrcString, target: IrcString, args: Dict): + """Gets the Ripple values from coingecko. + + %%xrp [] + """ + return self.coingecko_summary('xrp', args) + + def coingecko_summary(self, crypto: str, args: Dict): + crypto_id = self.CRYPTO_IDS.get(crypto) + if crypto_id is None: + raise ValueError - def cryptowat_summary(self, crypto: str, market: str, args: Dict): currency = args.get('', 'eur').lower() + def format_response(response: str) -> str: + return f'\x02[{crypto.upper()}]\x02 {response}' + if currency not in self.CURRENCIES or crypto == currency: - return '\x02[{}]\x02 Can\'t convert or invalid currency: {}'.format(crypto.upper(), currency) + return format_response(f'Can\'t convert or invalid currency: {currency}') - def get_data(cur): - return requests.get(self.API_URL.format(crypto=crypto, market=market, currency=cur)) + res = requests.get(self.API_URL, params={ + 'ids': crypto_id, + 'vs_currencies': currency, + 'include_market_cap': 'true', + 'include_24hr_vol': 'true', + 'include_24hr_change': 'true' + }) - data = get_data(currency) - if not data and currency != 'usd': - data = get_data('usd') - currency = 'usd' - if not data: - return '\x02[{}]\x02 No data received'.format(crypto) + if res.status_code == 429: + return format_response('Rate Limit exceeded') + + if res.status_code != 200: + return format_response('API Error') + + data = res.json()[crypto_id] + + if currency not in data: + return format_response('No data received') to_currency = self.CURRENCIES[currency] - result = data.json()['result'] - return '\x02[{crypto}]\x02 ' \ - 'Current: \x02\x0307{last}\x0F - ' \ - 'High: \x02\x0303{high}\x0F - ' \ - 'Low: \x02\x0304{low}\x0F - ' \ + return format_response( + 'Price: \x02\x0307{price}\x0F - ' \ 'Change: \x02\x0307{change:,.2f}%\x0F - ' \ - 'Volume: \x02\x0307{volume}\x0F' \ + 'Volume: \x02\x0307{volume}\x0F - ' \ + 'Market Cap: \x02\x0307{market_cap}\x0F' \ .format(crypto=crypto.upper(), - last=to_currency(result['price']['last']), - high=to_currency(result['price']['high']), - low=to_currency(result['price']['low']), - change=result['price']['change']['percentage'] * 100, - volume=result['volume']) + price=to_currency(data[currency]), + change=data[f'{currency}_24h_change'] * 100, + volume=data[f'{currency}_24h_vol'], + market_cap=to_currency(data[f'{currency}_market_cap'])))