From 7409bbb28666e1acb5bab911bfb1c5cf7c880f2d Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 26 Sep 2023 16:12:54 +0800 Subject: [PATCH] udpate global stocks --- database_caller.py | 86 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/database_caller.py b/database_caller.py index ebb681b..73f2943 100755 --- a/database_caller.py +++ b/database_caller.py @@ -330,7 +330,71 @@ def updateStocksPrePost(api_key, logf): json.dump(all_stocks_settings['symbols'], f) except: pass - + + +def updateGlobalStocks(api_key, logf): + + try: + try: + f = open('csv/globalstocks_settings.json', 'r') + all_globalstocks_settings = json.load(f) + f.close() + except: + all_globalstocks_settings = {"feature": "Global Stocks", "speed": "medium", "speed2": "medium", "animation": "continuous", "percent": True, "point": True, "logos": True, "chart": False, "title": True, "lohivol": True, "display_name": False, "symbols": {}} + + globalstock_info = all_globalstocks_settings['symbols'] + symbols = list(globalstock_info.keys()) + + headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7', + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'} + + # url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/stocks?symbols=' + url = 'https://query1.finance.yahoo.com/v7/finance/quote?fields=longName,regularMarketPrice,regularMarketChangePercent,regularMarketChange,regularMarketDayHigh,regularMarketDayLow,regularMarketVolume®ion=US&lang=en-US&symbols=' + + for symbol in symbols: + url += symbol + ',' + # url += '&apiKey=' + api_key + + url = url.replace('BRK.A', 'BRK-A').replace('BRK.B', 'BRK-B') + # response = requests.get(url, headers=headers) + # data = response.json() + # if "'error': {'code'" in str(data): + while True: + try: + with open('session.txt', 'rb') as f: + session = pickle.load(f) + with open('crumb.txt', 'r') as f: + crumb = f.read() + except: + getCookiesnCrumb() + with open('session.txt', 'rb') as f: + session = pickle.load(f) + with open('crumb.txt', 'r') as f: + crumb = f.read() + params = {'crumb': crumb} + data = session.get(url, headers=headers, params=params).json() + if "'error': {'code'" not in str(data): + break + else: + getCookiesnCrumb() + time.sleep(5) + # globalstock_info = {} + if len(data) > 0: + for symbol in symbols: + try: + for stock in data['quoteResponse']['result']: + if stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B') == symbol and 'name' in globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] and globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')]['name'] != '': + globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] = {'current': str(stock['regularMarketPrice']), 'change': str(stock['regularMarketChange']), 'percent_change': str(stock['regularMarketChangePercent']), 'day_low': str(stock['regularMarketDayLow']), 'day_high': str(stock['regularMarketDayHigh']), 'volume': str(human_format(stock['regularMarketVolume'])), 'name': globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')]['name']} + elif stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B') == symbol and 'name' not in globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')]: + globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] = {'current': str(stock['regularMarketPrice']), 'change': str(stock['regularMarketChange']), 'percent_change': str(stock['regularMarketChangePercent']), 'day_low': str(stock['regularMarketDayLow']), 'day_high': str(stock['regularMarketDayHigh']), 'volume': str(human_format(stock['regularMarketVolume'])), 'name': stock['longName'].replace(',','').replace('Inc.','Inc').replace('Corporation', 'Corp').replace('Ltd.', 'Ltd').replace('Limited','Ltd')} + except: + pass + all_globalstocks_settings['symbols'] = globalstock_info + with open('csv/globalstocks_settings.json', 'w+') as f: + json.dump(all_globalstocks_settings, f) + except: + pass + def updateCommodities(api_key, logf): @@ -1655,7 +1719,7 @@ if __name__ == '__main__': t = time.time() - update_frequencies = {'stocks':2, 'crypto':7, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440, 'commodities': 15, 'indices': 15, 'movies': 1440, 'ipo': 1440, 'prepost': 15, 'economic': 15, 'jokes': 15, 'market': 5, 'quotes': 15} #minutes + update_frequencies = {'stocks':2, 'crypto':7, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440, 'commodities': 15, 'indices': 15, 'movies': 1440, 'ipo': 1440, 'prepost': 15, 'economic': 15, 'jokes': 15, 'market': 5, 'quotes': 15, 'globalstocks': 15} #minutes NY_zone = pytz.timezone('America/New_York') CET_zone = pytz.timezone('EST') @@ -1733,7 +1797,7 @@ if __name__ == '__main__': "forex": {"time": "06/03/2022 03:54:02", "force": True}, "sports_l": {"time": "06/03/2022 04:10:09", "force": True}, "sports_p": {"time": "06/03/2022 04:10:09", "force": True}, "sports_u": {"time": "06/03/2022 04:10:09", "force": True},"sports_t": {"time": "06/03/2022 04:10:09", "force": True}, "commodities": {"time": "06/03/2022 04:10:09", "force": True}, "indices": {"time": "06/03/2022 04:10:09", "force": True}, "movies": {"time": "06/03/2022 04:10:09", "force": True}, "ipo": {"time": "06/03/2022 04:10:09", "force": True}, - "prepost": {"time": "06/03/2022 04:10:09", "force": True}, "economic": {"time": "06/03/2022 04:10:09", "force": True}, "jokes": {"time": "06/03/2022 04:10:09", "force": True}, "market": {"time": "06/03/2022 04:10:09", "force": True}, "sector": {"time": "06/03/2022 04:10:09", "force": True}, "quotes": {"time": "06/03/2022 04:10:09", "force": True}} + "prepost": {"time": "06/03/2022 04:10:09", "force": True}, "economic": {"time": "06/03/2022 04:10:09", "force": True}, "jokes": {"time": "06/03/2022 04:10:09", "force": True}, "market": {"time": "06/03/2022 04:10:09", "force": True}, "sector": {"time": "06/03/2022 04:10:09", "force": True}, "quotes": {"time": "06/03/2022 04:10:09", "force": True}, "globalstocks": {"time": "06/03/2022 04:10:09", "force": True}} try: if last_updates['scheduler']['force']: @@ -1926,7 +1990,21 @@ if __name__ == '__main__': update_process = Process(target = updateQuotes, args = (api_key,logf)) update_process.start() update_processes.append(update_process) - + + + # global stocks + globalstocks_time = datetime.strptime(last_updates['globalstocks']['time'], "%d/%m/%Y %H:%M:%S") + + NY_time = datetime.now(NY_zone).replace(tzinfo=None) + diff = (NY_time - globalstocks_time).total_seconds()/60 #minutes + + if last_updates['globalstocks']['force'] or diff >= update_frequencies['globalstocks']:# or msg == 'c': + globalstocks_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") + last_updates['globalstocks']['time'] = globalstocks_time + last_updates['globalstocks']['force'] = False + update_process = Process(target = updateGlobalStocks, args = (api_key,logf)) + update_process.start() + update_processes.append(update_process) # indices indices_time = datetime.strptime(last_updates['indices']['time'], "%d/%m/%Y %H:%M:%S")