|
| 1 | +import logging |
| 2 | + |
| 3 | +from .constants import ( |
| 4 | + DOWNLOAD_FILENAME_REGEX, |
| 5 | + DOWNLOAD_URL_REGEX, |
| 6 | + QUALITY_REGEX, |
| 7 | + VIDEO_INFO_CLEAN_REGEX, |
| 8 | + VIDEO_INFO_REGEX, |
| 9 | +) |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def extract_server_info(html_content: str, episode_title: str | None) -> dict | None: |
| 15 | + """ |
| 16 | + Extracts server information from the VixCloud/AnimeUnity embed page. |
| 17 | + Handles extraction from both window.video object and download URL. |
| 18 | + """ |
| 19 | + video_info = VIDEO_INFO_REGEX.search(html_content) |
| 20 | + download_url_match = DOWNLOAD_URL_REGEX.search(html_content) |
| 21 | + |
| 22 | + if not (download_url_match and video_info): |
| 23 | + return None |
| 24 | + |
| 25 | + info_str = VIDEO_INFO_CLEAN_REGEX.sub(r'"\1"', video_info.group(1)) |
| 26 | + |
| 27 | + # Use eval context for JS constants |
| 28 | + ctx = {"null": None, "true": True, "false": False} |
| 29 | + try: |
| 30 | + info = eval(info_str, ctx) |
| 31 | + except Exception as e: |
| 32 | + logger.error(f"Failed to parse JS object: {e}") |
| 33 | + return None |
| 34 | + |
| 35 | + download_url = download_url_match.group(1) |
| 36 | + info["link"] = download_url |
| 37 | + |
| 38 | + # Extract metadata from download URL if missing in window.video |
| 39 | + if filename_match := DOWNLOAD_FILENAME_REGEX.search(download_url): |
| 40 | + info["name"] = filename_match.group(1) |
| 41 | + else: |
| 42 | + info["name"] = f"{episode_title or 'Unknown'}" |
| 43 | + |
| 44 | + if quality_match := QUALITY_REGEX.search(download_url): |
| 45 | + # "720p" -> 720 |
| 46 | + info["quality"] = int(quality_match.group(1)[:-1]) |
| 47 | + else: |
| 48 | + info["quality"] = 0 # Fallback |
| 49 | + |
| 50 | + return info |
0 commit comments