added commodities to db caller

This commit is contained in:
Justin 2022-06-13 01:19:14 +08:00 committed by GitHub
parent 2cf0d7fd55
commit ed67a21908
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -95,6 +95,50 @@ def updateStocks(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 updateCommodities(api_key, logf):
try:
f = open('csv/commodities_settings.json', 'r')
all_commodities_settings = json.load(f)
f.close()
commodity_info = all_commodities_settings['symbols']
symbols = list(commodity_info.keys())
url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/commodities?symbols='
for symbol in symbols:
url += symbol + ','
url += '&apiKey=' + api_key
response = requests.get(url)
data = response.json()
commodity_info = {}
if len(data) > 0:
for symbol in symbols:
for commodity in data:
if commodity['symbol'] == symbol:
commodity_info[commodity['symbol']] = {'current': commodity['price'], 'unit': commodity['unit'], '24hr_change': commodity['price_over_24hr'], 'percent_change': commodity['percent_over_24hr']}
all_commodities_settings['symbols'] = commodity_info
f = open('csv/commodities_settings.json', 'w+')
json.dump(all_commodities_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):
@ -607,7 +651,7 @@ if __name__ == '__main__':
update_frequencies = {'stocks':2, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440} #minutes
update_frequencies = {'stocks':2, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440, 'commodities': 15} #minutes
NY_zone = pytz.timezone('America/New_York')
CET_zone = pytz.timezone('EST')
@ -729,6 +773,24 @@ if __name__ == '__main__':
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)
# weather
weather_time = datetime.strptime(last_updates['weather']['time'], "%d/%m/%Y %H:%M:%S")