Fix HDHive response unwrapping for resources and unlock data.
Parse nested openapi envelopes from HDHive search/unlock endpoints so resource lists and links are extracted from inner data instead of incorrectly falling back to empty arrays. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -9,6 +9,26 @@ from error_handling import AppServiceError, normalize_exception
|
||||
from storage import find_media_item, insert_log, upsert_media_item, upsert_task
|
||||
|
||||
|
||||
def _extract_hdhive_items(hdhive_result):
|
||||
payload = (hdhive_result or {}).get("data")
|
||||
if isinstance(payload, dict) and isinstance(payload.get("data"), list):
|
||||
return payload.get("data") or []
|
||||
if isinstance(payload, list):
|
||||
return payload
|
||||
if isinstance(payload, dict):
|
||||
return payload.get("items") or []
|
||||
return []
|
||||
|
||||
|
||||
def _extract_hdhive_unlock_data(unlock_result):
|
||||
payload = (unlock_result or {}).get("data")
|
||||
if isinstance(payload, dict) and isinstance(payload.get("data"), dict):
|
||||
return payload.get("data") or {}
|
||||
if isinstance(payload, dict):
|
||||
return payload
|
||||
return {}
|
||||
|
||||
|
||||
def now_iso():
|
||||
return datetime.utcnow().isoformat() + "Z"
|
||||
|
||||
@@ -55,9 +75,9 @@ def run_ingest_task(payload):
|
||||
|
||||
hdhive_search = search_resource(payload["type"], payload["tmdbId"])
|
||||
hdhive_first = None
|
||||
search_data = hdhive_search.get("data") or {}
|
||||
search_data = _extract_hdhive_items(hdhive_search)
|
||||
preferred_slug = str(payload.get("slug") or "").strip()
|
||||
if isinstance(search_data, list) and search_data:
|
||||
if search_data:
|
||||
if preferred_slug:
|
||||
hdhive_first = next(
|
||||
(item for item in search_data if (item or {}).get("slug") == preferred_slug),
|
||||
@@ -65,15 +85,6 @@ def run_ingest_task(payload):
|
||||
)
|
||||
if not hdhive_first:
|
||||
hdhive_first = search_data[0]
|
||||
elif isinstance(search_data, dict):
|
||||
items = search_data.get("items") or []
|
||||
if preferred_slug:
|
||||
hdhive_first = next(
|
||||
(item for item in items if (item or {}).get("slug") == preferred_slug),
|
||||
None,
|
||||
)
|
||||
if items:
|
||||
hdhive_first = hdhive_first or items[0]
|
||||
if not hdhive_first:
|
||||
raise AppServiceError(
|
||||
"HDHIVE 未检索到可用资源",
|
||||
@@ -92,7 +103,7 @@ def run_ingest_task(payload):
|
||||
provider="hdhive",
|
||||
)
|
||||
hdhive_unlock = unlock_link(slug)
|
||||
normalized_resource = normalize_resource(hdhive_first, hdhive_unlock.get("data"))
|
||||
normalized_resource = normalize_resource(hdhive_first, _extract_hdhive_unlock_data(hdhive_unlock))
|
||||
log(task_id, "HDHIVE_UNLOCK", "INFO", "HDHIVE 解锁成功", {"unlockUrl": normalized_resource["unlockUrl"]})
|
||||
|
||||
emby_exists = exists_by_tmdb_id(payload["tmdbId"])
|
||||
|
||||
Reference in New Issue
Block a user