adds btc price to bot
This commit is contained in:
parent
10c6d270bc
commit
e6e2635ce9
51
bot.py
51
bot.py
@ -41,7 +41,8 @@ CMC_SYMBOL_TO_ID = {
|
|||||||
'DAI': 4943,
|
'DAI': 4943,
|
||||||
}
|
}
|
||||||
CMC_API_KEY = os.environ.get('CMC_API_KEY')
|
CMC_API_KEY = os.environ.get('CMC_API_KEY')
|
||||||
CMC_QUOTE_URL = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id={}'
|
CMC_USD_QUOTE_URL = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id={}'
|
||||||
|
CMC_BTC_QUOTE_URL = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id={}&convert=BTC'
|
||||||
|
|
||||||
|
|
||||||
def first_of(attr, match, it):
|
def first_of(attr, match, it):
|
||||||
@ -89,17 +90,26 @@ def cmc_get_data(jso, cmc_id, pair_symbol='USD'):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def monetary_format(v, decimals=2):
|
def decimal_format(v, decimals=2):
|
||||||
if not v:
|
if not v:
|
||||||
v = 0
|
v = 0
|
||||||
f = locale.format('%.{}f'.format(decimals), v, grouping=True)
|
f = locale.format_string('%.{}f'.format(decimals), v, grouping=True)
|
||||||
return '${}'.format(f)
|
return '{}'.format(f)
|
||||||
|
|
||||||
|
|
||||||
|
def monetary_format(v, decimals=2):
|
||||||
|
return '${}'.format(decimal_format(v, decimals))
|
||||||
|
|
||||||
|
def btc_format(v):
|
||||||
|
return decimal_format(v, decimals=8)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TokenData:
|
class TokenData:
|
||||||
def __init__(self, symbol, price=None, stamp=datetime.now()):
|
def __init__(self, symbol, price=None, stamp=datetime.now()):
|
||||||
self.symbol = symbol
|
self.symbol = symbol
|
||||||
self._price = price
|
self._price = price
|
||||||
|
self._btcPrice = 0
|
||||||
self._percent_change = 0
|
self._percent_change = 0
|
||||||
self._volume = 0
|
self._volume = 0
|
||||||
self._market_cap = 0
|
self._market_cap = 0
|
||||||
@ -108,12 +118,12 @@ class TokenData:
|
|||||||
else:
|
else:
|
||||||
self.stamp = None
|
self.stamp = None
|
||||||
|
|
||||||
def _fetch_from_cmc(self):
|
def _fetch_from_cmc(self, url_template):
|
||||||
""" Get quote data for a specific known symbol """
|
""" Get quote data for a specific known symbol """
|
||||||
jso = None
|
jso = None
|
||||||
|
|
||||||
cmc_id = CMC_SYMBOL_TO_ID.get(self.symbol)
|
cmc_id = CMC_SYMBOL_TO_ID.get(self.symbol)
|
||||||
url = CMC_QUOTE_URL.format(cmc_id)
|
url = url_template.format(cmc_id)
|
||||||
r = requests.get(url, headers={
|
r = requests.get(url, headers={
|
||||||
'X-CMC_PRO_API_KEY': CMC_API_KEY,
|
'X-CMC_PRO_API_KEY': CMC_API_KEY,
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
@ -130,13 +140,18 @@ class TokenData:
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
""" Fetch price from binance """
|
""" Fetch price from binance """
|
||||||
|
jso = None
|
||||||
|
data = None
|
||||||
if self.stamp is None or (
|
if self.stamp is None or (
|
||||||
self.stamp is not None
|
self.stamp is not None
|
||||||
and self.stamp < datetime.now() - CACHE_DURATION
|
and self.stamp < datetime.now() - CACHE_DURATION
|
||||||
):
|
):
|
||||||
# CMC
|
# CMC USD
|
||||||
jso = self._fetch_from_cmc()
|
try:
|
||||||
data = cmc_get_data(jso, CMC_SYMBOL_TO_ID[self.symbol])
|
jso = self._fetch_from_cmc(CMC_USD_QUOTE_URL)
|
||||||
|
data = cmc_get_data(jso, CMC_SYMBOL_TO_ID[self.symbol])
|
||||||
|
except Exception as err:
|
||||||
|
print('Error fetching data: ', str(err))
|
||||||
if data is not None:
|
if data is not None:
|
||||||
self._price = data.get('price')
|
self._price = data.get('price')
|
||||||
self._percent_change = data.get('percent_change')
|
self._percent_change = data.get('percent_change')
|
||||||
@ -144,11 +159,25 @@ class TokenData:
|
|||||||
self._market_cap = data.get('market_cap')
|
self._market_cap = data.get('market_cap')
|
||||||
self.stamp = datetime.now()
|
self.stamp = datetime.now()
|
||||||
|
|
||||||
|
# CMC BTC
|
||||||
|
try:
|
||||||
|
jso = self._fetch_from_cmc(CMC_BTC_QUOTE_URL)
|
||||||
|
data = cmc_get_data(jso, CMC_SYMBOL_TO_ID[self.symbol], 'BTC')
|
||||||
|
except Exception as err:
|
||||||
|
print('Error fetching data: ', str(err))
|
||||||
|
if data is not None:
|
||||||
|
self._btcPrice = data.get('price')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def price(self):
|
def price(self):
|
||||||
self.update()
|
self.update()
|
||||||
return self._price
|
return self._price
|
||||||
|
|
||||||
|
@property
|
||||||
|
def btcPrice(self):
|
||||||
|
self.update()
|
||||||
|
return self._btcPrice
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def volume(self):
|
def volume(self):
|
||||||
self.update()
|
self.update()
|
||||||
@ -553,13 +582,15 @@ class TelegramMonitorBot:
|
|||||||
pdata = self.cached_prices[symbol]
|
pdata = self.cached_prices[symbol]
|
||||||
message = """
|
message = """
|
||||||
*Origin Token* (OGN)
|
*Origin Token* (OGN)
|
||||||
*Price*: {} ({}%)
|
*USD Price*: {} ({}%)
|
||||||
|
*BTC Price*: {}
|
||||||
*Market Cap*: {}
|
*Market Cap*: {}
|
||||||
*Volume(24h)*: {}
|
*Volume(24h)*: {}
|
||||||
|
|
||||||
@{}""".format(
|
@{}""".format(
|
||||||
monetary_format(pdata.price, decimals=5),
|
monetary_format(pdata.price, decimals=5),
|
||||||
pdata.percent_change,
|
pdata.percent_change,
|
||||||
|
btc_format(pdata.btcPrice),
|
||||||
monetary_format(pdata.market_cap),
|
monetary_format(pdata.market_cap),
|
||||||
monetary_format(pdata.volume),
|
monetary_format(pdata.volume),
|
||||||
update.effective_user.username,
|
update.effective_user.username,
|
||||||
|
Loading…
Reference in New Issue
Block a user