udpate global stocks

This commit is contained in:
Justin 2023-09-26 16:12:54 +08:00 committed by GitHub
parent 34a5e789b5
commit 7409bbb286
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -332,6 +332,70 @@ def updateStocksPrePost(api_key, logf):
pass
def updateGlobalStocks(api_key, logf):
try:
try:
f = open('csv/globalstocks_settings.json', 'r')
all_globalstocks_settings = json.load(f)
f.close()
except:
all_globalstocks_settings = {"feature": "Global Stocks", "speed": "medium", "speed2": "medium", "animation": "continuous", "percent": True, "point": True, "logos": True, "chart": False, "title": True, "lohivol": True, "display_name": False, "symbols": {}}
globalstock_info = all_globalstocks_settings['symbols']
symbols = list(globalstock_info.keys())
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://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/stocks?symbols='
url = 'https://query1.finance.yahoo.com/v7/finance/quote?fields=longName,regularMarketPrice,regularMarketChangePercent,regularMarketChange,regularMarketDayHigh,regularMarketDayLow,regularMarketVolume&region=US&lang=en-US&symbols='
for symbol in symbols:
url += symbol + ','
# url += '&apiKey=' + api_key
url = url.replace('BRK.A', 'BRK-A').replace('BRK.B', 'BRK-B')
# response = requests.get(url, headers=headers)
# data = response.json()
# if "'error': {'code'" in str(data):
while True:
try:
with open('session.txt', 'rb') as f:
session = pickle.load(f)
with open('crumb.txt', 'r') as f:
crumb = f.read()
except:
getCookiesnCrumb()
with open('session.txt', 'rb') as f:
session = pickle.load(f)
with open('crumb.txt', 'r') as f:
crumb = f.read()
params = {'crumb': crumb}
data = session.get(url, headers=headers, params=params).json()
if "'error': {'code'" not in str(data):
break
else:
getCookiesnCrumb()
time.sleep(5)
# globalstock_info = {}
if len(data) > 0:
for symbol in symbols:
try:
for stock in data['quoteResponse']['result']:
if stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B') == symbol and 'name' in globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] and globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')]['name'] != '':
globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] = {'current': str(stock['regularMarketPrice']), 'change': str(stock['regularMarketChange']), 'percent_change': str(stock['regularMarketChangePercent']), 'day_low': str(stock['regularMarketDayLow']), 'day_high': str(stock['regularMarketDayHigh']), 'volume': str(human_format(stock['regularMarketVolume'])), 'name': globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')]['name']}
elif stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B') == symbol and 'name' not in globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')]:
globalstock_info[stock['symbol'].replace('BRK-A', 'BRK.A').replace('BRK-B', 'BRK.B')] = {'current': str(stock['regularMarketPrice']), 'change': str(stock['regularMarketChange']), 'percent_change': str(stock['regularMarketChangePercent']), 'day_low': str(stock['regularMarketDayLow']), 'day_high': str(stock['regularMarketDayHigh']), 'volume': str(human_format(stock['regularMarketVolume'])), 'name': stock['longName'].replace(',','').replace('Inc.','Inc').replace('Corporation', 'Corp').replace('Ltd.', 'Ltd').replace('Limited','Ltd')}
except:
pass
all_globalstocks_settings['symbols'] = globalstock_info
with open('csv/globalstocks_settings.json', 'w+') as f:
json.dump(all_globalstocks_settings, f)
except:
pass
def updateCommodities(api_key, logf):
try:
@ -1655,7 +1719,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, 'market': 5, 'quotes': 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': 5, 'quotes': 15, 'globalstocks': 15} #minutes
NY_zone = pytz.timezone('America/New_York')
CET_zone = pytz.timezone('EST')
@ -1733,7 +1797,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}, "market": {"time": "06/03/2022 04:10:09", "force": True}, "sector": {"time": "06/03/2022 04:10:09", "force": True}, "quotes": {"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}, "sector": {"time": "06/03/2022 04:10:09", "force": True}, "quotes": {"time": "06/03/2022 04:10:09", "force": True}, "globalstocks": {"time": "06/03/2022 04:10:09", "force": True}}
try:
if last_updates['scheduler']['force']:
@ -1928,6 +1992,20 @@ if __name__ == '__main__':
update_processes.append(update_process)
# global stocks
globalstocks_time = datetime.strptime(last_updates['globalstocks']['time'], "%d/%m/%Y %H:%M:%S")
NY_time = datetime.now(NY_zone).replace(tzinfo=None)
diff = (NY_time - globalstocks_time).total_seconds()/60 #minutes
if last_updates['globalstocks']['force'] or diff >= update_frequencies['globalstocks']:# or msg == 'c':
globalstocks_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
last_updates['globalstocks']['time'] = globalstocks_time
last_updates['globalstocks']['force'] = False
update_process = Process(target = updateGlobalStocks, 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")