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:
@@ -3,6 +3,28 @@ from adapters.tmdb_adapter import get_media_detail, search_media
|
|||||||
from error_handling import AppServiceError
|
from error_handling import AppServiceError
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_hdhive_items(hdhive_result):
|
||||||
|
payload = (hdhive_result or {}).get("data")
|
||||||
|
# HDHive openapi uses envelope: { success, 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")
|
||||||
|
# HDHive unlock response is usually envelope with inner data object.
|
||||||
|
if isinstance(payload, dict) and isinstance(payload.get("data"), dict):
|
||||||
|
return payload.get("data") or {}
|
||||||
|
if isinstance(payload, dict):
|
||||||
|
return payload
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def search_media_by_keyword(query, media_type):
|
def search_media_by_keyword(query, media_type):
|
||||||
result = search_media(query, media_type)
|
result = search_media(query, media_type)
|
||||||
raw_items = result.get("items") or []
|
raw_items = result.get("items") or []
|
||||||
@@ -25,9 +47,7 @@ def search_media_by_keyword(query, media_type):
|
|||||||
def get_media_resources(media_type, tmdb_id):
|
def get_media_resources(media_type, tmdb_id):
|
||||||
detail = get_media_detail(tmdb_id, media_type)
|
detail = get_media_detail(tmdb_id, media_type)
|
||||||
hdhive = search_resource(media_type, tmdb_id)
|
hdhive = search_resource(media_type, tmdb_id)
|
||||||
search_data = hdhive.get("data") or []
|
search_data = _extract_hdhive_items(hdhive)
|
||||||
if isinstance(search_data, dict):
|
|
||||||
search_data = search_data.get("items") or []
|
|
||||||
|
|
||||||
resources = []
|
resources = []
|
||||||
for item in search_data:
|
for item in search_data:
|
||||||
@@ -37,7 +57,7 @@ def get_media_resources(media_type, tmdb_id):
|
|||||||
if slug:
|
if slug:
|
||||||
try:
|
try:
|
||||||
unlock = unlock_link(slug)
|
unlock = unlock_link(slug)
|
||||||
unlock_data = unlock.get("data") or {}
|
unlock_data = _extract_hdhive_unlock_data(unlock)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
unlock_error = str(error)
|
unlock_error = str(error)
|
||||||
normalized = normalize_resource(item, unlock_data)
|
normalized = normalize_resource(item, unlock_data)
|
||||||
|
|||||||
@@ -9,6 +9,26 @@ from error_handling import AppServiceError, normalize_exception
|
|||||||
from storage import find_media_item, insert_log, upsert_media_item, upsert_task
|
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():
|
def now_iso():
|
||||||
return datetime.utcnow().isoformat() + "Z"
|
return datetime.utcnow().isoformat() + "Z"
|
||||||
|
|
||||||
@@ -55,9 +75,9 @@ def run_ingest_task(payload):
|
|||||||
|
|
||||||
hdhive_search = search_resource(payload["type"], payload["tmdbId"])
|
hdhive_search = search_resource(payload["type"], payload["tmdbId"])
|
||||||
hdhive_first = None
|
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()
|
preferred_slug = str(payload.get("slug") or "").strip()
|
||||||
if isinstance(search_data, list) and search_data:
|
if search_data:
|
||||||
if preferred_slug:
|
if preferred_slug:
|
||||||
hdhive_first = next(
|
hdhive_first = next(
|
||||||
(item for item in search_data if (item or {}).get("slug") == preferred_slug),
|
(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:
|
if not hdhive_first:
|
||||||
hdhive_first = search_data[0]
|
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:
|
if not hdhive_first:
|
||||||
raise AppServiceError(
|
raise AppServiceError(
|
||||||
"HDHIVE 未检索到可用资源",
|
"HDHIVE 未检索到可用资源",
|
||||||
@@ -92,7 +103,7 @@ def run_ingest_task(payload):
|
|||||||
provider="hdhive",
|
provider="hdhive",
|
||||||
)
|
)
|
||||||
hdhive_unlock = unlock_link(slug)
|
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"]})
|
log(task_id, "HDHIVE_UNLOCK", "INFO", "HDHIVE 解锁成功", {"unlockUrl": normalized_resource["unlockUrl"]})
|
||||||
|
|
||||||
emby_exists = exists_by_tmdb_id(payload["tmdbId"])
|
emby_exists = exists_by_tmdb_id(payload["tmdbId"])
|
||||||
|
|||||||
Reference in New Issue
Block a user