Skip to content

Commit

Permalink
fix: Fix autodetect_locale function
Browse files Browse the repository at this point in the history
  • Loading branch information
mkb79 committed Oct 3, 2023
1 parent afa1806 commit a59a166
Showing 1 changed file with 13 additions and 9 deletions.
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

0 comments on commit a59a166

Please sign in to comment.