Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix autodetect_locale function #209

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/audible/localization.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import logging
import re
from typing import Dict, Optional
from urllib.parse import parse_qs, urlparse

import httpx
from bs4 import BeautifulSoup
from httpcore import ConnectError


Expand Down Expand Up @@ -94,6 +93,7 @@ def autodetect_locale(domain: str) -> Dict[str, str]:

Raises:
ConnectError: If site does not exist or network error raises.
Exception: If marketplace or country code can't be found.
"""
domain = domain.lstrip(".")
site = f"https://www.audible.{domain}"
Expand All @@ -105,13 +105,17 @@ def autodetect_locale(domain: str) -> Dict[str, str]:
logger.warning("site %s does not exists or Network Error occurs", site)
raise e

soup = BeautifulSoup(resp.text, "html.parser")

login_link = soup.find("a", class_="ui-it-sign-in-link")["href"]
parsed_link = urlparse(login_link)
query_string = parse_qs(parsed_link.query)
market_place_id = query_string["marketPlaceId"][0]
country_code = query_string["pageId"][0].split("_")[-1]
marketplace_pattern = re.compile(r"ue_mid = \'(.*)\'")
marketplace_search = re.search(marketplace_pattern, resp.text)
if marketplace_search is None:
raise Exception("can't find marketplace")
market_place_id = marketplace_search.group(1)

alias_pattern = re.compile(r"autocomplete_config.searchAlias = \"(.*)\"")
alias_search = re.search(alias_pattern, resp.text)
if alias_search is None:
raise Exception("can't find country code")
country_code = alias_search.group(1).split("-")[-1]

return {
"country_code": country_code,
Expand Down