Spaces:
Running
Running
Commit ·
e6abf12
1
Parent(s): 900c3a8
fix: Castle scraper - handle base64-encoded security key, robust data parsing
Browse files512 unmodified lines51351451551651751651751851952052152252352452552652752852953012 unmodified lines54354454553853954054154254354454554654754854954654754854955055155255355455555655755855956056156256356456556656756856933 unmodified lines60360460558960660760860961061116 unmodified lines628629630631612613614615616617618619620621622632633634635636637638639640641642643644645646647648649650651652653512 unmodified lines return results, errors
sec_data = sec_resp.json() security_key = sec_data.get("data", {}).get("securityKey", "") if not security_key: raw_key = sec_data.get("data", "") if not raw_key: errors.append("Castle: No security key in response") return results, errors # data can be base64-encoded string or plain string try: security_key = base64.b64decode(raw_key).decode("utf-8", errors="ignore") except Exception: security_key = raw_key # Also try nested format: {"data": {"securityKey": "..."}} if isinstance(sec_data.get("data"), dict): security_key = sec_data["data"].get("securityKey", security_key)
# Derive AES key and IV: (securityKey + "T!BgJB")[:16] aes_key_str = (security_key + "T!BgJB")[:16]12 unmodified lines
search_data = search_resp.json() encrypted_data = search_data.get("data", "") if not encrypted_data: errors.append("Castle: No encrypted data in search response") return results, errors
# Decrypt search results try: raw = base64.b64decode(encrypted_data) cipher = AES.new(aes_key, AES.MODE_CBC, aes_iv) decrypted = unpad(cipher.decrypt(raw), AES.block_size).decode("utf-8", errors="ignore") search_results = json.loads(decrypted) except Exception as dec_err: errors.append(f"Castle: Search decryption failed - {str(dec_err)}") # Handle case where data might not be encrypted (direct JSON) search_results = None if isinstance(encrypted_data, (dict, list)): search_results = encrypted_data elif isinstance(encrypted_data, str) and encrypted_data: try: raw = base64.b64decode(encrypted_data) cipher = AES.new(aes_key, AES.MODE_CBC, aes_iv) decrypted = unpad(cipher.decrypt(raw), AES.block_size).decode("utf-8", errors="ignore") search_results = json.loads(decrypted) except Exception as dec_err: # Try parsing as plain JSON try: search_results = json.loads(encrypted_data) except: errors.append(f"Castle: Search decryption failed - {str(dec_err)}") return results, errors if search_results is None: errors.append("Castle: No search data in response") return results, errors
# Find matching movie by title33 unmodified lines
detail_data = detail_resp.json() encrypted_detail = detail_data.get("data", "") if encrypted_detail: if isinstance(encrypted_detail, (dict, list)): detail_parsed = encrypted_detail elif isinstance(encrypted_detail, str) and encrypted_detail: try: raw = base64.b64decode(encrypted_detail) cipher = AES.new(aes_key, AES.MODE_CBC, aes_iv)16 unmodified lines
video_data = video_resp.json() encrypted_video = video_data.get("data", "") if not encrypted_video: errors.append("Castle: No encrypted video data in response") return results, errors
try: raw = base64.b64decode(encrypted_video) cipher = AES.new(aes_key, AES.MODE_CBC, aes_iv) decrypted_video = unpad(cipher.decrypt(raw), AES.block_size).decode("utf-8", errors="ignore") video_parsed = json.loads(decrypted_video) except Exception as dec_err: errors.append(f"Castle: Video decryption failed - {str(dec_err)}") video_parsed = None if isinstance(encrypted_video, (dict, list)): video_parsed = encrypted_video elif isinstance(encrypted_video, str) and encrypted_video: try: raw = base64.b64decode(encrypted_video) cipher = AES.new(aes_key, AES.MODE_CBC, aes_iv) decrypted_video = unpad(cipher.decrypt(raw), AES.block_size).decode("utf-8", errors="ignore") video_parsed = json.loads(decrypted_video) except Exception as dec_err: # Try parsing as plain JSON try: video_parsed = json.loads(encrypted_video) except: errors.append(f"Castle: Video decryption failed - {str(dec_err)}") return results, errors if video_parsed is None: errors.append("Castle: No video data in response") return results, errors
# Extract stream URLs from video responsemain.py+54-26