# -*- coding: utf-8 -*-
import re
import json
import html as _html
try:
    import requests
except Exception:
    # --- 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 shim compat patches (only run when real requests is missing) ---
    _Response.ok = property(lambda self: self.status_code < 400)
    _Response.apparent_encoding = property(lambda self: "utf-8")
    def _shim_mount(self, *a, **k):
        return None
    _Session.mount = _shim_mount
    class _CookieDict(dict):
        def set(self, name, value, domain=None, path=None, **kw):
            self[name] = value
        def get_dict(self):
            return dict(self)
    _orig_session_init = _Session.__init__
    def _session_init_patched(self):
        _orig_session_init(self)
        self.cookies = _CookieDict(self.cookies)
        self.proxies = {}
    _Session.__init__ = _session_init_patched
    _orig_request = _Session.request
    def _request_patched(self, method, url, headers=None, data=None, files=None, **kw):
        if files:
            boundary = "----py578FormBoundary7MA4YWxkTrZu0gW"
            chunks = []
            items = files.items() if isinstance(files, dict) else files
            for k, v in items:
                if isinstance(v, (tuple, list)):
                    filename = v[0] if len(v) > 0 else None
                    val = v[1] if len(v) > 1 else ""
                    ctype = v[2] if len(v) > 2 else None
                else:
                    filename, val, ctype = None, v, None
                if hasattr(val, "read"):
                    val = val.read()
                if isinstance(val, str):
                    val_b = val.encode("utf-8")
                elif isinstance(val, (bytes, bytearray)):
                    val_b = bytes(val)
                else:
                    val_b = str(val).encode("utf-8")
                head = '--%s\r\nContent-Disposition: form-data; name="%s"' % (boundary, k)
                if filename:
                    head += '; filename="%s"' % filename
                head += "\r\n"
                if ctype:
                    head += "Content-Type: %s\r\n" % ctype
                elif filename:
                    head += "Content-Type: application/octet-stream\r\n"
                head += "\r\n"
                chunks.append(head.encode("utf-8") + val_b + b"\r\n")
            chunks.append(("--%s--\r\n" % boundary).encode("utf-8"))
            body = b"".join(chunks)
            hd = dict(headers or {})
            hd["Content-Type"] = "multipart/form-data; boundary=%s" % boundary
            return _orig_request(self, method, url, headers=hd, data=body, **kw)
        return _orig_request(self, method, url, headers=headers, data=data, **kw)
    _Session.request = _request_patched
    _RequestsShim.RequestException = _ReqExc
    _RequestsShim.Timeout = _Timeout
    _RequestsShim.ConnectionError = _ConnectionError
    _RequestsShim.HTTPError = _HTTPError
    class _ReqUtils:
        quote = staticmethod(_uparse.quote)
        unquote = staticmethod(_uparse.unquote)
        urlencode = staticmethod(_uparse.urlencode)
    _RequestsShim.utils = _ReqUtils
    class _ReqPackages:
        class urllib3:
            @staticmethod
            def disable_warnings(*a, **k):
                pass
    _RequestsShim.packages = _ReqPackages
    # --- end py578 shim compat patches ---
from base.spider import Spider
SITE = "https://www.zhangyuys.com"
API = SITE + "/api/v1/videos.php"
COL_API = SITE + "/api/v1/collections.php"
PLAY_BASE = "https://www.zhangyu.bar/h/%s/index.m3u8"
PIC_BASE = "https://www.zhangyu.bar/p/%s.jpg"
UA = ("Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 "
      "(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36")
TIMEOUT = 8
PER = 20

CATS = [
    ("all", "最新"),
    ("hot", "今日热门"),
    ("43", "真人"),
    ("1", "动画"),
    ("col", "合集"),
]


class Spider(Spider):

    def __init__(self):
        try:
            super().__init__()
        except TypeError:
            pass
        self.sitekey = ""
        self._cache = {}

    def getName(self):
        return "章鱼Play"

    def getDependence(self):
        return ""

    def init(self, extend=""):
        print(">>>>> 章鱼Play.init extend=", extend)
        cfg = {}
        try:
            if extend:
                cfg = json.loads(extend)
        except Exception:
            cfg = {}
        if not isinstance(cfg, dict):
            cfg = {}
        # siteKey 兜底：优先取内核注入的源 key（PyLoader.getSpider 在 init 前写入）
        self.sitekey = str(self.__dict__.get("siteKey", "") or "").strip()
        if not self.sitekey:
            self.sitekey = str(cfg.get("siteKey") or cfg.get("sitekey") or "").strip()
        self._cache = {}
        return None

    def destroy(self):
        return None

    def isVideoFormat(self, url):
        return False

    def isTextFormat(self, url):
        return False

    def manualVideoCheck(self):
        return False

    def playerContent(self, flag, id, vipFlags):
        """FongMi/OK影视 播放链路：由本方法返回最终播放地址与请求头。
        缺了它壳就拿不到播放地址（表现为点播放无反应/提示无地址）。"""
        print(">>>>> 章鱼Play.playerContent flag=%s id=%s" % (flag, str(id)[:100]))
        raw = str(id or "").strip()
        if raw.startswith("播放$") or raw.startswith("章鱼Play$"):
            raw = raw.split("$", 1)[1]
        if not raw.startswith("http"):
            return {"parse": 1, "url": SITE + "/", "header": {}}
        return {
            "parse": 0,
            "url": raw,
            "header": {"User-Agent": UA, "Referer": SITE + "/"},
        }

    # ------------------------------------------------------------- 请求层
    def _http(self, url, timeout=TIMEOUT):
        r = requests.get(url, headers={"User-Agent": UA, "Referer": SITE + "/"}, timeout=timeout)
        return r.text

    def _json(self, url):
        try:
            return json.loads(self._http(url))
        except Exception as e:
            print(">>>>> 章鱼Play _json ERR", url[:80], e)
            return None

    def _dur(self, sec):
        try:
            s = int(sec)
            m, s = divmod(s, 60)
            if m >= 60:
                h, m = divmod(m, 60)
                return "%d:%02d:%02d" % (h, m, s)
            return "%d:%02d" % (m, s)
        except Exception:
            return ""

    # ------------------------------------------------------------- 列表
    def _card(self, v):
        vid = v.get("video_id") or ""
        if not vid:
            return None
        col = v.get("collection") or {}
        rem = self._dur(v.get("duration")) or "HD"
        if col.get("title"):
            rem += " " + (col.get("episode_label") or "")
        return {
            "vod_id": vid,
            "vod_name": v.get("title") or vid,
            "vod_pic": PIC_BASE % vid,
            "vod_remarks": rem,
            "vod_content": (v.get("description") or "") or (col.get("title") or ""),
        }

    def _cards(self, j):
        out = []
        for v in (j.get("data") or {}).get("videos") or []:
            c = self._card(v)
            if c:
                out.append(c)
        return out

    def _pages(self, j):
        d = j.get("data") or {}
        total = d.get("total") or 0
        return max(1, -(-total // PER))

    # ------------------------------------------------------------- 首页
    def homeContent(self, filter):
        print(">>>>> 章鱼Play.homeContent")
        classes = [{"type_id": t, "type_name": n} for t, n in CATS]
        return {"class": classes, "filters": {}}

    def homeVideoContent(self):
        print(">>>>> 章鱼Play.homeVideoContent")
        j = self._json(API + "?sort=views_day&order=desc&per_page=%d&status=ready" % PER)
        return {"list": self._cards(j) if j else []}

    # ------------------------------------------------------------- 分类
    def categoryContent(self, tid, pg, filter, extend):
        print(">>>>> 章鱼Play.categoryContent tid=", tid, "pg=", pg)
        try:
            pg = int(pg)
        except Exception:
            pg = 1
        if tid == "col":
            return self._collections()
        if tid == "all":
            q = "sort=upload"
        elif tid == "hot":
            q = "sort=views_day"
        elif str(tid).isdigit():
            q = "sort=upload&category_id=%s" % tid
        else:
            return {"list": []}
        url = "%s?%s&order=desc&per_page=%d&status=ready&page=%d" % (API, q, PER, pg)
        j = self._json(url)
        if not j:
            return {"list": []}
        return {"page": pg, "pagecount": self._pages(j), "limit": PER,
                "total": (j.get("data") or {}).get("total") or 0, "list": self._cards(j)}

    def _collections(self):
        """合集列表：collections.php 一次全量返回"""
        print(">>>>> 章鱼Play.collections")
        j = self._json(COL_API)
        if not j or not j.get("data"):
            return {"list": []}
        out = []
        for c in j["data"]:
            cid = c.get("id")
            if cid is None:
                continue
            out.append({
                "vod_id": "col_%s" % cid,
                "vod_name": c.get("title") or c.get("slug") or str(cid),
                "vod_pic": c.get("cover_url") or PIC_BASE % "",
                "vod_remarks": "%s集" % c.get("episode_count", 0),
                "vod_content": c.get("description") or "",
            })
        return {"page": 1, "pagecount": 1, "limit": len(out), "total": len(out), "list": out}

    # ------------------------------------------------------------- 详情
    def detailContent(self, ids):
        print(">>>>> 章鱼Play.detailContent ids=", ids)
        if not ids:
            return {"list": []}
        # ids 兼容 list / str 两种入参形态（写源总结 v9：主仓转 list，部分 fork 直传 str）
        if isinstance(ids, (list, tuple)):
            vid = ids[0]
        else:
            vid = str(ids)
        vid = str(vid).strip()
        if vid.startswith("col_"):
            return self._col_detail(vid[4:])
        name = self._title_from_page(vid) or vid
        return {"list": [{
            "vod_id": vid,
            "vod_name": name,
            "vod_pic": PIC_BASE % vid,
            "type_name": "章鱼Play",
            "vod_play_from": "章鱼Play",
            "vod_play_url": "播放$" + PLAY_BASE % vid,
        }]}

    def _title_from_page(self, vid):
        """抓详情页取真实标题，失败返回 None"""
        try:
            t = self._http(SITE + "/" + vid + ".html")
            mt = re.search(r"<title>([^<]*)</title>", t)
            if mt:
                name = _html.unescape(mt.group(1))
                name = re.sub(r"\s*[-|]\s*章鱼Play.*$", "", name).strip()
                if name:
                    return name
        except Exception:
            pass
        return None

    def _col_detail(self, cid):
        print(">>>>> 章鱼Play.col_detail cid=", cid)
        url = "%s?collection_id=%s&sort=upload&order=asc&per_page=100&status=ready" % (API, cid)
        j = self._json(url)
        if not j:
            return {"list": []}
        eps = []
        for v in (j.get("data") or {}).get("videos") or []:
            vid = v.get("video_id")
            if not vid:
                continue
            col = v.get("collection") or {}
            label = col.get("episode_label") or v.get("title") or vid
            order = col.get("episode_order") or 0
            eps.append((order, label, PLAY_BASE % vid))
        eps.sort(key=lambda x: x[0])
        cj = self._json(COL_API)
        cname = None
        if cj and cj.get("data"):
            for c in cj["data"]:
                if str(c.get("id")) == str(cid):
                    cname = c.get("title") or c.get("slug")
                    break
        title = cname or ("合集%s" % cid)
        play = "".join("#%s$%s" % (re.sub(r"[#$]", " ", label), url) for _, label, url in eps)
        return {"list": [{
            "vod_id": "col_%s" % cid,
            "vod_name": title,
            "vod_pic": PIC_BASE % "",
            "type_name": "章鱼Play",
            "vod_play_from": "章鱼Play",
            "vod_play_url": play,
            "vod_content": "共%d集" % len(eps),
        }]}

    # ------------------------------------------------------------- 搜索
    def searchContent(self, key, quick, pg="1"):
        print(">>>>> 章鱼Play.searchContent key=", key)
        try:
            t = self._http(SITE + "/search.html?q=" + str(key))
        except Exception as e:
            print(">>>>> 章鱼Play.search ERR", e)
            return {"list": []}
        pat = re.findall(r'<a class="rec-card"[^>]*href="/([A-Za-z0-9]{8,})\.html"[^>]*title="([^"]*)"', t)
        out = []
        for vid, name in pat:
            out.append({
                "vod_id": vid,
                "vod_name": _html.unescape(name),
                "vod_pic": PIC_BASE % vid,
                "vod_remarks": "HD",
                "vod_content": "",
            })
        return {"list": out}


if __name__ == "__main__":
    s = Spider()
    s.init()
    h = s.homeContent(False)
    print("home classes:", [c["type_name"] for c in h["class"]])
    hv = s.homeVideoContent()
    print("homeVideo n:", len(hv["list"]), hv["list"][0]["vod_name"][:20] if hv["list"] else "NONE")
    c = s.categoryContent("all", 2, False, "")
    print("cat all p2: total=%s n=%s" % (c.get("total"), len(c["list"])))
    c = s.categoryContent("col", 1, False, "")
    print("cat col n:", len(c["list"]))
    d = s.detailContent(["1gsMDzF8LUFO"])
    print("detail:", d["list"][0]["vod_name"][:20], d["list"][0]["vod_play_url"][:60])
    d = s.detailContent(["col_74"])
    print("detail col eps:", len(d["list"][0]["vod_play_url"].split("#")) - 1)
    d2 = s.detailContent("1gsMDzF8LUFO")
    print("detail str ids ok:", d2["list"][0]["vod_id"] == "1gsMDzF8LUFO")
    sc = s.searchContent("古寺", False)
    print("search n:", len(sc["list"]))
