From b40842e040880969fd72169816ae1ae07dccf5e4 Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 14 Jun 2023 16:44:37 +0800 Subject: [PATCH] economic settings db caller (WIP) --- database_caller.py | 104 ++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 59 deletions(-) diff --git a/database_caller.py b/database_caller.py index 6480061..96a4080 100755 --- a/database_caller.py +++ b/database_caller.py @@ -591,12 +591,8 @@ def updateIndices(api_key, logf): def updateCrypto(api_key, logf): - - try: - - f = open('csv/crypto_settings.json', 'r') all_crypto_settings = json.load(f) f.close() @@ -618,8 +614,6 @@ def updateCrypto(api_key, logf): response = requests.get(url) data = response.json() - - coin_info = {} if len(data) > 0: for sb in symbol_base: @@ -639,7 +633,6 @@ def updateCrypto(api_key, logf): except: pass - #logf = open('log.txt', "a") #exc_type, exc_obj, exc_tb = sys.exc_info() #fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] @@ -651,9 +644,7 @@ def updateCrypto(api_key, logf): #logf.close() def updateForex(api_key, logf): - - try: f = open('csv/forex_settings.json', 'r') all_forex_settings = json.load(f) @@ -670,8 +661,7 @@ def updateForex(api_key, logf): targets = ','.join(symbols) url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/forex?symbols=' - - + for i,s in enumerate(symbols): url += s + '-' + bases[i] + ',' url = url[:-1] #remove last comma @@ -679,8 +669,7 @@ def updateForex(api_key, logf): response = requests.get(url) data = response.json() - - + if len(data) > 0: c_dict = {} @@ -689,9 +678,6 @@ def updateForex(api_key, logf): if d['uid'].replace('/',',') == sb: c_dict[d['uid'].replace('/',',')] = {'current': d['rate'], '24hr_change': d['rate_over_24hr'], 'percent_change':d['percent_over_24hr']} - - - all_forex_settings['symbols'] = c_dict f = open( "csv/forex_settings.json", 'w+' ) @@ -709,9 +695,12 @@ def updateForex(api_key, logf): #logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) #logf.close() + +def updateEconomic(api_key, logf): + + def updateNews(api_key, logf): - try: f = open('csv/news_settings.json', 'r') all_settings = json.load(f) @@ -1386,11 +1375,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} #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} #minutes NY_zone = pytz.timezone('America/New_York') CET_zone = pytz.timezone('EST') @@ -1417,15 +1402,15 @@ if __name__ == '__main__': weather_key = api_keys[1].strip() except Exception as e: weather_key = False - logf = open('log.txt', "a") - exc_type, exc_obj, exc_tb = sys.exc_info() - fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] - logf.write(str(e)) - logf.write('. file: ' + fname) - logf.write('. line: ' + str(exc_tb.tb_lineno)) - 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() +# logf = open('log.txt', "a") +# exc_type, exc_obj, exc_tb = sys.exc_info() +# fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] +# logf.write(str(e)) +# logf.write('. file: ' + fname) +# logf.write('. line: ' + str(exc_tb.tb_lineno)) +# 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() try: movie_key = open('movie_api_key.txt').readlines()[0] @@ -1457,7 +1442,6 @@ if __name__ == '__main__': try: while True: - try: f = open('csv/last_updates.json', 'r') last_updates = json.load(f) @@ -1516,8 +1500,6 @@ if __name__ == '__main__': #msg = getInput() - - NY_time = datetime.now(NY_zone).replace(tzinfo=None) #stocks @@ -1568,50 +1550,58 @@ if __name__ == '__main__': # crypto crypto_time = datetime.strptime(last_updates['crypto']['time'], "%d/%m/%Y %H:%M:%S") - NY_time = datetime.now(NY_zone).replace(tzinfo=None) diff = (NY_time - crypto_time).total_seconds()/60 #minutes - - + if last_updates['crypto']['force'] or diff >= update_frequencies['crypto']:# or msg == 'c': crypto_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") #updateCrypto(api_key, logf) - - last_updates['crypto']['time'] = crypto_time last_updates['crypto']['force'] = False update_process = Process(target = updateCrypto, args = (api_key,logf)) update_process.start() update_processes.append(update_process) + # commodities commodities_time = datetime.strptime(last_updates['commodities']['time'], "%d/%m/%Y %H:%M:%S") - - + NY_time = datetime.now(NY_zone).replace(tzinfo=None) diff = (NY_time - commodities_time).total_seconds()/60 #minutes - - + if last_updates['commodities']['force'] or diff >= update_frequencies['commodities']:# or msg == 'c': commodities_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") #updateCrypto(api_key, logf) - - + last_updates['commodities']['time'] = commodities_time last_updates['commodities']['force'] = False update_process = Process(target = updateCommodities, args = (api_key,logf)) update_process.start() update_processes.append(update_process) + + + # economic calendar + economic_time = datetime.strptime(last_updates['economic']['time'], "%d/%m/%Y %H:%M:%S") + + NY_time = datetime.now(NY_zone).replace(tzinfo=None) + diff = (NY_time - economic_time).total_seconds()/60 #minutes + + if last_updates['economic']['force'] or diff >= update_frequencies['economic']:# or msg == 'c': + economic_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") + last_updates['economic']['time'] = economic_time + last_updates['economic']['force'] = False + update_process = Process(target = updateEconomic, 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") - - + NY_time = datetime.now(NY_zone).replace(tzinfo=None) diff = (NY_time - indices_time).total_seconds()/60 #minutes - - + if last_updates['indices']['force'] or diff >= update_frequencies['indices']:# or msg == 'c': indices_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") @@ -1621,14 +1611,13 @@ if __name__ == '__main__': update_process.start() update_processes.append(update_process) + # movies movies_time = datetime.strptime(last_updates['movies']['time'], "%d/%m/%Y %H:%M:%S") - - + NY_time = datetime.now(NY_zone).replace(tzinfo=None) diff = (NY_time - movies_time).total_seconds()/60 #minutes - - + if last_updates['movies']['force'] or diff >= update_frequencies['movies']: movies_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") last_updates['movies']['time'] = movies_time @@ -1640,12 +1629,10 @@ if __name__ == '__main__': # ipos ipo_time = datetime.strptime(last_updates['ipo']['time'], "%d/%m/%Y %H:%M:%S") - - + NY_time = datetime.now(NY_zone).replace(tzinfo=None) diff = (NY_time - ipo_time).total_seconds()/60 #minutes - - + if last_updates['ipo']['force'] or diff >= update_frequencies['ipo']: ipo_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") last_updates['ipo']['time'] = ipo_time @@ -1657,8 +1644,7 @@ if __name__ == '__main__': # weather weather_time = datetime.strptime(last_updates['weather']['time'], "%d/%m/%Y %H:%M:%S") - - + NY_time = datetime.now(NY_zone).replace(tzinfo=None) diff = (NY_time - weather_time).total_seconds()/60 #minutes if last_updates['weather']['force'] or diff >= update_frequencies['weather']:# or msg == 'w':