Skip to content

Commit

Permalink
Pass cookies to getcrumb (#143)
Browse files Browse the repository at this point in the history
* styke: added reason to error logging

* fix: updated header user-agent

* fix: pass current cookies to crumb request

* style: added error log after missing cookies
  • Loading branch information
iprak authored Nov 2, 2024
1 parent 2428493 commit 60450fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion custom_components/yahoofinance/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-encoding": "gzip,deflate,br,zstd",
"accept-language": "en-US,en;q=0.9",
"user-agent": "Mozilla/5.0 (Linux; Android) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.109 Safari/537.36 CrKey/1.54.248666",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
}
""" Headers for all other requests. """

Expand Down
21 changes: 18 additions & 3 deletions custom_components/yahoofinance/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ async def try_get_crumb_cookies(self) -> str | None:
)
return None

if self.cookies_missing():
_LOGGER.error("Attempting to get crumb but have no cookies")

await self.try_crumb_page()
return self.crumb

Expand All @@ -122,7 +125,10 @@ async def initial_navigation(self, url: str) -> ConsentData | None:

if response.status != HTTPStatus.OK:
_LOGGER.error(
"Failed to navigate to %s, status=%d", url, response.status
"Failed to navigate to %s, status=%d, reason=%s",
url,
response.status,
response.reason,
)
return None

Expand Down Expand Up @@ -170,7 +176,11 @@ async def process_consent(self, consent_data: ConsentData) -> bool:
# 200

if response.status != HTTPStatus.OK:
_LOGGER.error("Failed to post consent %d", response.status)
_LOGGER.error(
"Failed to post consent %d, reason=%s",
response.status,
response.reason,
)
return False

if response.cookies:
Expand Down Expand Up @@ -204,6 +214,7 @@ async def try_crumb_page(self) -> str | None:
GET_CRUMB_URL,
headers=REQUEST_HEADERS,
timeout=aiohttp.ClientTimeout(total=REQUEST_TIMEOUT),
cookies=self.cookies,
) as response:
_LOGGER.debug("Crumb response status: %d, %s", response.status, response)

Expand All @@ -215,7 +226,11 @@ async def try_crumb_page(self) -> str | None:
_LOGGER.debug("Crumb page reported %s", self.crumb)
return self.crumb

_LOGGER.error("Crumb request responded with status=%d", response.status)
_LOGGER.error(
"Crumb request responded with status=%d, reason=%s",
response.status,
response.reason,
)

if response.status == 429:
# Ideally we would want to use the seconds passed back in the header
Expand Down

0 comments on commit 60450fe

Please sign in to comment.