#Kyele
import sys
import json
import time
import re
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 ---
    requests.RequestException = requests.exceptions.RequestException
    requests.Timeout = requests.exceptions.Timeout
    requests.ConnectionError = requests.exceptions.ConnectionError
    requests.HTTPError = requests.exceptions.HTTPError
    requests.SSLError = requests.exceptions.SSLError
    def _req_mount(self, prefix, adapter):
        pass
    requests.Session.mount = _req_mount
    requests.Session.proxies = {}
    requests.Session.trust_env = True
    class _CookieDict(dict):
        def set(self, name, value, **kw):
            self[name] = value
        def get_dict(self):
            return dict(self)
        def clear(self):
            dict.clear(self)
    _orig_sess_init = requests.Session.__init__
    def _sess_init(self, *a, **kw):
        _orig_sess_init(self, *a, **kw)
        self.cookies = _CookieDict(self.cookies)
        self.proxies = {}
    requests.Session.__init__ = _sess_init
    _orig_sess_request = requests.Session.request
    def _sess_request(self, method, url, headers=None, timeout=15, data=None, json=None, params=None, cookies=None, verify=False, allow_redirects=True, stream=False, **kw):
            # 映匣适配：容忍 IncompleteRead（分片传输截断时用已读部分），并自动解 gzip/deflate
            import urllib.parse as _up2
            import urllib.request as _ur2
            import urllib.error as _ue2
            import http.client as _hc2
            import json as _json2
            if params:
                _qs = _up2.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 = _json2.dumps(json, ensure_ascii=False).encode("utf-8")
                _hd.setdefault("Content-Type", "application/json")
            elif isinstance(data, dict):
                _body = _up2.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()))
            _to = timeout
            if isinstance(_to, (tuple, list)):
                _to = max(_to)
            if _to and float(_to) > 120:
                _to = float(_to) / 1000.0
            _last_exc = None
            for _attempt in range(2):
                _req = _ur2.Request(url, data=_body, headers=_hd, method=method.upper())
                try:
                    with self._opener.open(_req, timeout=_to or 15) as _resp:
                        try:
                            _content = _resp.read()
                        except _hc2.IncompleteRead as _ire:
                            _content = _ire.partial or b""
                        _hdrs = dict(_resp.headers.items())
                        _final = _resp.geturl()
                        _status = _resp.status
                except _ue2.HTTPError as _he:
                    try:
                        _content = _he.read()
                    except _hc2.IncompleteRead as _ire:
                        _content = _ire.partial or b""
                    except Exception:
                        _content = b""
                    _hdrs = dict(_he.headers.items()) if _he.headers else {}
                    _final = _he.geturl() if hasattr(_he, "geturl") else url
                    _status = _he.code
                except Exception as _exc:
                    _last_exc = _exc
                    continue
                try:
                    _ce = (_hdrs.get("Content-Encoding", "") or "").lower()
                    if "gzip" in _ce:
                        import gzip as _gz
                        _content = _gz.decompress(_content)
                    elif "deflate" in _ce:
                        import zlib as _zl
                        _content = _zl.decompress(_content)
                except Exception:
                    pass
                for _c in self._jar:
                    self.cookies[_c.name] = _c.value
                return requests.Response(_status, _hdrs, _content, _final, dict(self.cookies))
            raise requests.exceptions.ConnectionError(str(_last_exc))
    requests.Session.request = _sess_request
    def _resp_apparent_encoding(self):
        _ct = self.headers.get("Content-Type", "") or ""
        return _ct.split("charset=")[-1].split(";")[0].strip() if "charset=" in _ct else "utf-8"
    requests.Response.apparent_encoding = property(_resp_apparent_encoding)
    requests.Response.ok = property(lambda self: self.status_code < 400)
    requests.Response.reason = property(lambda self: "")
    def _resp_apparent_encoding(self):
        _ct = self.headers.get("Content-Type", "") or ""
        return _ct.split("charset=")[-1].split(";")[0].strip() if "charset=" in _ct else "utf-8"
    requests.Response.apparent_encoding = property(_resp_apparent_encoding)
    requests.Response.ok = property(lambda self: self.status_code < 400)
    requests.Response.reason = property(lambda self: "")
    class _ReqUtils:
        @staticmethod
        def quote(s, safe="/"):
            import urllib.parse as _up
            return _up.quote(s, safe=safe)
        @staticmethod
        def unquote(s):
            import urllib.parse as _up
            return _up.unquote(s)
    requests.utils = _ReqUtils
sys.path.append('..')
from base.spider import Spider
class Spider(Spider):
    def __init__(self):
        super().__init__()
        self.base = 'https://www.pandalive.co.kr'; self.api = 'https://api.pandalive.co.kr'; self.session = requests.Session()
        self.ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36'
        self.common_headers = {'User-Agent': self.ua, 'Accept': 'application/json, text/plain, */*', 'Origin': self.base, 'Referer': self.base + '/'}
        self.x_device_info = {"t": "webPc", "v": "1.0", "ui": "0", "ck": {"sessKeyAsp": ""}}; self.extra_cookie = ''
    def init(self, extend=""):
        try:
            if extend:
                cfg = extend if isinstance(extend, dict) else json.loads(extend)
                self.x_device_info = cfg.get('x_device_info', self.x_device_info); self.extra_cookie = cfg.get('cookie', '')
        except Exception: pass
        try:
            self.session.headers.update(self.common_headers)
            if self.extra_cookie: self.session.headers['Cookie'] = self.extra_cookie
            self.session.get(self.base, timeout=8); self._app_token()
        except Exception: pass
        return self
    def getName(self): return 'PandaLive'
    def isVideoFormat(self, url): return url.endswith('.m3u8') or url.endswith('.mp4')
    def manualVideoCheck(self): return False
    def destroy(self):
        try: self.session.close()
        except Exception: pass
    def _app_token(self):
        headers = self._with_x_device_info(dict(self.session.headers))
        return self.session.get(f'{self.api}/v1/member/app_token', headers=headers, timeout=8)
    def _list_live(self, page=None, page_size=None, order_by='user', only_new='N'):
        if page_size is None: page_size = 60
        limit = page_size; offset = 0 if page is None else max(0, (page - 1) * limit)
        headers = self._with_x_device_info(dict(self.session.headers))
        headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
        data = {'orderBy': order_by, 'onlyNewBj': only_new, 'limit': str(limit), 'offset': str(offset)}
        try:
            r = self.session.post(f'{self.api}/v1/live', data=data, headers=headers, timeout=8)
            j = {}
            try: j = r.json()
            except Exception: pass
            if isinstance(j, dict) and isinstance(j.get('list'), list) and len(j['list']) > 0: return j
        except Exception: pass
        try: return self.session.get(f'{self.api}/v1/live', timeout=8).json()
        except Exception: return {}
    def _list_live_page(self, page, page_size, order_by='user', only_new='N'):
        j = self._list_live(page=page, page_size=page_size, order_by=order_by, only_new=only_new)
        return j.get('list', []) if isinstance(j, dict) else []
    def _list_live_aggregate(self, max_pages=5, page_size=60, min_expect=60, order_by='user', only_new='N'):
        try:
            cache_key = f'pandalive_agg_v2_{order_by}_{only_new}'; cached = self.getCache(cache_key)
            if isinstance(cached, dict) and isinstance(cached.get('list'), list): return cached['list']
        except Exception: pass
        seen = set(); result = []
        first = self._list_live(page=None, page_size=page_size, order_by=order_by, only_new=only_new)
        items = first.get('list', []) if isinstance(first, dict) else []
        for it in items:
            code = it.get('code') or it.get('userId')
            if code and code not in seen: seen.add(code); result.append(it)
        p = 1
        while len(result) < min_expect and p <= max_pages:
            page_items = self._list_live_page(page=p, page_size=page_size, order_by=order_by, only_new=only_new)
            added = 0
            for it in page_items:
                code = it.get('code') or it.get('userId')
                if code and code not in seen: seen.add(code); result.append(it); added += 1
            if added == 0 and p > 1: break
            p += 1
        try:
            payload = {"expiresAt": int(time.time()) + 20, "list": result}
            self.setCache(f'pandalive_agg_v2_{order_by}_{only_new}', payload)
        except Exception: pass
        return result
    def _live_play(self, play_id):
        body = {'play_id': play_id, 'device': 'webPc', 'player': 'ivs'}
        headers = self._with_x_device_info(dict(self.session.headers))
        headers['Content-Type'] = 'application/json'; headers['Referer'] = f'{self.base}/play/{play_id.split("_")[0]}'
        r = self.session.post(f'{self.api}/v1/live/play', headers=headers, data=json.dumps(body), timeout=8)
        if r.status_code != 200:
            try: self._app_token()
            except Exception: pass
            r = self.session.post(f'{self.api}/v1/live/play', headers=headers, data=json.dumps(body), timeout=8)
        try: return r.json()
        except Exception: return {'result': False, 'status': r.status_code, 'text': r.text}
    def _with_x_device_info(self, headers):
        try: headers['x-device-info'] = json.dumps(self.x_device_info, separators=(',', ':'))
        except Exception: headers['x-device-info'] = '{"t":"webPc","v":"1.0","ui":"0","ck":{"sessKeyAsp":""}}'
        return headers
    def homeContent(self, filter):
        classes = [{"type_name": "LIVE", "type_id": "live"}]
        if filter:
            filters = {"live": [{"key": "sort", "value": [
                            {"n": "观看次数", "v": "user-N"}, {"n": "热门", "v": "hot-N"},
                            {"n": "最新", "v": "new-N"}, {"n": "新人", "v": "user-Y"}
                        ]}]}
        else: filters = {}
        return {"class": classes, "filters": filters}
    def homeVideoContent(self):
        items = self._list_live_aggregate(max_pages=3, page_size=60, min_expect=48, order_by='user', only_new='N')
        if not items:
            items = (self._list_live().get('list', []))
        return {"list": [self._to_vod(it) for it in items[:48]]}
    def categoryContent(self, tid, pg, filter, extend):
        page_size = 24; p = 1
        try: p = int(pg)
        except Exception: pass
        order_by, only_new = 'user', 'N'
        try:
            if isinstance(extend, str):
                try: extend = json.loads(extend) if extend.strip().startswith('{') else {}
                except Exception: extend = {}
            if isinstance(extend, dict):
                s = extend.get('sort')
                if isinstance(s, str) and '-' in s:
                    ab = s.split('-', 1)
                    if len(ab) == 2: order_by, only_new = (ab[0] or 'user'), (ab[1] or 'N')
            if (order_by, only_new) == ('user', 'N') and (tid or '').lower() != 'live':
                order_by, only_new = self._tid_to_sort(tid)
        except Exception: pass
        server_items = self._list_live_page(page=p, page_size=page_size, order_by=order_by, only_new=only_new)
        if len(server_items) >= page_size:
            return {"page": p, "pagecount": 99999, "limit": page_size, "total": 999999, "list": [self._to_vod(it) for it in server_items]}
        all_items = self._list_live_aggregate(max_pages=12, page_size=100, min_expect=120, order_by=order_by, only_new=only_new)
        total = len(all_items)
        if total <= 0:
            base_list = self._list_live().get('list', [])
            total = len(base_list)
            if total <= 0: return {"page": p, "pagecount": 1, "limit": page_size, "total": 0, "list": []}
            start = (p - 1) * page_size; end = start + page_size
            part = base_list[start:end]
            videos = [self._to_vod(it) for it in part]
            return {"page": p, "pagecount": (total + page_size - 1)//page_size, "limit": page_size, "total": total, "list": videos}
        start = ((p - 1) * page_size) % total; part = []
        for i in range(page_size): part.append(all_items[(start + i) % total])
        return {"page": p, "pagecount": 99999, "limit": page_size, "total": 999999, "list": [self._to_vod(it) for it in part]}
    def _tid_to_sort(self, tid):
        t = (tid or '').lower()
        if t == 'live_newbj': return 'user', 'Y'
        if t == 'live_hot': return 'hot', 'N'
        if t == 'live_new': return 'new', 'N'
        return 'user', 'N'
    def detailContent(self, ids):
        vid = ids[0]; parts = vid.split('|'); play_id = parts[0]
        user_id = parts[1] if len(parts) > 1 else play_id.split('_')[0]
        title = parts[2] if len(parts) > 2 else user_id
        vod = {"vod_id": vid, "vod_name": title, "vod_pic": "", "type_name": "LIVE", "vod_year": "", "vod_area": "", "vod_remarks": "PandaLive", "vod_actor": "", "vod_director": "", "vod_content": title, "vod_play_from": "IVS", "vod_play_url": f"直播${vid}"}
        return {"list": [vod]}
    def searchContent(self, key, quick, pg="1"):
        items = self._list_live().get('list', [])
        key_l = key.lower(); result = []
        for it in items:
            title = str(it.get('title', '')); user_id = str(it.get('userId', '')); user_nick = str(it.get('userNick', ''))
            if key_l in title.lower() or key_l in user_id.lower() or key_l in user_nick.lower():
                result.append(self._to_vod(it))
        return {"list": result, "page": 1}
    def playerContent(self, flag, id, vipFlags):
        try:
            vid = id; parts = vid.split('|'); play_id = parts[0]
            j = self._live_play(play_id); m3u8 = self._find_first_m3u8(j) if isinstance(j, dict) else ''
            if not m3u8: return {"parse": 1, "playUrl": "", "url": f"{self.base}/play/{play_id.split('_')[0]}", "header": self._play_headers()}
            return {"parse": 0, "playUrl": "", "url": m3u8, "header": self._play_headers()}
        except Exception: return {"parse": 1, "playUrl": "", "url": f"{self.base}", "header": self._play_headers()}
    def liveContent(self, url):
        try:
            play_id = url; j = self._live_play(play_id)
            m3u8 = self._find_first_m3u8(j)
            if m3u8: return {"parse": 0, "url": m3u8, "header": self._play_headers()}
        except Exception: pass
        return {"parse": 1, "url": f"{self.base}/play/{url}", "header": self._play_headers()}
    def localProxy(self, param):
        action = param.get('action') if isinstance(param, dict) else None
        if action == 'play':
            play_id = param.get('play_id', ''); j = self._live_play(play_id)
            m3u8 = self._find_first_m3u8(j)
            if m3u8: return self._redirect(m3u8)
        return None
    def _redirect(self, url):
        return {"code": 302, "headers": {"Location": url}}
    def _find_first_m3u8(self, obj):
        try:
            text = json.dumps(obj, ensure_ascii=False)
            m = re.search(r'https?://[^\s"\\]+\.m3u8[^\s"\\]*', text)
            if m: return m.group(0)
        except Exception: pass
        return ''
    def _play_headers(self):
        return {'User-Agent': self.ua, 'Referer': self.base + '/', 'Origin': self.base}
    def _to_vod(self, it):
        title = it.get('title') or it.get('userNick') or it.get('userId') or 'LIVE'
        pic = it.get('thumbUrl') or it.get('ivsThumbnail') or ''
        user_id = it.get('userId', ''); play_id = it.get('code', user_id); vod_id = f"{play_id}|{user_id}|{title}"
        remarks = f"观众 {it.get('user', 0)} | 点赞 {it.get('likeCnt', 0)}"
        return {'vod_id': vod_id, 'vod_name': title, 'vod_pic': pic, 'vod_remarks': remarks}