Merge pull request #50 from scoobybejesus/patch-3

Update price.py
This commit is contained in:
jw 2021-01-07 05:36:13 -08:00 committed by GitHub
commit 65c7245116
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 17 deletions

View File

@ -421,19 +421,20 @@ def top(bot, trigger):
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)) rounded_total_mcap = trim_mcap(usd_total_mkt_cap)
rounded_total_mcap = str(total_mcap_short)+"B"
topXstring += "Total market cap $" + rounded_total_mcap + " | " topXstring += "Total market cap $" + rounded_total_mcap + " | "
except: except:
bot.say("Can't connect to coinmarketcap API") bot.say("Can't connect to coingecko API")
if not trigger.group(2): if not trigger.group(2):
limit = 20 limit = 20
else: else:
limit = int(trigger.group(2)) limit = int(trigger.group(2))
if limit > 20: if limit > 20:
bot.say("Too high! Max is 20!") bot.say("Too high! Max is 20!")
limit = 20
elif limit < 1: elif limit < 1:
bot.say("Dude...") bot.say("Dude...")
return
r = requests.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page={}&page=1&sparkline=false'.format(limit)) r = requests.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page={}&page=1&sparkline=false'.format(limit))
j = r.json() j = r.json()
for i in j: for i in j:
@ -442,21 +443,32 @@ 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: rounded_mcap = trim_mcap(market_cap_usd)
if market_cap_usd >= 1e10:
market_cap_short = int(int(round(market_cap_usd,-9))/int(1e9))
else:
market_cap_short = float(round(market_cap_usd,-8)/1e9)
rounded_mcap = str(market_cap_short)+"B"
else:
#rounded_mcap = "tiny"
market_cap_short = float(round(market_cap_usd,-5)/1e6)
rounded_mcap = str(market_cap_short)+"M"
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:
bot.say("The use is 'top' and then a digit 1 - 20") bot.say("The use is 'top' and then a digit 1 - 20")
def trim_mcap(val):
mcap = 0
magnitude_sym = "M"
if val >= 1e12: # >= 1T
magnitude_sym = "T"
if val >= 1e13:
mcap = round(val/float(1e12),1) # >= 10T show one decimal
else:
mcap = round(val/float(1e12),2) # 1T <= x < 10T show two decimals
elif val >= 1e9: # >= 1B
magnitude_sym = "B"
if val >= 1e11:
mcap = int(round(val/float(1e9),0)) # >= 100B show no decimals
else:
mcap = round(val/float(1e9),1) # 1B <= x < 100B show one decimal
else: # < 1B
# mcap = "tiny"
mcap = int(round(val/float(1e6),0)) # < 1B show no decimals
rounded_mcap = str(mcap) + magnitude_sym
return rounded_mcap
@sopel.module.commands('tall') @sopel.module.commands('tall')
def tall(bot, trigger): def tall(bot, trigger):