import re
import sys
import json
import time
import base64
from concurrent.futures import ThreadPoolExecutor, as_completed
from urllib.parse import quote
try:
    from requests import Session, adapters
    from urllib3.util.retry import Retry
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 ---
    Session = _Session
    class _Adapters:
        class HTTPAdapter:
            def __init__(self, *a, **k):
                pass
    adapters = _Adapters
    class Retry:
        def __init__(self, *a, **k):
            pass
sys.path.append('..')
from base.spider import Spider

class Spider(Spider):
    def init(self, extend=""):
        self.host = "https://music.yingzi.ee"
        self.session = Session()
        adapter = adapters.HTTPAdapter(
            max_retries=Retry(total=3, backoff_factor=0.5, status_forcelist=[429, 500, 502, 503, 504]),
            pool_connections=30, pool_maxsize=50
        )
        self.session.mount("http://", adapter)
        self.session.mount("https://", adapter)
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36",
            "Referer": self.host + "/"
        }
        self.session.headers.update(self.headers)
        self.netease_headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
            "Referer": "https://music.163.com/"
        }
        self._cache = {}
        self._cache_ttl = 300

        # 一级分类 + 二级榜单
        self.platforms = {
            "netease": {
                "name": "网易云音乐",
                "playlists": {
                    "3778678": "热歌榜",
                    "19723756": "飙升榜",
                    "3779629": "新歌榜",
                    "2884035": "原创榜"
                }
            },
            "tencent": {
                "name": "QQ音乐",
                "playlists": {
                    "热歌": "热歌榜",
                    "新歌": "新歌榜",
                    "飙升": "飙升榜",
                    "流行": "流行榜"
                }
            },
            "kugou": {
                "name": "酷狗音乐",
                "playlists": {
                    "热歌": "热歌榜",
                    "新歌": "新歌榜",
                    "飙升": "飙升榜",
                    "DJ": "DJ榜"
                }
            }
        }

    def getName(self): return "影子音乐"
    def isVideoFormat(self, url): return bool(re.search(r'\.(mp3|m4a|flac|wav|ogg)(\?|$)', url or "", re.I))
    def manualVideoCheck(self): return False
    def destroy(self): self.session.close()

    # ==================== 首页 ====================
    def homeContent(self, filter):
        classes = []
        filters = {}
        for source, cfg in self.platforms.items():
            classes.append({"type_name": cfg["name"], "type_id": source})
            fvals = [{"n": v, "v": k} for k, v in cfg["playlists"].items()]
            if fvals:
                filters[source] = [{"key": "list", "name": "榜单", "value": fvals}]
        return {"class": classes, "filters": filters, "list": []}

    def homeVideoContent(self): return {"list": []}

    # ==================== 分类页：直接返回歌曲列表 ====================
    def categoryContent(self, tid, pg, filter, extend):
        pg = int(pg or 1)
        source = tid
        # 默认榜单
        list_id = extend.get("list", "")
        if not list_id and source in self.platforms:
            list_id = next(iter(self.platforms[source]["playlists"].keys()))
        list_name = self.platforms.get(source, {}).get("playlists", {}).get(list_id, "精选")

        songs = []
        if source == "netease" and list_id.isdigit():
            songs = self._get_netease_playlist(list_id)
        elif source in ("tencent", "kugou"):
            # QQ/酷狗用搜索模拟榜单
            songs = self._do_search(source, list_id, pg)

        if not songs:
            return {"list": [], "page": pg, "pagecount": 1, "limit": 100, "total": 0}

        items = []
        for song in songs[:200]:
            if source == "netease":
                sid = str(song.get("id", ""))
                name = song.get("name", "未知")
                artists = ", ".join([a.get("name", "") for a in song.get("ar", [])])
                pic = song.get("al", {}).get("picUrl", "")
                # 网易云歌单内歌曲无sign，detail时按需搜索获取
                vod_id = self.e64(f"{source}###{sid}###{name}###{pic}")
            else:
                sid = song.get("id", "")
                name = song.get("name", "未知")
                artists = ", ".join(song.get("artist", []))
                pic = self._get_pic(source, song)
                sign = song.get("sign", "")
                vod_id = self.e64(f"{source}###{sid}###{sign}###{name}###{pic}")

            display_name = f"{name} - {artists}" if artists else name
            items.append({
                "vod_id": vod_id,
                "vod_name": display_name,
                "vod_pic": pic or "",
                "style": {"type": "rect", "ratio": 1}
            })

        # 网易云歌单不分页；QQ/酷狗搜索可翻页
        pagecount = 999 if source in ("tencent", "kugou") else 1
        return {
            "list": items,
            "page": pg,
            "pagecount": pagecount,
            "limit": 100,
            "total": len(items)
        }

    # ==================== 详情页：单曲直接 playable ====================
    def detailContent(self, ids):
        raw = self.d64(ids[0])
        parts = raw.split("###")

        # 格式: source###sid###sign###name###pic  (QQ/酷狗有sign)
        # 格式: source###sid###name###pic        (网易云无sign)
        if len(parts) == 5:
            source, sid, sign, name, pic = parts
        elif len(parts) == 4:
            source, sid, name, pic = parts
            sign = self._get_sign(source, sid, name)
        else:
            return {"list": []}

        play_id = self.e64(f"{source}###{sid}###{sign}")
        vod = {
            "vod_id": ids[0],
            "vod_name": name,
            "vod_pic": pic,
            "vod_play_from": source,
            "vod_play_url": f"播放${play_id}"
        }
        return {"list": [vod]}

    # ==================== 搜索：三平台并发 ====================
    def searchContent(self, key, quick, pg="1"):
        pg = int(pg or 1)
        all_songs = []

        with ThreadPoolExecutor(max_workers=3) as ex:
            futures = {ex.submit(self._do_search, s, key, pg): s for s in self.platforms.keys()}
            for f in as_completed(futures):
                try:
                    all_songs.extend(f.result())
                except:
                    pass

        seen = set()
        unique = []
        for s in all_songs:
            k = f"{s.get('source')}:{s.get('id')}"
            if k not in seen:
                seen.add(k)
                unique.append(s)

        res = []
        for s in unique:
            source = s.get("source", "")
            sid = s.get("id", "")
            name = s.get("name", "未知")
            artists = ", ".join(s.get("artist", []))
            pic = self._get_pic(source, s)
            sign = s.get("sign", "")
            vod_id = self.e64(f"{source}###{sid}###{sign}###{name}###{pic}")
            res.append({
                "vod_id": vod_id,
                "vod_name": f"{name} - {artists}" if artists else name,
                "vod_pic": pic,
                "style": {"type": "rect", "ratio": 1}
            })

        return {"list": res, "page": pg}

    # ==================== 播放器 ====================
    def playerContent(self, flag, id, vipFlags):
        raw = self.d64(id)
        parts = raw.split("###")

        if len(parts) == 3:
            source, sid, sign = parts
        else:
            return {"parse": 0, "url": "", "header": self.headers}

        url = self._get_play_url(source, sid, sign)
        if not url:
            return {"parse": 0, "url": "", "header": self.headers}

        result = {
            "parse": 0,
            "url": url,
            "header": self.headers.copy()
        }
        if "qq.com" in url:
            result["header"]["Referer"] = "https://y.qq.com/"
        elif "kugou" in url:
            result["header"]["Referer"] = "https://www.kugou.com/"

        # 网易云歌词
        if source == "netease":
            lrc = self._get_netease_lyric(sid)
            if lrc:
                result["lrc"] = lrc

        return result

    # ==================== 本地代理 ====================
    def localProxy(self, param):
        url = param.get("url", "")
        if not url:
            return None
        try:
            h = self.headers.copy()
            if "music.126.net" in url:
                h.update(self.netease_headers)
            elif "y.gtimg.cn" in url:
                h["Referer"] = "https://y.qq.com/"
            elif "kugou" in url:
                h["Referer"] = "https://www.kugou.com/"

            r = self.session.get(url, headers=h, timeout=10)
            ct = r.headers.get("Content-Type", "image/jpeg")
            if "image" not in ct:
                ct = "image/jpeg"
            return [200, ct, r.content, {}]
        except:
            return None

    # ==================== 内部工具 ====================
    def _get_netease_playlist(self, list_id):
        ck = f"pl_netease_{list_id}"
        if ck in self._cache and time.time() - self._cache[ck]["time"] < self._cache_ttl:
            return self._cache[ck]["data"]
        try:
            r = self.session.get(f"{self.host}/api.php?types=playlist&id={list_id}&source=netease", timeout=10)
            tracks = r.json().get("playlist", {}).get("tracks", [])
            self._cache[ck] = {"data": tracks, "time": time.time()}
            return tracks
        except:
            return []

    def _do_search(self, source, keyword, pg):
        ck = f"search_{source}_{keyword}_{pg}"
        if ck in self._cache and time.time() - self._cache[ck]["time"] < self._cache_ttl:
            return self._cache[ck]["data"]
        try:
            r = self.session.get(
                f"{self.host}/api.php?types=search&source={source}&count=30&pages={pg}&name={quote(keyword)}",
                timeout=10
            )
            data = r.json()
            if isinstance(data, list):
                for item in data:
                    item["source"] = source
                self._cache[ck] = {"data": data, "time": time.time()}
                return data
        except:
            pass
        return []

    def _get_sign(self, source, sid, name):
        ck = f"sign_{source}_{sid}"
        if ck in self._cache and time.time() - self._cache[ck]["time"] < self._cache_ttl:
            return self._cache[ck]["data"]
        try:
            r = self.session.get(
                f"{self.host}/api.php?types=search&source={source}&count=10&pages=1&name={quote(name or 'a')}",
                timeout=10
            )
            for song in r.json():
                if str(song.get("id")) == str(sid):
                    sign = song.get("sign", "")
                    self._cache[ck] = {"data": sign, "time": time.time()}
                    return sign
        except:
            pass
        return ""

    def _get_play_url(self, source, sid, sign):
        if not sign:
            return ""
        try:
            r = self.session.get(f"{self.host}/api.php?types=url&id={sid}&source={source}&sign={sign}", timeout=10)
            url = r.json().get("url", "")
            return url.replace("\/", "/") if url else ""
        except:
            return ""

    def _get_netease_lyric(self, sid):
        try:
            r = self.session.get(
                f"https://music.163.com/api/song/lyric?id={sid}&lv=1&kv=1&tv=-1",
                headers=self.netease_headers, timeout=10
            )
            return r.json().get("lrc", {}).get("lyric", "")
        except:
            return ""

    def _get_pic(self, source, song):
        pic_id = song.get("pic_id", "")
        if source == "tencent":
            return f"https://y.gtimg.cn/music/photo_new/T002R300x300M000{pic_id}.jpg"
        if source == "kugou" and len(pic_id) >= 4:
            return f"https://singerimg.kugou.com/uploadpic/softhead/400/{pic_id[:2]}/{pic_id[:4]}/{pic_id}.jpg"
        return ""

    def e64(self, text):
        return base64.b64encode(text.encode("utf-8")).decode("utf-8")

    def d64(self, text):
        return base64.b64decode(text.encode("utf-8")).decode("utf-8")
