Spaces:
Running
Running
Commit ·
e6abf12
1
Parent(s): 900c3a8
fix: Castle scraper - handle base64-encoded security key, robust data parsing
Browse files512 unmodified lines51351451551651751851952052152212 unmodified lines53553653753853954054154254354454554654754854955055155233 unmodified lines58658758858959059159216 unmodified lines609610611612613614615616617618619620621622623624625512 unmodified linesreturn results, errorssec_data = sec_resp.json()security_key = sec_data.get("data", {}).get("securityKey", "")if not security_key:errors.append("Castle: No security key in response")return results, errors# Derive AES key and IV: (securityKey + "T!BgJB")[:16]aes_key_str = (security_key + "T!BgJB")[:16]12 unmodified linessearch_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 resultstry: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)}")return results, errors# Find matching movie by title33 unmodified linesdetail_data = detail_resp.json()encrypted_detail = detail_data.get("data", "")if encrypted_detail:try:raw = base64.b64decode(encrypted_detail)cipher = AES.new(aes_key, AES.MODE_CBC, aes_iv)16 unmodified linesvideo_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, errorstry: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)}")return results, errors# Extract stream URLs from video response512 unmodified lines51351451551651751851952052152252352452552652752852953012 unmodified lines54354454554654754854955055155255355455555655755855956056156256356456556656756856933 unmodified lines60360460560660760860961061116 unmodified lines628629630631632633634635636637638639640641642643644645646647648649650651652653512 unmodified linesreturn results, errorssec_data = sec_resp.json()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 stringtry: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 linessearch_data = search_resp.json()encrypted_data = search_data.get("data", "")# Handle case where data might not be encrypted (direct JSON)search_results = Noneif isinstance(encrypted_data, (dict, list)):search_results = encrypted_dataelif 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 JSONtry:search_results = json.loads(encrypted_data)except:errors.append(f"Castle: Search decryption failed - {str(dec_err)}")return results, errorsif search_results is None:errors.append("Castle: No search data in response")return results, errors# Find matching movie by title33 unmodified linesdetail_data = detail_resp.json()encrypted_detail = detail_data.get("data", "")if isinstance(encrypted_detail, (dict, list)):detail_parsed = encrypted_detailelif 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 linesvideo_data = video_resp.json()encrypted_video = video_data.get("data", "")video_parsed = Noneif isinstance(encrypted_video, (dict, list)):video_parsed = encrypted_videoelif 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 JSONtry:video_parsed = json.loads(encrypted_video)except:errors.append(f"Castle: Video decryption failed - {str(dec_err)}")return results, errorsif video_parsed is None:errors.append("Castle: No video data in response")return results, errors# Extract stream URLs from video response
main.py+54-26