Skip to content

Commit

Permalink
Merge pull request #76 from xinluh/patch-1
Browse files Browse the repository at this point in the history
Fix Yahoo source invalid cookie / invalid crumb for v7 endpoint
  • Loading branch information
blais authored Jun 29, 2023
2 parents b761102 + 56e906c commit 2122268
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion beanprice/sources/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,21 @@ class Source(source.Source):
def get_latest_price(self, ticker: str) -> Optional[source.SourcePrice]:
"""See contract in beanprice.source.Source."""

session = requests.Session()
session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0'})
session.get('https://fc.yahoo.com') # This populates the correct cookies in the session
crumb = session.get('https://query1.finance.yahoo.com/v1/test/getcrumb').text

url = "https://query1.finance.yahoo.com/v7/finance/quote"
fields = ['symbol', 'regularMarketPrice', 'regularMarketTime']
payload = {
'symbols': ticker,
'fields': ','.join(fields),
'exchange': 'NYSE',
'crumb': crumb,
}
payload.update(_DEFAULT_PARAMS)
response = requests.get(url, params=payload, headers={'User-Agent': None})
response = session.get(url, params=payload)
try:
result = parse_response(response)
except YahooError as error:
Expand Down

0 comments on commit 2122268

Please sign in to comment.