Files
MusicServer/app/sdk/platforms/mg/lyric.py
T
snowy-yang dfc06bf4de feat(python): 完成 Python 全量迁移——剩余平台、登录通道与公共层修复
- 新增 5sing/apple/xm/me/qt/lr 平台包与取流实现(含 xm-sign、MissEvan 签名、蜻蜓 HMAC、Apple developer token)
- 新增 wy/kg/tx/lr 登录:扫码、手机号、账密(kg 20028 风控指纹)、cookie 绑定、kg/tx 续期
- request 层:保留多值 Set-Cookie;JSON body 紧凑化(修复 kg 网关验签);直连不经系统代理(对齐 Node)
- 登录路由统一 Body(embed=True),修复扫码轮询 422;lifespan 接入登录续期调度
- ruff/format/pyright 全绿;14 平台真实联网与 HTTP 路由验证通过
2026-10-23 21:55:45 +08:00

50 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""咪咕歌词(对应 sdk/mg/lyric.ts)。
resourceinfo.do 取 lrcUrl/lyricUrl 再下载文本;歌词 CDN 只认 y.migu.cn 的 Referer,
与资源接口的 music.migu.cn Referer 不同,所以单独发一次请求。
上游对应 music-lib/migu/lyric.go。
"""
from __future__ import annotations
import re
from typing import Any
from app.sdk.common.request import http_fetch
from app.sdk.platforms.mg.util import SONG_DETAIL_URL, build_url, migu_get
LRC_REFERER = "https://y.migu.cn/"
LRC_UA = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36"
)
_HTTP_PREFIX = re.compile(r"^http://")
def content_id_of(song_info: dict[str, Any]) -> str:
"""从 extra.content_id 或 songmid(contentId|resourceType|formatType)取 contentId。"""
extra = song_info.get("extra") or {}
if isinstance(extra, dict) and extra.get("content_id"):
return str(extra["content_id"])
raw_id = str(song_info.get("songmid") or song_info.get("id") or "")
return raw_id.split("|")[0]
async def get_lyric(song_info: dict[str, Any]) -> dict[str, str]:
content_id = content_id_of(song_info)
if not content_id:
raise RuntimeError("migu 歌曲 id 无效")
body = await migu_get(build_url(SONG_DETAIL_URL, {"resourceId": content_id, "resourceType": "2"}))
resource = body.get("resource") if isinstance(body, dict) else None
if not resource:
raise RuntimeError("migu 歌词资源不存在")
first = resource[0] if isinstance(resource[0], dict) else {}
lrc_url = str(first.get("lrcUrl") or "") or str(first.get("lyricUrl") or "")
if not lrc_url:
return {"lyric": "", "tlyric": ""}
lrc_url = _HTTP_PREFIX.sub("https://", lrc_url)
resp = await http_fetch(lrc_url, headers={"User-Agent": LRC_UA, "Referer": LRC_REFERER}, timeout=20_000)
if resp.status_code != 200:
raise RuntimeError(f"migu 歌词下载失败 {resp.status_code}")
return {"lyric": resp.body if isinstance(resp.body, str) else "", "tlyric": ""}