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
|
||||
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user