prepost db caller

This commit is contained in:
Justin 2023-03-29 16:46:13 +08:00 committed by GitHub
parent ea66a3ba8b
commit bece376294
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,6 +28,7 @@ try:
last_updates = json.load(f) last_updates = json.load(f)
f.close() f.close()
last_updates['stocks']['force'] = True last_updates['stocks']['force'] = True
last_updates['prepost']['force'] = True
last_updates['sports_l']['force'] = True last_updates['sports_l']['force'] = True
f = open('csv/last_updates.json', 'w') f = open('csv/last_updates.json', 'w')
json.dump(last_updates, f) json.dump(last_updates, f)
@ -112,6 +113,62 @@ 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.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
#logf.close() #logf.close()
def updateStocksPrePost(api_key, logf):
try:
f = open('csv/stocks_settings.json', 'r')
all_stocks_settings = json.load(f)
f.close()
stock_info = all_stocks_settings['symbols']
symbols = list(stock_info.keys())
prepost_url = 'https://query2.finance.yahoo.com/v7/finance/quote?symbols='
for symbol in symbols:
prepost_url += symbol + ','
prepost_url += '&fields=regularMarketPreviousClose,regularMarketPrice,preMarketPrice,preMarketChangePercent,regularMarketChangePercent,regularMarketChange,preMarketChange,postMarketPrice,postMarketChange,postMarketChangePercent&region=US&lang=en-US'
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}
prepost = requests.get(prepost_url, headers=headers)
prepost_data = prepost.json()['quoteResponse']['result']
time_now = datetime.now(pytz.timezone('America/New_York')).strftime("%H:%M EST")
if len(prepost_data) > 0:
for symbol in symbols:
for stock in prepost_data:
if stock['symbol'] == symbol:
stock_info[stock['symbol']] = {"time_now":time_now}
try:
stock_info[stock['symbol']]['Pre-market'] = {'preprice': '%.2f' % stock['preMarketPrice'],
'prechange': '%.2f' % stock['preMarketChange'],
'prepercent': '%.2f' % stock['preMarketChangePercent']}
except:
stock_info[stock['symbol']]['Pre-market'] = {'preprice': '%.2f' % stock['regularMarketPrice'],
'prechange': '%.2f' % 0,
'prepercent': '%.2f' % 0}
try:
stock_info[stock['symbol']]['Post-market'] = {'postprice': '%.2f' % stock['postMarketPrice'],
'postchange': '%.2f' % stock['postMarketChange'],
'postpercent': '%.2f' % stock['postMarketChangePercent']}
except:
stock_info[stock['symbol']]['Post-market'] = {'postprice': '%.2f' % stock['regularMarketPrice'],
'postchange': '%.2f' % 0,
'postpercent': '%.2f' % 0}
all_stocks_settings['symbols'] = stock_info
with open('csv/prepost_settings.json', 'w+') as f:
json.dump(all_stocks_settings['symbols'], f)
except:
pass
def updateCommodities(api_key, logf): def updateCommodities(api_key, logf):
try: try:
@ -1057,7 +1114,7 @@ if __name__ == '__main__':
update_frequencies = {'stocks':2, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440, 'commodities': 15, 'indices': 15, 'movies': 1440, 'ipo': 1440} #minutes update_frequencies = {'stocks':2, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440, 'commodities': 15, 'indices': 15, 'movies': 1440, 'ipo': 1440, 'prepost': 15} #minutes
NY_zone = pytz.timezone('America/New_York') NY_zone = pytz.timezone('America/New_York')
CET_zone = pytz.timezone('EST') CET_zone = pytz.timezone('EST')
@ -1166,6 +1223,33 @@ if __name__ == '__main__':
update_process = Process(target = updateStocks, args = (api_key,logf)) update_process = Process(target = updateStocks, args = (api_key,logf))
update_process.start() update_process.start()
update_processes.append(update_process) update_processes.append(update_process)
NY_time1 = datetime.now(NY_zone).replace(tzinfo=None)
NY_time2 = datetime.now(NY_zone).replace(tzinfo=None)
#prepost
preopen = NY_time1.replace(hour=4, minute=0, second=0, microsecond=0).replace(tzinfo=None)
preclose = NY_time1.replace(hour=9, minute=30, second=0, microsecond=0).replace(tzinfo=None)
postopen = NY_time2.replace(hour=16, minute=0, second=0, microsecond=0).replace(tzinfo=None)
postclose = NY_time2.replace(hour=20, minute=20, second=0, microsecond=0).replace(tzinfo=None)
prepost_frequency = update_frequencies['prepost']
prepost_time = datetime.strptime(last_updates['prepost']['time'], "%d/%m/%Y %H:%M:%S")
pre_open = preopen < NY_time1 < preclose and NY_time1.weekday() <= 4
post_open = postopen < NY_time2 < postclose and NY_time2.weekday() <= 4
diff1 = (NY_time1 - prepost_time).total_seconds()/60 #minutes
diff2 = (NY_time2 - prepost_time).total_seconds()/60 #minutes
if (last_updates['prepost']['force']) or (diff1 >= update_frequencies['prepost'] and pre_open) or (diff2 >= update_frequencies['prepost'] and post_open):
prepost_time = NY_time1.strftime("%d/%m/%Y %H:%M:%S")
last_updates['prepost']['time'] = prepost_time
last_updates['prepost']['force'] = False
update_process = Process(target = updateStocksPrePost, args = (api_key,logf))
update_process.start()
update_processes.append(update_process)
# crypto # crypto
crypto_time = datetime.strptime(last_updates['crypto']['time'], "%d/%m/%Y %H:%M:%S") crypto_time = datetime.strptime(last_updates['crypto']['time'], "%d/%m/%Y %H:%M:%S")