diff --git a/backend/services/media_service.py b/backend/services/media_service.py index 4a209d6..a5dbf11 100644 --- a/backend/services/media_service.py +++ b/backend/services/media_service.py @@ -3,6 +3,28 @@ from adapters.tmdb_adapter import get_media_detail, search_media 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): result = search_media(query, media_type) 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): detail = get_media_detail(tmdb_id, media_type) hdhive = search_resource(media_type, tmdb_id) - search_data = hdhive.get("data") or [] - if isinstance(search_data, dict): - search_data = search_data.get("items") or [] + search_data = _extract_hdhive_items(hdhive) resources = [] for item in search_data: @@ -37,7 +57,7 @@ def get_media_resources(media_type, tmdb_id): if slug: try: unlock = unlock_link(slug) - unlock_data = unlock.get("data") or {} + unlock_data = _extract_hdhive_unlock_data(unlock) except Exception as error: unlock_error = str(error) normalized = normalize_resource(item, unlock_data) diff --git a/backend/services/orchestrator.py b/backend/services/orchestrator.py index 3969477..5de9572 100644 --- a/backend/services/orchestrator.py +++ b/backend/services/orchestrator.py @@ -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"])