From b212831237ba3befbe9984cee8e710ad904bff43 Mon Sep 17 00:00:00 2001 From: scoobybejesus <21372487+scoobybejesus@users.noreply.github.com> Date: Wed, 6 Jan 2021 18:25:44 -0500 Subject: [PATCH 1/3] 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. --- price.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/price.py b/price.py index e79b526..679ace5 100644 --- a/price.py +++ b/price.py @@ -418,12 +418,21 @@ def top(bot, trigger): topXstring = "" try: try: - r = requests.get('https://api.coingecko.com/api/v3/global') - j = r.json() - 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 = str(total_mcap_short)+"B" - topXstring += "Total market cap $" + rounded_total_mcap + " | " + r = requests.get('https://api.coingecko.com/api/v3/global') + j = r.json() + usd_total_mkt_cap = float(j['data']['total_market_cap']['usd']) + total_mcap_short = 0 + magnitude_sym = "M" + 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: bot.say("Can't connect to coinmarketcap API") if not trigger.group(2): @@ -442,16 +451,18 @@ def top(bot, trigger): rank = i['market_cap_rank'] price_usd = float(i['current_price']) market_cap_usd = float(i['market_cap']) - if market_cap_usd >= 1e9: - 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" + mcap_short = 0 + magnitude_sym = "M" + if market_cap_usd >= 1e12: + mcap_short = round(market_cap_usd/float(1e12),2) + magnitude_sym = "T" + elif market_cap_usd >= 1e9: + mcap_short = round(market_cap_usd/float(1e9),1) + magnitude_sym = "B" else: - #rounded_mcap = "tiny" - market_cap_short = float(round(market_cap_usd,-5)/1e6) - rounded_mcap = str(market_cap_short)+"M" + #mcap_short = "tiny" + mcap_short = round(market_cap_usd/float(1e6),0) + rounded_mcap = str(mcap_short)+magnitude_sym topXstring += "{0}. {1} ${2} | ".format(rank, symbol, rounded_mcap) #TODO: add price_usd, rounded bot.say(topXstring[:-2]) except: From a1dc88b16b0f9efe105e2521293a0de3c601b891 Mon Sep 17 00:00:00 2001 From: scoobybejesus <21372487+scoobybejesus@users.noreply.github.com> Date: Wed, 6 Jan 2021 21:44:53 -0500 Subject: [PATCH 2/3] Update price.py Extract and add to mcap rounding function. Minor other changes to top(). --- price.py | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/price.py b/price.py index 679ace5..e9189ab 100644 --- a/price.py +++ b/price.py @@ -421,28 +421,20 @@ def top(bot, trigger): r = requests.get('https://api.coingecko.com/api/v3/global') j = r.json() usd_total_mkt_cap = float(j['data']['total_market_cap']['usd']) - total_mcap_short = 0 - magnitude_sym = "M" - 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 + rounded_total_mcap = trim_mcap(usd_total_mkt_cap) topXstring += "Total market cap $" + rounded_total_mcap + " | " except: - bot.say("Can't connect to coinmarketcap API") + bot.say("Can't connect to coingecko API") if not trigger.group(2): limit = 20 else: limit = int(trigger.group(2)) if limit > 20: bot.say("Too high! Max is 20!") + limit = 20 elif limit < 1: 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)) j = r.json() for i in j: @@ -451,23 +443,32 @@ def top(bot, trigger): rank = i['market_cap_rank'] price_usd = float(i['current_price']) market_cap_usd = float(i['market_cap']) - mcap_short = 0 - magnitude_sym = "M" - if market_cap_usd >= 1e12: - mcap_short = round(market_cap_usd/float(1e12),2) - magnitude_sym = "T" - elif market_cap_usd >= 1e9: - mcap_short = round(market_cap_usd/float(1e9),1) - magnitude_sym = "B" - else: - #mcap_short = "tiny" - mcap_short = round(market_cap_usd/float(1e6),0) - rounded_mcap = str(mcap_short)+magnitude_sym + rounded_mcap = trim_mcap(market_cap_usd) topXstring += "{0}. {1} ${2} | ".format(rank, symbol, rounded_mcap) #TODO: add price_usd, rounded bot.say(topXstring[:-2]) except: 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') def tall(bot, trigger): From c8ef41eaf0478b9c222ffc911f678812850c06a2 Mon Sep 17 00:00:00 2001 From: scoobybejesus <21372487+scoobybejesus@users.noreply.github.com> Date: Wed, 6 Jan 2021 21:48:36 -0500 Subject: [PATCH 3/3] Update price.py Removed two tabs that snuck in and replaced with spaces --- price.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/price.py b/price.py index e9189ab..a8e4402 100644 --- a/price.py +++ b/price.py @@ -431,10 +431,10 @@ def top(bot, trigger): limit = int(trigger.group(2)) if limit > 20: bot.say("Too high! Max is 20!") - limit = 20 + limit = 20 elif limit < 1: bot.say("Dude...") - return + 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)) j = r.json() for i in j: