#!/usr/bin/python
# -*- coding: utf-8 -*-
import re, urllib.parse
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 augment: adapters/packages/utils/exceptions/mount ---
    class _HTTPAdapterStub:
        def __init__(self, *a, **k):
            pass


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


    class _AdaptersNS:
        HTTPAdapter = _HTTPAdapterStub


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


    class _PackagesNS:
        urllib3 = _U3NS


    class _UtilsNS:
        @staticmethod
        def quote(s, safe='/'):
            return _uparse.quote(s, safe=safe)


    class _TooManyRedirects(_ReqExc):
        pass


    _RequestsShim.adapters = _AdaptersNS
    _RequestsShim.packages = _PackagesNS
    _RequestsShim.utils = _UtilsNS
    _RequestsShim.RequestException = _ReqExc
    _RequestsShim.ConnectionError = _ConnectionError
    _RequestsShim.Timeout = _Timeout
    _RequestsShim.HTTPError = _HTTPError
    _RequestsShim.TooManyRedirects = _TooManyRedirects
    _Exceptions.TooManyRedirects = _TooManyRedirects
    _Exceptions.SSLError = _ConnectionError
    _Exceptions.RetryError = _ReqExc


    def _py578_sess_mount(self, prefix, adapter=None):
        pass


    _Session.mount = _py578_sess_mount
    _py578_sess_init = _Session.__init__


    def _py578_sess_init2(self):
        _py578_sess_init(self)
        self.proxies = {}
        self.trust_env = True


    _Session.__init__ = _py578_sess_init2
    _Response.apparent_encoding = property(lambda self: 'utf-8')
    _Response.ok = property(lambda self: self.status_code < 400)
    # --- end py578 shim augment ---
from lxml import etree
from base.spider import Spider

class Spider(Spider):
    def getName(self): return "小皮影院"
    def init(self, extend=""):
        self.host = "https://www.xptv.cc"
        self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", "Referer": self.host + "/"}
        self.categories = [
            {"type_id": "dianying", "type_name": "电影"},
            {"type_id": "dianshiju", "type_name": "电视剧"},
            {"type_id": "dongman", "type_name": "动漫"}
        ]
        self.filters = {
            "dianying": [
                {"key": "year", "name": "年份", "value": [
                    {"n": "全部", "v": ""}, {"n": "2026", "v": "2026"}, {"n": "2025", "v": "2025"}, {"n": "2024", "v": "2024"},
                    {"n": "2023", "v": "2023"}, {"n": "2022", "v": "2022"}, {"n": "2021", "v": "2021"}, {"n": "2020", "v": "2020"}, {"n": "更早", "v": "1800-1979"}
                ]},
                {"key": "area", "name": "地区", "value": [
                    {"n": "全部", "v": ""}, {"n": "内地", "v": "%E5%86%85%E5%9C%B0"}, {"n": "中国香港", "v": "%E4%B8%AD%E5%9B%BD%E9%A6%99%E6%B8%AF"},
                    {"n": "中国台湾", "v": "%E4%B8%AD%E5%9B%BD%E5%8F%B0%E6%B9%BE"}, {"n": "韩国", "v": "%E9%9F%A9%E5%9B%BD"}, {"n": "日本", "v": "%E6%97%A5%E6%9C%AC"},
                    {"n": "美国", "v": "%E7%BE%8E%E5%9B%BD"}, {"n": "法国", "v": "%E6%B3%95%E5%9B%BD"}, {"n": "英国", "v": "%E8%8B%B1%E5%9B%BD"},
                    {"n": "泰国", "v": "%E6%B3%B0%E5%9B%BD"}, {"n": "印度", "v": "%E5%8D%B0%E5%BA%A6"}, {"n": "其它", "v": "%E5%85%B6%E5%AE%83"}
                ]},
                {"key": "class", "name": "类型", "value": [
                    {"n": "全部", "v": ""}, {"n": "剧情", "v": "%E5%89%A7%E6%83%85"}, {"n": "喜剧", "v": "%E5%96%9C%E5%89%A7"}, {"n": "爱情", "v": "%E7%88%B1%E6%83%85"},
                    {"n": "动作", "v": "%E5%8A%A8%E4%BD%9C"}, {"n": "犯罪", "v": "%E7%8A%AF%E7%BD%AA"}, {"n": "惊悚", "v": "%E6%83%8A%E6%82%9A"}, {"n": "悬疑", "v": "%E6%82%AC%E7%96%91"},
                    {"n": "科幻", "v": "%E7%A7%91%E5%B9%BB"}, {"n": "恐怖", "v": "%E6%81%90%E6%80%96"}, {"n": "奇幻", "v": "%E5%A5%87%E5%B9%BB"}, {"n": "冒险", "v": "%E5%86%92%E9%99%A9"},
                    {"n": "古装", "v": "%E5%8F%A4%E8%A3%85"}, {"n": "动画", "v": "%E5%8A%A8%E7%94%BB"}, {"n": "传记", "v": "%E4%BC%A0%E8%AE%B0"}, {"n": "纪录", "v": "%E7%BA%AA%E5%BD%95"}
                ]},
                {"key": "by", "name": "排序", "value": [
                    {"n": "按时间", "v": "time"}, {"n": "按热度", "v": "hits"}, {"n": "按评分", "v": "score"}
                ]}
            ],
            "dianshiju": [
                {"key": "year", "name": "年份", "value": [
                    {"n": "全部", "v": ""}, {"n": "2026", "v": "2026"}, {"n": "2025", "v": "2025"}, {"n": "2024", "v": "2024"},
                    {"n": "2023", "v": "2023"}, {"n": "2022", "v": "2022"}, {"n": "更早", "v": "1800-1979"}
                ]},
                {"key": "area", "name": "地区", "value": [
                    {"n": "全部", "v": ""}, {"n": "内地", "v": "%E5%86%85%E5%9C%B0"}, {"n": "中国香港", "v": "%E4%B8%AD%E5%9B%BD%E9%A6%99%E6%B8%AF"},
                    {"n": "中国台湾", "v": "%E4%B8%AD%E5%9B%BD%E5%8F%B0%E6%B9%BE"}, {"n": "韩国", "v": "%E9%9F%A9%E5%9B%BD"}, {"n": "日本", "v": "%E6%97%A5%E6%9C%AC"},
                    {"n": "美国", "v": "%E7%BE%8E%E5%9B%BD"}, {"n": "其它", "v": "%E5%85%B6%E5%AE%83"}
                ]},
                {"key": "class", "name": "类型", "value": [
                    {"n": "全部", "v": ""}, {"n": "剧情", "v": "%E5%89%A7%E6%83%85"}, {"n": "喜剧", "v": "%E5%96%9C%E5%89%A7"}, {"n": "爱情", "v": "%E7%88%B1%E6%83%85"},
                    {"n": "动作", "v": "%E5%8A%A8%E4%BD%9C"}, {"n": "犯罪", "v": "%E7%8A%AF%E7%BD%AA"}, {"n": "悬疑", "v": "%E6%82%AC%E7%96%91"},
                    {"n": "古装", "v": "%E5%8F%A4%E8%A3%85"}, {"n": "历史", "v": "%E5%8E%86%E5%8F%B2"}, {"n": "战争", "v": "%E6%88%98%E4%BA%89"},
                    {"n": "动画", "v": "%E5%8A%A8%E7%94%BB"}, {"n": "纪录", "v": "%E7%BA%AA%E5%BD%95"}
                ]},
                {"key": "by", "name": "排序", "value": [
                    {"n": "按时间", "v": "time"}, {"n": "按热度", "v": "hits"}, {"n": "按评分", "v": "score"}
                ]}
            ],
            "dongman": [
                {"key": "year", "name": "年份", "value": [
                    {"n": "全部", "v": ""}, {"n": "2026", "v": "2026"}, {"n": "2025", "v": "2025"}, {"n": "2024", "v": "2024"},
                    {"n": "2023", "v": "2023"}, {"n": "2022", "v": "2022"}, {"n": "更早", "v": "1800-1979"}
                ]},
                {"key": "isend", "name": "状态", "value": [
                    {"n": "全部", "v": ""}, {"n": "完结", "v": "%E5%AE%8C%E7%BB%93"}, {"n": "连载", "v": "%E8%BF%9E%E8%BD%BD"}
                ]}
            ]
        }
    def _get(self, url):
        try:
            r = requests.get(url, headers=self.headers, timeout=8)
            r.encoding = r.apparent_encoding or "utf-8"
            return r.text
        except: return None
    def _fix(self, u):
        if not u: return ""
        if u.startswith("//"): return "https:" + u
        if u.startswith("/"): return self.host + u
        if u.startswith("http"): return u
        return ""
    def _parse_list(self, text):
        if not text: return []
        tree = etree.HTML(text); res, seen = [], set()
        for a in tree.xpath('//a[contains(@href,"/v/")]'):
            try:
                h = a.get("href", "") or ""
                m = re.search(r"/v/(\d+)\.html", h)
                if not m or m.group(1) in seen: continue
                seen.add(m.group(1))
                t = "".join(a.xpath('.//h3/text()')).strip() or a.get("title", "") or ""
                imgs = a.xpath('.//img')
                p = ""
                if imgs:
                    for attr in ["data-original", "data-src", "src"]:
                        p = imgs[0].get(attr, "") or ""
                        if p: break
                res.append({"vod_id": m.group(1), "vod_name": t, "vod_pic": self._fix(p)})
            except: continue
        return res
    def homeContent(self, filter):
        h = self._get(self.host + "/")
        return {"class": self.categories, "list": self._parse_list(h) if h else [], "filters": self.filters}
    def categoryContent(self, tid, pg, filter, extend):
        parts = []
        if extend:
            for k in ["year", "area", "class", "by", "isend"]:
                v = (extend.get(k, "") or "").strip()
                if v: parts.append(f"{k}/{v}")
        if parts:
            url = self.host + f"/show/{tid}/" + "/".join(parts) + ("/page/" + pg if pg != "1" else "") + ".html"
        else:
            url = self.host + f"/show/{tid}.html" if pg == "1" else self.host + f"/show/{tid}/page/{pg}.html"
        h = self._get(url)
        if not h: return {"page": int(pg), "pagecount": 99, "limit": 24, "total": 999, "list": []}
        tree = etree.HTML(h)
        pc = 1
        for x in tree.xpath('//div[contains(@class,"font-mono")]//text()'):
            if "/" in x:
                try: pc = int(x.split("/")[-1].strip()); break
                except: pass
        return {"page": int(pg), "pagecount": pc, "limit": 24, "total": 999, "list": self._parse_list(h)}
    def detailContent(self, ids):
        res = {"list": []}
        for vid in ids:
            try:
                h = self._get(f"{self.host}/v/{vid}.html")
                if not h: continue
                tree = etree.HTML(h)
                name = "".join(tree.xpath('//h1/text()')).strip()
                pic = self._fix("".join(tree.xpath('//main//img[1]/@src')))
                grids = tree.xpath('//div[contains(@class,"playlist-grid")]')
                if not grids:
                    grids = tree.xpath('//div[contains(@class,"playlist")]//div[contains(@class,"grid")]')
                srcs, eps = [], []
                for grid in grids:
                    items = grid.xpath('.//a[contains(@href,"/p/")]')
                    el = [f'{"".join(a.xpath(".//text()")).strip()}${self._fix(a.get("href",""))}' for a in items]
                    if el: srcs.append(f"节点{len(srcs)+1}" if len(grids) > 1 else "云播"); eps.append("#".join(el))
                if not srcs:
                    items = tree.xpath('//a[contains(@href,"/p/")]')
                    el = list(dict.fromkeys([f'{"".join(a.xpath(".//text()")).strip()}${self._fix(a.get("href",""))}' for a in items]))
                    if el: srcs.append("云播"); eps.append("#".join(el))
                res["list"].append({"vod_id": vid, "vod_name": name, "vod_pic": pic, "vod_play_from": "$$$".join(srcs), "vod_play_url": "$$$".join(eps)})
            except: continue
        return res
    def searchContent(self, key, quick, pg="1"):
        return {"list": [], "page": int(pg)}
    def playerContent(self, flag, id, vipFlags):
        url = id if id.startswith("http") else self._fix(id)
        try:
            h = self._get(url)
            if h and 'var player_aaaa=' in h:
                p = h.find('var player_aaaa=') + len('var player_aaaa=')
                depth, end, i = 0, p, p
                while i < len(h):
                    if h[i] == '{': depth += 1
                    elif h[i] == '}':
                        depth -= 1
                        if depth == 0: end = i + 1; break
                    i += 1
                import json
                data = json.loads(h[p:end])
                play_id = urllib.parse.unquote(data.get('url', ''))
                if data.get('from') == 'juhe' and play_id:
                    parse_url = 'https://api.apiimg.com/show/super.php?id=' + urllib.parse.quote(play_id, safe='=')
                    ph = self._get(parse_url)
                    if ph:
                        ms = re.findall(r'https?://[^"\'<>]+?\.(?:m3u8|mp4)[^"\'<>]*', ph, re.I)
                        ms += re.findall(r'https?:\\/\\/[^"\'<>]+?\.(?:m3u8|mp4)[^"\'<>]*', ph, re.I)
                        ms = list(dict.fromkeys([x.replace('\\/', '/') for x in ms if 'api.apiimg.com' not in x and 'playbg.php' not in x and x.lower().startswith('http')]))
                        if ms:
                            hd = {"User-Agent": self.headers["User-Agent"], "Referer": parse_url}
                            return {"parse": 0, "url": ms[0], "header": hd}
        except: pass
        return {"parse": 1, "url": url, "header": self.headers}