nxy/bot/coins.py

112 lines
3.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import requests
2017-08-22 13:57:57 +00:00
from docopt import Dict
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import Plugin
2017-10-15 01:49:08 +00:00
def _currency(name, prefix=None, suffix=None):
def tostr(value):
return '{prefix}{value:,.2f}{suffix}'.format(
prefix=prefix or '',
suffix=suffix or ('' if (prefix or suffix) else (' ' + name)),
value=value)
return tostr
class Coins(Plugin):
2024-12-11 00:08:54 +00:00
API_URL = 'https://api.coingecko.com/api/v3/simple/price'
2017-10-15 01:37:49 +00:00
2017-06-30 17:33:26 +00:00
CURRENCIES = {
2017-10-15 01:49:08 +00:00
'usd': _currency('usd', prefix='$'),
'eur': _currency('eur', suffix=''),
'eth': _currency('eth', suffix='Ξ'),
'btc': _currency('btc', suffix='฿'),
'xmr': _currency('xmr'),
2024-12-11 00:08:54 +00:00
'xrp': _currency('xrp')
}
CRYPTO_IDS = {
'btc': 'bitcoin',
'eth': 'ethereum',
'xmr': 'monero',
'xrp': 'ripple'
2017-06-30 17:33:26 +00:00
}
@command
2017-08-22 13:57:57 +00:00
def btc(self, mask: IrcString, target: IrcString, args: Dict):
2024-12-11 00:08:54 +00:00
"""Gets the Bitcoin values from coingecko.
2017-06-30 17:33:26 +00:00
%%btc [<currency>]
"""
2024-12-11 00:08:54 +00:00
return self.coingecko_summary('btc', args)
2017-10-15 01:42:18 +00:00
@command
2017-08-22 13:57:57 +00:00
def eth(self, mask: IrcString, target: IrcString, args: Dict):
2024-12-11 00:08:54 +00:00
"""Gets the Ethereum values from coingecko.
2017-06-30 17:33:26 +00:00
%%eth [<currency>]
"""
2024-12-11 00:08:54 +00:00
return self.coingecko_summary('eth', args)
2017-10-15 01:42:18 +00:00
2017-09-21 04:08:50 +00:00
@command
def xmr(self, mask: IrcString, target: IrcString, args: Dict):
2024-12-11 00:08:54 +00:00
"""Gets the Monero values from coingecko.
2017-09-21 04:08:50 +00:00
%%xmr [<currency>]
"""
2024-12-11 00:08:54 +00:00
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
2017-08-22 12:52:07 +00:00
currency = args.get('<currency>', 'eur').lower()
2017-06-30 17:33:26 +00:00
2024-12-11 00:08:54 +00:00
def format_response(response: str) -> str:
return f'\x02[{crypto.upper()}]\x02 {response}'
2017-06-30 17:33:26 +00:00
if currency not in self.CURRENCIES or crypto == currency:
2024-12-11 00:08:54 +00:00
return format_response(f'Can\'t convert or invalid currency: {currency}')
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'
})
if res.status_code == 429:
return format_response('Rate Limit exceeded')
if res.status_code != 200:
return format_response('API Error')
2017-06-30 17:33:26 +00:00
2024-12-11 00:08:54 +00:00
data = res.json()[crypto_id]
2024-12-11 00:08:54 +00:00
if currency not in data:
return format_response('No data received')
2017-08-22 12:52:07 +00:00
2017-10-15 01:51:12 +00:00
to_currency = self.CURRENCIES[currency]
2024-12-11 00:08:54 +00:00
return format_response(
'Price: \x02\x0307{price}\x0F - ' \
2017-08-22 12:52:07 +00:00
'Change: \x02\x0307{change:,.2f}%\x0F - ' \
2024-12-11 00:08:54 +00:00
'Volume: \x02\x0307{volume}\x0F - ' \
'Market Cap: \x02\x0307{market_cap}\x0F' \
2017-08-22 12:52:07 +00:00
.format(crypto=crypto.upper(),
2024-12-11 00:08:54 +00:00
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'])))