parent
3f7d1ae13c
commit
91abf2bf93
85
bot/coins.py
85
bot/coins.py
@ -15,7 +15,7 @@ def _currency(name, prefix=None, suffix=None):
|
|||||||
return tostr
|
return tostr
|
||||||
|
|
||||||
class Coins(Plugin):
|
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 = {
|
CURRENCIES = {
|
||||||
'usd': _currency('usd', prefix='$'),
|
'usd': _currency('usd', prefix='$'),
|
||||||
@ -23,60 +23,89 @@ class Coins(Plugin):
|
|||||||
'eth': _currency('eth', suffix='Ξ'),
|
'eth': _currency('eth', suffix='Ξ'),
|
||||||
'btc': _currency('btc', suffix='฿'),
|
'btc': _currency('btc', suffix='฿'),
|
||||||
'xmr': _currency('xmr'),
|
'xmr': _currency('xmr'),
|
||||||
|
'xrp': _currency('xrp')
|
||||||
|
}
|
||||||
|
|
||||||
|
CRYPTO_IDS = {
|
||||||
|
'btc': 'bitcoin',
|
||||||
|
'eth': 'ethereum',
|
||||||
|
'xmr': 'monero',
|
||||||
|
'xrp': 'ripple'
|
||||||
}
|
}
|
||||||
|
|
||||||
@command
|
@command
|
||||||
def btc(self, mask: IrcString, target: IrcString, args: Dict):
|
def btc(self, mask: IrcString, target: IrcString, args: Dict):
|
||||||
"""Gets the Bitcoin values from cryptowatch.
|
"""Gets the Bitcoin values from coingecko.
|
||||||
|
|
||||||
%%btc [<currency>]
|
%%btc [<currency>]
|
||||||
"""
|
"""
|
||||||
return self.cryptowat_summary('btc', 'coinbase', args)
|
return self.coingecko_summary('btc', args)
|
||||||
|
|
||||||
@command
|
@command
|
||||||
def eth(self, mask: IrcString, target: IrcString, args: Dict):
|
def eth(self, mask: IrcString, target: IrcString, args: Dict):
|
||||||
"""Gets the Ethereum values from cryptowatch.
|
"""Gets the Ethereum values from coingecko.
|
||||||
|
|
||||||
%%eth [<currency>]
|
%%eth [<currency>]
|
||||||
"""
|
"""
|
||||||
return self.cryptowat_summary('eth', 'coinbase', args)
|
return self.coingecko_summary('eth', args)
|
||||||
|
|
||||||
@command
|
@command
|
||||||
def xmr(self, mask: IrcString, target: IrcString, args: Dict):
|
def xmr(self, mask: IrcString, target: IrcString, args: Dict):
|
||||||
"""Gets the Monero values from cryptowatch.
|
"""Gets the Monero values from coingecko.
|
||||||
|
|
||||||
%%xmr [<currency>]
|
%%xmr [<currency>]
|
||||||
"""
|
"""
|
||||||
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 [<currency>]
|
||||||
|
"""
|
||||||
|
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('<currency>', 'eur').lower()
|
currency = args.get('<currency>', 'eur').lower()
|
||||||
|
|
||||||
|
def format_response(response: str) -> str:
|
||||||
|
return f'\x02[{crypto.upper()}]\x02 {response}'
|
||||||
|
|
||||||
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 format_response(f'Can\'t convert or invalid currency: {currency}')
|
||||||
|
|
||||||
def get_data(cur):
|
res = requests.get(self.API_URL, params={
|
||||||
return requests.get(self.API_URL.format(crypto=crypto, market=market, currency=cur))
|
'ids': crypto_id,
|
||||||
|
'vs_currencies': currency,
|
||||||
|
'include_market_cap': 'true',
|
||||||
|
'include_24hr_vol': 'true',
|
||||||
|
'include_24hr_change': 'true'
|
||||||
|
})
|
||||||
|
|
||||||
data = get_data(currency)
|
if res.status_code == 429:
|
||||||
if not data and currency != 'usd':
|
return format_response('Rate Limit exceeded')
|
||||||
data = get_data('usd')
|
|
||||||
currency = 'usd'
|
if res.status_code != 200:
|
||||||
if not data:
|
return format_response('API Error')
|
||||||
return '\x02[{}]\x02 No data received'.format(crypto)
|
|
||||||
|
data = res.json()[crypto_id]
|
||||||
|
|
||||||
|
if currency not in data:
|
||||||
|
return format_response('No data received')
|
||||||
|
|
||||||
to_currency = self.CURRENCIES[currency]
|
to_currency = self.CURRENCIES[currency]
|
||||||
|
|
||||||
result = data.json()['result']
|
return format_response(
|
||||||
return '\x02[{crypto}]\x02 ' \
|
'Price: \x02\x0307{price}\x0F - ' \
|
||||||
'Current: \x02\x0307{last}\x0F - ' \
|
|
||||||
'High: \x02\x0303{high}\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 - ' \
|
||||||
|
'Market Cap: \x02\x0307{market_cap}\x0F' \
|
||||||
.format(crypto=crypto.upper(),
|
.format(crypto=crypto.upper(),
|
||||||
last=to_currency(result['price']['last']),
|
price=to_currency(data[currency]),
|
||||||
high=to_currency(result['price']['high']),
|
change=data[f'{currency}_24h_change'] * 100,
|
||||||
low=to_currency(result['price']['low']),
|
volume=data[f'{currency}_24h_vol'],
|
||||||
change=result['price']['change']['percentage'] * 100,
|
market_cap=to_currency(data[f'{currency}_market_cap'])))
|
||||||
volume=result['volume'])
|
|
||||||
|
Loading…
Reference in New Issue
Block a user