# coding=utf-8
import sys
sys.path.append('..')
from base.spider import Spider  # 核心：继承TVBox基类防崩溃
import re
import json
# [py578 adaptation] requests hard-dependency fallback (real requests preferred)
try:
    import requests
except Exception:
    requests = None
if requests is None:
    # --- embedded requests-compatible shim (urllib based) for Yingxia adaptation ---
    # Inserted by py578 adaptation: used only when real `requests` is unavailable.
    import json as _json
    import ssl as _ssl
    import urllib.error as _uerr
    import urllib.parse as _uparse
    import urllib.request as _ureq
    import http.cookiejar as _cookiejar


    class _ReqExc(Exception):
        pass


    class _Timeout(_ReqExc):
        pass


    class _ConnectionError(_ReqExc):
        pass


    class _HTTPError(_ReqExc):
        pass


    class _CaseDict(dict):
        def get(self, k, d=None):
            for kk, vv in self.items():
                if kk.lower() == str(k).lower():
                    return vv
            return d

        def __getitem__(self, k):
            v = self.get(k)
            if v is None:
                raise KeyError(k)
            return v


    class _Response:
        def __init__(self, status_code=0, headers=None, content=b"", url="", cookies=None):
            self.status_code = status_code
            self.headers = _CaseDict(headers or {})
            self.content = content or b""
            self.url = url
            self.cookies = cookies or {}
            self.encoding = None
            self._text = None

        @property
        def text(self):
            if self._text is None:
                enc = self.encoding
                if not enc:
                    ct = self.headers.get("Content-Type", "")
                    m = _uparse.parse_qs(ct.replace(";", "&")) if False else None
                    enc = "utf-8"
                    if "charset=" in ct:
                        enc = ct.split("charset=")[-1].split(";")[0].strip() or "utf-8"
                try:
                    self._text = self.content.decode(enc, "ignore")
                except Exception:
                    self._text = self.content.decode("utf-8", "ignore")
            return self._text

        def json(self):
            return _json.loads(self.text)

        def raise_for_status(self):
            if self.status_code >= 400:
                raise _HTTPError("HTTP %s" % self.status_code)

        def iter_content(self, chunk_size=65536):
            for i in range(0, len(self.content), chunk_size):
                yield self.content[i:i + chunk_size]

        def close(self):
            pass


    class _Session:
        def __init__(self):
            self.headers = {"User-Agent": "Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 Chrome/120 Mobile Safari/537.36"}
            self.cookies = {}
            self._jar = _cookiejar.CookieJar()
            self._ctx = _ssl._create_unverified_context()
            self._opener = _ureq.build_opener(
                _ureq.HTTPCookieProcessor(self._jar),
                _ureq.HTTPSHandler(context=self._ctx))

        def request(self, method, url, headers=None, timeout=15, data=None,
                    json=None, params=None, cookies=None, verify=False,
                    allow_redirects=True, stream=False, **kw):
            if params:
                qs = _uparse.urlencode(params, doseq=True)
                url = url + ("&" if "?" in url else "?") + qs
            hd = dict(self.headers)
            if headers:
                hd.update(headers)
            body = None
            if json is not None:
                body = _json.dumps(json, ensure_ascii=False).encode("utf-8")
                hd.setdefault("Content-Type", "application/json")
            elif isinstance(data, dict):
                body = _uparse.urlencode(data, doseq=True).encode("utf-8")
                hd.setdefault("Content-Type", "application/x-www-form-urlencoded")
            elif isinstance(data, str):
                body = data.encode("utf-8")
            elif isinstance(data, (bytes, bytearray)):
                body = bytes(data)
            if cookies:
                hd["Cookie"] = "; ".join("%s=%s" % kv for kv in cookies.items())
            elif self.cookies:
                hd.setdefault("Cookie", "; ".join("%s=%s" % kv for kv in self.cookies.items()))
            req = _ureq.Request(url, data=body, headers=hd, method=method.upper())
            try:
                to = timeout
                if isinstance(to, (tuple, list)):
                    to = max(to)
                if to and float(to) > 120:
                    to = float(to) / 1000.0
                with self._opener.open(req, timeout=to or 15) as resp:
                    content = resp.read()
                    hdrs = dict(resp.headers.items())
                    final = resp.geturl()
                    status = resp.status
            except _uerr.HTTPError as e:
                try:
                    content = e.read()
                except Exception:
                    content = b""
                hdrs = dict(e.headers.items()) if e.headers else {}
                final = e.geturl() if hasattr(e, "geturl") else url
                status = e.code
            except Exception as e:
                raise _ConnectionError(str(e))
            for c in self._jar:
                self.cookies[c.name] = c.value
            return _Response(status, hdrs, content, final, dict(self.cookies))

        def get(self, url, **kw):
            return self.request("GET", url, **kw)

        def post(self, url, data=None, **kw):
            return self.request("POST", url, data=data, **kw)

        def head(self, url, **kw):
            return self.request("HEAD", url, **kw)

        def close(self):
            pass


    class _Exceptions:
        RequestException = _ReqExc
        Timeout = _Timeout
        ConnectTimeout = _Timeout
        ReadTimeout = _Timeout
        ConnectionError = _ConnectionError
        HTTPError = _HTTPError
        SSLError = _ConnectionError


    class _RequestsShim:
        Session = _Session
        exceptions = _Exceptions
        Response = _Response

        @staticmethod
        def request(method, url, **kw):
            return _Session().request(method, url, **kw)

        @staticmethod
        def get(url, **kw):
            return _Session().request("GET", url, **kw)

        @staticmethod
        def post(url, data=None, **kw):
            return _Session().request("POST", url, data=data, **kw)

        @staticmethod
        def head(url, **kw):
            return _Session().request("HEAD", url, **kw)


    requests = _RequestsShim
    # --- end embedded requests shim ---

    # --- py578 compat extras for deep requests usage (fallback only) ---
    class _HTTPAdapter:
        def __init__(self, *a, **k):
            pass


    class _Retry:
        def __init__(self, *a, **k):
            pass


    def _session_mount(self, *a, **k):
        return None


    _Session.mount = _session_mount
    _Session.put = lambda self, url, data=None, **kw: self.request("PUT", url, data=data, **kw)
    _Session.delete = lambda self, url, **kw: self.request("DELETE", url, **kw)
    _Session.patch = lambda self, url, data=None, **kw: self.request("PATCH", url, data=data, **kw)
    _Session.options = lambda self, url, **kw: self.request("OPTIONS", url, **kw)
    _Response.apparent_encoding = "utf-8"
    _Response.ok = property(lambda self: self.status_code < 400)
    _Response.history = property(lambda self: [])


    class _ReqUtils:
        quote = staticmethod(_uparse.quote)
        quote_plus = staticmethod(_uparse.quote_plus)
        unquote = staticmethod(_uparse.unquote)
        unquote_plus = staticmethod(_uparse.unquote_plus)


    class _ReqAdapters:
        HTTPAdapter = _HTTPAdapter


    class _ReqUrllib3:
        @staticmethod
        def disable_warnings(*a, **k):
            pass

        class exceptions:
            class InsecureRequestWarning(Warning):
                pass


    class _ReqPackages:
        urllib3 = _ReqUrllib3


    _RequestsShim.utils = _ReqUtils
    _RequestsShim.adapters = _ReqAdapters
    _RequestsShim.packages = _ReqPackages
    _RequestsShim.put = staticmethod(lambda url, data=None, **kw: _Session().request("PUT", url, data=data, **kw))
    _RequestsShim.delete = staticmethod(lambda url, **kw: _Session().request("DELETE", url, **kw))
    _RequestsShim.patch = staticmethod(lambda url, data=None, **kw: _Session().request("PATCH", url, data=data, **kw))
    # --- end py578 compat extras ---
# [py578] import requests  (provided by adaptation above)

class Spider(Spider):
    def getName(self):
        return "美不胜收(免翻)"

    def init(self, extend=""):
        pass

    # ==========================================
    # 模块 1：网络配置模块 (requests 重构版)
    # ==========================================
    def _fetch(self, url):
        try:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
                'Referer': 'https://mbss3.xyz/'
            }
            # verify=False 忽略证书报错，替换掉不稳定的 ssl 库
            response = requests.get(url, headers=headers, verify=False, timeout=10)
            response.encoding = 'utf-8'
            return response.text
        except Exception:
            return ""

    # ==========================================
    # 模块 2：分类路由模块
    # ==========================================
    def homeContent(self, filter):
        classes = [
            {"type_id": "20", "type_name": "在线一区"},
            {"type_id": "21", "type_name": "国产色情"},
            {"type_id": "22", "type_name": "主播直播"},
            {"type_id": "23", "type_name": "亚洲精品"},
            {"type_id": "24", "type_name": "中文字幕"},
            {"type_id": "25", "type_name": "人妻熟女"},
            {"type_id": "26", "type_name": "强奸乱伦"},
            {"type_id": "27", "type_name": "欧美精品"},
            {"type_id": "28", "type_name": "成人动漫"},
            {"type_id": "29", "type_name": "在线二区"},
            {"type_id": "30", "type_name": "自拍偷拍"},
            {"type_id": "31", "type_name": "制服丝袜"},
            {"type_id": "32", "type_name": "变态另类"},
            {"type_id": "33", "type_name": "AI换脸"},
            {"type_id": "34", "type_name": "三级伦理"},
            {"type_id": "35", "type_name": "AV解说"},
            {"type_id": "36", "type_name": "传媒探花"},
            {"type_id": "37", "type_name": "美乳巨乳"}
        ]
        return {"class": classes}

    def categoryContent(self, tid, pg, filter, extend):
        try:
            # 强制转换为整数，防止 OK影视 强类型检查崩溃
            page_num = int(pg)
            url = f"https://mbss3.xyz/index.php/vod/type/id/{tid}/page/{page_num}.html"
            html = self._fetch(url)
            
            videos = self._parse_cards(html)
            
            if not videos:
                return {
                    "list": [{
                        "vod_id": "error",
                        "vod_name": f"无数据, 源码长度:{len(html)}",
                        "vod_pic": "https://img.picui.cn/free/2024/06/18/66718d09a0da7.png",
                        "vod_remarks": "排查网页"
                    }],
                    "page": page_num, "pagecount": 1, "limit": 1, "total": 1
                }

            return {
                "list": videos,
                "page": page_num,
                "pagecount": 999,
                "limit": len(videos),
                "total": 9999
            }
        except Exception as e:
            return {
                "list": [{
                    "vod_id": "crash",
                    "vod_name": f"异常: {str(e)}",
                    "vod_pic": "https://img.picui.cn/free/2024/06/18/66718d09a0da7.png",
                    "vod_remarks": "代码错误"
                }],
                "page": 1, "pagecount": 1, "limit": 1, "total": 1
            }

    # ==========================================
    # 模块 3：UI卡片解析模块
    # ==========================================
    def _parse_cards(self, html):
        videos = []
        items = re.findall(r'<li.*?>(.*?)</li>', html, re.I | re.S)
        if not items:
            items = re.findall(r'(<a class="thumbnail".*?</div>)', html, re.I | re.S)

        for item in items:
            href_match = re.search(r'<a class="thumbnail" href=["\']([^"\']+)["\']', item, re.I)
            pic_match = re.search(r'data-original=["\']([^"\']+)["\']', item, re.I)
            
            if not href_match or not pic_match:
                continue
                
            vod_id = href_match.group(1)
            pic = pic_match.group(1)
            
            # 手动拼接 URL，彻底抛弃 urllib.parse 避免 NameError
            if not vod_id.startswith('http'):
                vod_id = "https://mbss3.xyz" + vod_id if vod_id.startswith('/') else "https://mbss3.xyz/" + vod_id
            
            title_match = re.search(r'<h5>\s*<a[^>]*>(.*?)</a>', item, re.I | re.S)
            vod_name = title_match.group(1).strip() if title_match else "未知视频"
            
            remark_match = re.search(r'<p class="vodtitle">(.*?)</p>', item, re.I | re.S)
            if remark_match:
                raw_remark = re.sub(r'<[^>]+>', '', remark_match.group(1)).strip()
                date_match = re.search(r'\d{4}-(\d{2}-\d{2})', raw_remark)
                view_match = re.search(r'观看:\s*(\d+)', raw_remark)
                
                tags = []
                if date_match:
                    tags.append(date_match.group(1))
                if view_match:
                    views = int(view_match.group(1))
                    if views >= 10000:
                        tags.append(f"👁{round(views/10000, 1)}w")
                    else:
                        tags.append(f"👁{views}")
                vod_remarks = " ".join(tags) if tags else "高清"
            else:
                vod_remarks = "高清"

            videos.append({
                "vod_id": vod_id,
                "vod_name": vod_name,
                "vod_pic": pic.strip(),
                "vod_remarks": vod_remarks
            })
            
        return videos

    # ==========================================
    # 模块 4 & 5：详情与播放直连模块
    # ==========================================
    def detailContent(self, ids):
        # 修复 Chaquopy 的数组跨语言类型陷阱
        if isinstance(ids, str):
            target_url = ids
        else:
            try:
                target_url = str(ids[0])
            except Exception:
                target_url = str(ids)
        
        vod = {
            "vod_id": target_url,
            "vod_name": "美不胜收(免翻)",
            "vod_director": "YJ-Gemini",
            "vod_play_from": "YJ-Gemini 专线",
            "vod_play_url": f"原画直连${target_url}"
        }
        return {"list": [vod]}

    def playerContent(self, flag, id, vipFlags):
        try:
            detail_html = self._fetch(id)
            best_link = ""
            
            play_match = re.search(r'href=["\'](/index\.php/vod/play/[^"\']+)["\']', detail_html)
            if play_match:
                play_path = play_match.group(1)
                play_url = "https://mbss3.xyz" + play_path if play_path.startswith('/') else "https://mbss3.xyz/" + play_path
                play_html = self._fetch(play_url)
                
                mac_player = re.search(r'player_aaaa\s*=\s*(\{.*?\})', play_html)
                if mac_player:
                    try:
                        json_str = mac_player.group(1).replace('\\/', '/')
                        player_data = json.loads(json_str)
                        if "url" in player_data:
                            # 手动解码，替代 urllib.parse.unquote
                            raw_url = player_data["url"]
                            best_link = raw_url.replace('%3A', ':').replace('%2F', '/').replace('%3F', '?').replace('%3D', '=')
                    except Exception:
                        pass
                        
                if not best_link:
                    m3u8_links = re.findall(r'(https?://[^\s\'"]+\.(?:m3u8|mp4))', play_html)
                    if m3u8_links:
                        best_link = m3u8_links[0]
                        
            if not best_link:
                return {"parse": 1, "jx": 0, "url": id}

            return {
                "parse": 0, 
                "jx": 0, 
                "url": best_link,
                "header": {
                    "User-Agent": "Mozilla/5.0",
                    "Referer": "https://mbss3.xyz/"
                }
            }
        except Exception as e:
            return {"parse": 0, "jx": 0, "url": f"http://error.com/error.mp4?msg={str(e)}"}

    # ==========================================
    # 模块 6：搜索模块
    # ==========================================
    def searchContent(self, key, quick):
        try:
            # 纯 Python 的 URL 编码，避免使用 urllib
            encoded_key = key.encode('utf-8').hex()
            encoded_key = '%' + '%'.join([encoded_key[i:i+2] for i in range(0, len(encoded_key), 2)])
            url = f"https://mbss3.xyz/index.php/vod/search.html?wd={encoded_key}"
            
            html = self._fetch(url)
            videos = self._parse_cards(html)
            return {"list": videos}
        except Exception:
            return {"list": []}
