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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -166,7 +166,7 @@ def updateStocksPrePost(api_key, logf):
symbols = list(stock_info.keys())
#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:
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',
'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:
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}
prepost = requests.get(prepost_url, headers=headers).json()
if "'error': {'code'" in str(prepost):
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}
prepost = session.get(prepost_url, headers=headers, params=params).json()
if 'Unauthorized' not in str(prepost):
break
else:
getCookiesnCrumb()
time.sleep(2)
prepost = session.get(prepost_url.replace('v6','v7'), headers=headers, params=params).json()
# prepost = requests.get(prepost_url, headers=headers)
# if 'Unauthorized' in str(prepost.json()):
# prepost = requests.get(prepost_url.replace('v7','v6'), headers=headers)
if "'error': {'code'" not in str(prepost):
break
else:
getCookiesnCrumb()
time.sleep(5)
prepost_data = prepost['quoteResponse']['result']
time_now = datetime.now(pytz.timezone('America/New_York')).strftime("%H:%M EST")