Update price.py

Top() gets "T", "B", or "M", with trillions rounding to 2 decimals, billions rounding to 1 decimal, and millions not rounding at all.
This commit is contained in:
scoobybejesus 2021-01-06 18:25:44 -05:00 committed by GitHub
parent efa7c4a0fb
commit b212831237
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 15 deletions

View File

@ -418,12 +418,21 @@ def top(bot, trigger):
topXstring = "" topXstring = ""
try: try:
try: try:
r = requests.get('https://api.coingecko.com/api/v3/global') r = requests.get('https://api.coingecko.com/api/v3/global')
j = r.json() j = r.json()
usd_total_mkt_cap = float(j['data']['total_market_cap']['usd']) usd_total_mkt_cap = float(j['data']['total_market_cap']['usd'])
total_mcap_short = int(int(round(usd_total_mkt_cap,-9))/int(1e9)) total_mcap_short = 0
rounded_total_mcap = str(total_mcap_short)+"B" magnitude_sym = "M"
topXstring += "Total market cap $" + rounded_total_mcap + " | " if usd_total_mkt_cap >= 1e12:
total_mcap_short = round(usd_total_mkt_cap/float(1e12),2)
magnitude_sym = "T"
elif usd_total_mkt_cap >= 1e9:
total_mcap_short = round(usd_total_mkt_cap/float(1e9),1)
magnitude_sym = "B"
else:
total_mcap_short = round(usd_total_mkt_cap/float(1e6),0)
rounded_total_mcap = str(total_mcap_short)+magnitude_sym
topXstring += "Total market cap $" + rounded_total_mcap + " | "
except: except:
bot.say("Can't connect to coinmarketcap API") bot.say("Can't connect to coinmarketcap API")
if not trigger.group(2): if not trigger.group(2):
@ -442,16 +451,18 @@ def top(bot, trigger):
rank = i['market_cap_rank'] rank = i['market_cap_rank']
price_usd = float(i['current_price']) price_usd = float(i['current_price'])
market_cap_usd = float(i['market_cap']) market_cap_usd = float(i['market_cap'])
if market_cap_usd >= 1e9: mcap_short = 0
if market_cap_usd >= 1e10: magnitude_sym = "M"
market_cap_short = int(int(round(market_cap_usd,-9))/int(1e9)) if market_cap_usd >= 1e12:
else: mcap_short = round(market_cap_usd/float(1e12),2)
market_cap_short = float(round(market_cap_usd,-8)/1e9) magnitude_sym = "T"
rounded_mcap = str(market_cap_short)+"B" elif market_cap_usd >= 1e9:
mcap_short = round(market_cap_usd/float(1e9),1)
magnitude_sym = "B"
else: else:
#rounded_mcap = "tiny" #mcap_short = "tiny"
market_cap_short = float(round(market_cap_usd,-5)/1e6) mcap_short = round(market_cap_usd/float(1e6),0)
rounded_mcap = str(market_cap_short)+"M" rounded_mcap = str(mcap_short)+magnitude_sym
topXstring += "{0}. {1} ${2} | ".format(rank, symbol, rounded_mcap) #TODO: add price_usd, rounded topXstring += "{0}. {1} ${2} | ".format(rank, symbol, rounded_mcap) #TODO: add price_usd, rounded
bot.say(topXstring[:-2]) bot.say(topXstring[:-2])
except: except: