fix(metadata): use audible.de api for german audiobooks
This commit is contained in:
+37
-16
@@ -1,32 +1,53 @@
|
|||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
from .base import MetadataResult
|
from .base import MetadataResult
|
||||||
|
|
||||||
|
# Comma-separated marketplace TLDs, first entries rank first in the results.
|
||||||
|
REGIONS = [r.strip() for r in os.environ.get("WORDARR_AUDIBLE_REGIONS", "de,com").split(",")]
|
||||||
|
|
||||||
async def search(query: str) -> list[MetadataResult]:
|
|
||||||
|
async def _search_region(client: httpx.AsyncClient, tld: str, query: str) -> list[dict]:
|
||||||
params = {
|
params = {
|
||||||
"keywords": query,
|
"keywords": query,
|
||||||
"num_results": 10,
|
"num_results": 10,
|
||||||
"response_groups": "media,contributors,product_desc,product_attrs",
|
"response_groups": "media,contributors,product_desc,product_attrs",
|
||||||
"products_sort_by": "Relevance",
|
"products_sort_by": "Relevance",
|
||||||
}
|
}
|
||||||
async with httpx.AsyncClient(timeout=15) as client:
|
try:
|
||||||
resp = await client.get("https://api.audible.com/1.0/catalog/products", params=params)
|
resp = await client.get(f"https://api.audible.{tld}/1.0/catalog/products", params=params)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
products = resp.json().get("products", [])
|
return resp.json().get("products", [])
|
||||||
|
except httpx.HTTPError:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
async def search(query: str) -> list[MetadataResult]:
|
||||||
|
async with httpx.AsyncClient(timeout=15) as client:
|
||||||
|
region_results = await asyncio.gather(
|
||||||
|
*(_search_region(client, tld, query) for tld in REGIONS)
|
||||||
|
)
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
for p in products:
|
seen_asins = set()
|
||||||
images = p.get("product_images") or {}
|
for products in region_results:
|
||||||
release = p.get("release_date") or ""
|
for p in products:
|
||||||
results.append(
|
asin = p.get("asin", "")
|
||||||
MetadataResult(
|
if asin in seen_asins:
|
||||||
media_type="audiobook",
|
continue
|
||||||
title=p.get("title", ""),
|
seen_asins.add(asin)
|
||||||
authors=", ".join(a.get("name", "") for a in p.get("authors") or []),
|
images = p.get("product_images") or {}
|
||||||
external_id=p.get("asin", ""),
|
release = p.get("release_date") or ""
|
||||||
year=int(release[:4]) if release[:4].isdigit() else None,
|
results.append(
|
||||||
cover_url=next(iter(images.values()), ""),
|
MetadataResult(
|
||||||
|
media_type="audiobook",
|
||||||
|
title=p.get("title", ""),
|
||||||
|
authors=", ".join(a.get("name", "") for a in p.get("authors") or []),
|
||||||
|
external_id=asin,
|
||||||
|
year=int(release[:4]) if release[:4].isdigit() else None,
|
||||||
|
cover_url=next(iter(images.values()), ""),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
return results
|
return results
|
||||||
|
|||||||
Reference in New Issue
Block a user