changed it a bit to keep v6 api, but if v6 don't work, use the v7 workaround

This commit is contained in:
Justin
2023-05-24 19:19:39 +08:00
committed by GitHub
parent 3ca67b9a4d
commit b94de007f9

View File

@@ -166,7 +166,7 @@ def updateStocksPrePost(api_key, logf):
symbols = list(stock_info.keys()) symbols = list(stock_info.keys())
#KEEP THIS JUST IN CASE V7 GOES DOWN prepost_url = 'https://query2.finance.yahoo.com/v6/finance/quote?symbols=' #KEEP THIS JUST IN CASE V7 GOES DOWN prepost_url = 'https://query2.finance.yahoo.com/v6/finance/quote?symbols='
prepost_url = 'https://query2.finance.yahoo.com/v7/finance/quote?symbols=' prepost_url = 'https://query2.finance.yahoo.com/v6/finance/quote?symbols='
for symbol in symbols: for symbol in symbols:
prepost_url += symbol + ',' prepost_url += symbol + ','
@@ -175,31 +175,29 @@ def updateStocksPrePost(api_key, logf):
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', 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'} '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'}
while True: prepost = requests.get(prepost_url, headers=headers).json()
try: if "'error': {'code'" in str(prepost):
with open('session.txt', 'rb') as f: while True:
session = pickle.load(f) try:
with open('crumb.txt', 'r') as f: with open('session.txt', 'rb') as f:
crumb = f.read() session = pickle.load(f)
except: with open('crumb.txt', 'r') as f:
getCookiesnCrumb() crumb = f.read()
with open('session.txt', 'rb') as f: except:
session = pickle.load(f) getCookiesnCrumb()
with open('crumb.txt', 'r') as f: with open('session.txt', 'rb') as f:
crumb = f.read() session = pickle.load(f)
params = {'crumb': crumb} with open('crumb.txt', 'r') as f:
crumb = f.read()
params = {'crumb': crumb}
prepost = session.get(prepost_url, headers=headers, params=params).json() prepost = session.get(prepost_url.replace('v6','v7'), headers=headers, params=params).json()
if 'Unauthorized' not in str(prepost): if "'error': {'code'" not in str(prepost):
break break
else: else:
getCookiesnCrumb() getCookiesnCrumb()
time.sleep(2) time.sleep(5)
# prepost = requests.get(prepost_url, headers=headers)
# if 'Unauthorized' in str(prepost.json()):
# prepost = requests.get(prepost_url.replace('v7','v6'), headers=headers)
prepost_data = prepost['quoteResponse']['result'] prepost_data = prepost['quoteResponse']['result']
time_now = datetime.now(pytz.timezone('America/New_York')).strftime("%H:%M EST") time_now = datetime.now(pytz.timezone('America/New_York')).strftime("%H:%M EST")