get cookies and crumb for yahoo finance prepost data
workaround for their security measures implemented recently with the v7 api
This commit is contained in:
parent
a96e9b9657
commit
3ca67b9a4d
@ -139,10 +139,26 @@ def updateStocks(api_key, logf):
|
||||
#logf.close()
|
||||
|
||||
|
||||
def getCookiesnCrumb():
|
||||
|
||||
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'}
|
||||
cookie_url = 'https://finance.yahoo.com'
|
||||
crumb_url = 'https://query1.finance.yahoo.com/v1/test/getcrumb'
|
||||
|
||||
session = requests.Session()
|
||||
session.get(cookie_url, headers=headers)
|
||||
crumb = session.get(crumb_url, headers=headers).content.decode('utf-8')
|
||||
|
||||
with open('session.txt', 'wb') as f:
|
||||
pickle.dump(session, f)
|
||||
with open('crumb.txt', 'w') as f:
|
||||
f.write(crumb)
|
||||
|
||||
|
||||
def updateStocksPrePost(api_key, logf):
|
||||
|
||||
|
||||
try:
|
||||
|
||||
f = open('csv/stocks_settings.json', 'r')
|
||||
all_stocks_settings = json.load(f)
|
||||
f.close()
|
||||
@ -156,15 +172,36 @@ def updateStocksPrePost(api_key, logf):
|
||||
|
||||
prepost_url += '&fields=regularMarketPreviousClose,regularMarketPrice,preMarketPrice,preMarketChangePercent,regularMarketChangePercent,regularMarketChange,preMarketChange,postMarketPrice,postMarketChange,postMarketChangePercent®ion=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)
|
||||
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 = session.get(prepost_url, headers=headers, params=params).json()
|
||||
|
||||
if 'Unauthorized' not in str(prepost):
|
||||
break
|
||||
else:
|
||||
getCookiesnCrumb()
|
||||
time.sleep(2)
|
||||
|
||||
# prepost = requests.get(prepost_url, headers=headers)
|
||||
# if 'Unauthorized' in str(prepost.json()):
|
||||
# prepost = requests.get(prepost_url.replace('v7','v6'), headers=headers)
|
||||
|
||||
if 'Unauthorized' in str(prepost.json()):
|
||||
prepost = requests.get(prepost_url.replace('v7','v6'), headers=headers)
|
||||
|
||||
prepost_data = prepost.json()['quoteResponse']['result']
|
||||
prepost_data = prepost['quoteResponse']['result']
|
||||
time_now = datetime.now(pytz.timezone('America/New_York')).strftime("%H:%M EST")
|
||||
|
||||
if len(prepost_data) > 0:
|
||||
|
Loading…
Reference in New Issue
Block a user