From 98dff26067c6f78a6182dc71c46b3a2270607124 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 8 Aug 2023 18:30:48 +0800 Subject: [PATCH] added market update --- database_caller.py | 89 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/database_caller.py b/database_caller.py index b9770ff..10f5aae 100755 --- a/database_caller.py +++ b/database_caller.py @@ -32,6 +32,7 @@ try: last_updates['stocks']['force'] = True last_updates['prepost']['force'] = True last_updates['sports_l']['force'] = True + last_updates['market']['force'] = True f = open('csv/last_updates.json', 'w') json.dump(last_updates, f) f.close() @@ -851,6 +852,71 @@ def updateJokes(api_key, logf): pass +def updateMarket(api_key, logf): + try: + try: + f = open('csv/market_settings.json', 'r') + all_market_settings = json.load(f) + f.close() + except: + all_market_settings = {"feature": "Gainers, Losers, Active", "speed": "medium", "speed2": "medium", "animation": "up", "percent": True, "point": True, "logos": True, "chart": False, "title": True, "lohivol": False, "categories": ["Top Gainers", "Top Losers", "Most Active"], "gainers": {}, "losers": {}, "mostactive": {}} + + 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://cloud.iexapis.com/stable/stock/market/overview?token=pk_aff870df1a984daa9dd43c71801c1936&' + data = requests.get(url, headers=headers).json() + + mostactive = data['mostactive'] + gainers = data['gainers'] + losers = data['losers'] + + try: + for item in losers: + all_market_settings['losers'][item['symbol']] = { + "current": str(item['latestPrice']), + "change": str(item['change']), + "percent_change": str(item['changePercent']), + "day_low": str(item['low']), + "day_high": str(item['high']), + "volume": human_format(item['volume']) + } + except: + pass + try: + for item in gainers: + all_market_settings['gainers'][item['symbol']] = { + "current": str(item['latestPrice']), + "change": str(item['change']), + "percent_change": str(item['changePercent']), + "day_low": str(item['low']), + "day_high": str(item['high']), + "volume": human_format(item['volume']) + } + except: + pass + try: + for item in mostactive: + all_market_settings['mostactive'][item['symbol']] = { + "current": str(item['latestPrice']), + "change": str(item['change']), + "percent_change": str(item['changePercent']), + "day_low": str(item['low']), + "day_high": str(item['high']), + "volume": human_format(item['volume']) + } + except: + pass + + with open('csv/market_settings.json', 'w+') as f: + json.dump(all_market_settings, f) + + # all_sector_settings['sectorPerformance'] = data['sectorPerformance'] + # with open('csv/sector_settings.json', 'w+') as f: + # json.dump(all_sector_settings, f) + except: + pass + + def updateNews(api_key, logf): try: try: @@ -1534,7 +1600,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} #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': 15} #minutes NY_zone = pytz.timezone('America/New_York') CET_zone = pytz.timezone('EST') @@ -1612,7 +1678,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}} + "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}} try: if last_updates['scheduler']['force']: @@ -1755,7 +1821,26 @@ if __name__ == '__main__': update_process.start() update_processes.append(update_process) + + # market + market_time = datetime.strptime(last_updates['market']['time'], "%d/%m/%Y %H:%M:%S") + NY_time = datetime.now(NY_zone).replace(tzinfo=None) + diff = (NY_time - market_time).total_seconds()/60 #minutes + + NY_time = datetime.now(NY_zone).replace(tzinfo=None) + opening = NY_time.replace(hour=9, minute=30, second=0, microsecond=0).replace(tzinfo=None) + closing = NY_time.replace(hour=16, minute=5, second=0, microsecond=0).replace(tzinfo=None) + stock_open = opening < NY_time < closing and datetime.today().weekday() <= 4 + + if last_updates['market']['force'] or (diff >= update_frequencies['market'] and stock_open):# or msg == 'c': + market_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") + last_updates['market']['time'] = market_time + last_updates['market']['force'] = False + update_process = Process(target = updateMarket, args = (api_key,logf)) + update_process.start() + update_processes.append(update_process) + # jokes jokes_time = datetime.strptime(last_updates['jokes']['time'], "%d/%m/%Y %H:%M:%S")