From 206fd96dcd4cd7e697168f2b5d49e61d854d0cc6 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 3 Aug 2023 19:23:22 +0800 Subject: [PATCH] human_formatize volume --- database_caller.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/database_caller.py b/database_caller.py index e1c793d..14da1b6 100755 --- a/database_caller.py +++ b/database_caller.py @@ -145,7 +145,16 @@ def getCookiesnCrumb(): with open('crumb.txt', 'w') as f: f.write(crumb) - + +def human_format(num): + num = float('{:.3g}'.format(num)) + magnitude = 0 + while abs(num) >= 1000: + magnitude += 1 + num /= 1000.0 + return '{}{}'.format('{:f}'.format(num).rstrip('0').rstrip('.'), ['', 'K', 'M', 'B', 'T'][magnitude]) + + def updateStocks(api_key, logf): try: @@ -177,7 +186,7 @@ def updateStocks(api_key, logf): if len(data) > 0: for symbol in symbols: try: - stock_info[data[symbol]['quote']['symbol']] = {'current': data[symbol]['quote']['latestPrice'], 'change': data[symbol]['quote']['change'], 'percent_change':data[symbol]['quote']['changePercent'] * 100, 'day_low':data[symbol]['quote']['low'], 'day_high':data[symbol]['quote']['high'], 'volume':data[symbol]['quote']['latestVolume']} + stock_info[data[symbol]['quote']['symbol']] = {'current': data[symbol]['quote']['latestPrice'], 'change': data[symbol]['quote']['change'], 'percent_change':data[symbol]['quote']['changePercent'] * 100, 'day_low':data[symbol]['quote']['low'], 'day_high':data[symbol]['quote']['high'], 'volume':human_format(data[symbol]['quote']['latestVolume'])} except: pass all_stocks_settings['symbols'] = stock_info @@ -216,7 +225,7 @@ def updateStocks(api_key, logf): try: for stock in data['quoteResponse']['result']: if stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B') == symbol: - stock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] = {'current': stock['regularMarketPrice'], 'change': stock['regularMarketChange'], 'percent_change':stock['regularMarketChangePercent'], 'day_low': stock['regularMarketDayLow'], 'day_high': stock['regularMarketDayHigh'], 'volume': stock['regularMarketVolume']} + stock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] = {'current': stock['regularMarketPrice'], 'change': stock['regularMarketChange'], 'percent_change':stock['regularMarketChangePercent'], 'day_low': stock['regularMarketDayLow'], 'day_high': stock['regularMarketDayHigh'], 'volume': human_format(stock['regularMarketVolume'])} except: pass all_stocks_settings['symbols'] = stock_info @@ -364,15 +373,6 @@ def updateCommodities(api_key, logf): #logf.write('. type: ' + str(exc_type)) #logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) #logf.close() - - -def human_format(num): - num = float('{:.3g}'.format(num)) - magnitude = 0 - while abs(num) >= 1000: - magnitude += 1 - num /= 1000.0 - return '{}{}'.format('{:f}'.format(num).rstrip('0').rstrip('.'), ['', 'K', 'M', 'B', 'T'][magnitude]) def updateMovies(api_key, logf): @@ -642,7 +642,7 @@ def updateCrypto(api_key, logf): if symbol.upper() + ',' + base.upper() == sb: - coin_info[symbol.upper() + ',' + base.upper()] = {'current': d['price'], '24hr_change': d['price_over_24hr'], 'percent_change': d['percent_over_24hr'], 'volume': d['volume'], 'day_low': d['day_low'], 'day_high': d['day_high']} + coin_info[symbol.upper() + ',' + base.upper()] = {'current': d['price'], '24hr_change': d['price_over_24hr'], 'percent_change': d['percent_over_24hr'], 'volume': human_format(d['volume']), 'day_low': d['day_low'], 'day_high': d['day_high']} all_crypto_settings['symbols'] = coin_info with open('csv/crypto_settings.json', 'w+') as f: