added indices to db caller

This commit is contained in:
Justin 2022-10-06 13:00:59 +08:00 committed by GitHub
parent aed587014f
commit a056e908a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -140,6 +140,54 @@ def updateCommodities(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 updateIndices(api_key, logf):
try:
f = open('csv/indices_settings.json', 'r')
all_indices_settings = json.load(f)
f.close()
index_info = all_indices_settings['symbols']
symbols = list(index_info.keys())
url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/indices?symbols='
for symbol in symbols:
url += symbol + ','
url += '&apiKey=' + api_key
response = requests.get(url)
data = response.json()
index_info = {}
if len(data) > 0:
for symbol in symbols:
for index in data:
if index['symbol'] == symbol:
index_info[index['symbol']] = {'name': index['name'], 'current': index['price'], 'point_change': index['change'], 'percent_change': index['percent_change']}
all_indices_settings['symbols'] = index_info
f = open('csv/indices_settings.json', 'w+')
json.dump(all_indices_settings, f)
f.close()
except Exception as e:
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()
def updateCrypto(api_key, logf):
@ -651,7 +699,7 @@ if __name__ == '__main__':
update_frequencies = {'stocks':2, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440, 'commodities': 15} #minutes
update_frequencies = {'stocks':2, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440, 'commodities': 15, 'indices': 15} #minutes
NY_zone = pytz.timezone('America/New_York')
CET_zone = pytz.timezone('EST')
@ -722,7 +770,7 @@ if __name__ == '__main__':
"news": {"time": "06/03/2022 04:07:09", "force": True}, "weather": {"time": "06/03/2022 04:08:20", "force": True},
"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}}
"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}}
@ -792,6 +840,23 @@ if __name__ == '__main__':
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")
last_updates['indices']['time'] = indices_time
last_updates['indices']['force'] = False
update_process = Process(target = updateIndices, args = (api_key,logf))
update_process.start()
update_processes.append(update_process)
# weather
weather_time = datetime.strptime(last_updates['weather']['time'], "%d/%m/%Y %H:%M:%S")