Files
MusicServer/sdk/joox/lyric.ts
T
snowy-yang 33b96f92c4 feat(sdk): 新增咪咕/千千/JOOX/哔哩哔哩/5sing/Apple Music 六平台
平台注册表由 9 个扩到 15 个,同时补齐 sdk/types.ts 的类型与
OpenAPI 文档里的能力位声明。

- migu:命中 Z3D/I3D 加密候选时不返回直链,改由服务端解密代理出流,
  SDK 侧只负责登记票据,避免客户端拿到无法播放的密文
- apple:只提供 preview 试听片段(完整曲为 DRM),标 noAggregate
  排除在跨平台精确匹配之外,防止用它顶掉其它平台的可播结果
- 其余平台按既有 PlatformModule 约定实现搜索/专辑/歌单/歌词

为什么合并成一次提交:六平台共用同一份注册表与能力位声明,
拆开会产生互相引用、单独无法通过类型检查的中间态。
2026-10-03 00:26:32 +08:00

85 lines
3.6 KiB
TypeScript
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.
// JOOX 歌词(上游 joox/lyric.go 移植)
// 端点:GET https://api.joox.com/web-fcgi-bin/web_lyric?musicid=<id>&country=sg&lang=zh_cn
// 响应形如 MusicJsonCallback({"code":0,"lyric":"<base64>"}),lyric 为 base64 UTF-8 歌词。
// 实测(2026-10-02)该旧接口仍可用(web_get_songinfo 已 404,但 web_lyric 未下线)。
// 兜底:openjoox2 /track 详情的 lrc_content(同为 base64),旧接口失败时使用。
// 解码:base64 → UTF-8 → HTML 实体(&amp; 等)→ { lyric, tlyric: '' }
// 统一接口约定:{ promise, cancelHttp };promise resolve → { lyric, tlyric }
import { httpFetch } from '../request.ts'
import { jooxHeaders, decodeJooxLyric, normalizeJooxID, firstNonEmpty } from './util.ts'
import { buildTrackUrl } from './musicInfo.ts'
export const LEGACY_LYRIC_URL = 'https://api.joox.com/web-fcgi-bin/web_lyric'
/** 解析 MusicJsonCallback(...) / 裸 JSON 包裹 */
export const parseLyricPayload = (body: any): any => {
if (body && typeof body === 'object') return body
let text = String(body ?? '')
const idx = text.indexOf('MusicJsonCallback(')
if (idx >= 0) {
text = text.slice(idx + 'MusicJsonCallback('.length)
text = text.replace(/\)\s*;?\s*$/, '')
}
try {
return JSON.parse(text)
} catch {
return null
}
}
/** 上游 GetLyrics 的纯逻辑部分:web_lyric 响应体 → 歌词文本 */
export const parseLegacyLyric = (statusCode: number, body: any): string => {
if (statusCode != 200) throw new Error(`joox 歌词获取失败(HTTP ${statusCode})`)
const payload = parseLyricPayload(body)
if (!payload) throw new Error('joox 歌词解析失败')
const raw = String(firstNonEmpty(payload.lyric, ''))
if (Number(payload.code ?? 0) !== 0 || !raw) throw new Error('joox 歌词不存在或为空')
return decodeJooxLyric(raw)
}
/** 上游 GetLyrics 通道(非取消句柄版,测试/内部用) */
export const getLegacyLyric = async(songId: string): Promise<string> => {
const url = `${LEGACY_LYRIC_URL}?musicid=${encodeURIComponent(songId)}&country=sg&lang=zh_cn`
const { statusCode, body } = await httpFetch(url, { headers: jooxHeaders(), timeout: 20000 }).promise as any
return parseLegacyLyric(statusCode, body)
}
export const getLyric = (songInfo: any, ..._rest: any[]) => {
const requestObj: { promise: any, cancelHttp: () => void } = { promise: null, cancelHttp() {} }
let current: any = null
requestObj.cancelHttp = () => {
if (current?.cancelHttp) current.cancelHttp()
}
const songId = normalizeJooxID(songInfo?.songmid ?? songInfo?.id ?? songInfo?.songid ?? '')
requestObj.promise = (async() => {
if (!songId) throw new Error('joox 歌曲 id 无效')
// 1) 上游 web_lyric 通道
let legacyErr: any = null
try {
current = httpFetch(
`${LEGACY_LYRIC_URL}?musicid=${encodeURIComponent(songId)}&country=sg&lang=zh_cn`,
{ headers: jooxHeaders(), timeout: 20000 },
)
const { statusCode, body } = await current.promise
return { lyric: parseLegacyLyric(statusCode, body), tlyric: '' }
} catch (err) {
legacyErr = err
}
// 2) 兜底:openjoox2 详情 lrc_content(base64)
current = httpFetch(buildTrackUrl(songId), { headers: jooxHeaders(), timeout: 20000 })
const { statusCode, body } = await current.promise as any
const raw = String(firstNonEmpty(body?.lrc_content, ''))
if (statusCode != 200 || !raw) throw legacyErr ?? new Error('joox 歌词不存在或为空')
return { lyric: decodeJooxLyric(raw), tlyric: '' }
})()
return requestObj
}
export default { getLyric, getLegacyLyric, parseLyricPayload, parseLegacyLyric }