Files
MusicServer/sdk/apis/apple.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

73 lines
3.4 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.
// Apple Music 取流(上游 apple.go GetDownloadURL)
// ⚠️ Apple 只提供 **preview 试听片段**(audio-ssl.itunes.apple.com,AAC/m4a,约 30-90 秒,实测单文件 ~3MB),
// 完整曲目是 FairPlay DRM 加密流(extendedAssetUrls.enhancedHls),需要 gamdl 等外部工具解密,
// 本 SDK 不提供。因此这里**任何请求档位都返回 type='preview'**,并在注释/响应里明确标注,不假装是无损完整曲。
// 歌曲的 previews[0].url 来自 catalog 歌曲详情(搜索结果缓存里的 previewUrl 也会直接复用,同上游 s.URL 逻辑)。
// 统一接口约定:apis('apple').getMusicUrl(songInfo, type) → { promise, cancelHttp }
// promise resolve → { type:'preview', url, ext:'m4a', preview:true, songDuration, isrc }
// (songDuration 是歌曲完整时长,试听片段本身约 30-90 秒)
// type 请求参数不影响结果(只有 preview 一档);supportQualitys 声明为 ['128k'] 以对齐
// /sources 与 /url?quality 枚举,返回的实际档位以 type='preview' 为准。
import { appleAmpGetTracked, extractAppleID, getAppleConfig } from '../apple/util.ts'
export const supportQualitys = ['128k']
const isAudioURL = (value: any): boolean => /^https?:/i.test(String(value ?? ''))
/** 详情 → preview 结果(内部/测试用) */
export const previewFromAttributes = (attr: any) => {
const url = String(attr?.previews?.[0]?.url ?? '')
if (!url) {
throw new Error('apple music: no preview URL available (完整曲目受 DRM 保护,SDK 仅提供 30-90 秒试听片段)')
}
return {
type: 'preview',
url,
ext: 'm4a',
preview: true,
// 注意:这是歌曲完整时长(catalog durationInMillis),不是试听片段长度——试听片段约 30-90 秒
songDuration: Math.round(Number(attr?.durationInMillis ?? 0) / 1000),
isrc: attr?.isrc || '',
}
}
/** 按 song id 拉取 preview(上游 GetDownloadURL → Parse) */
export const getPreviewUrl = async(songId: string, tracker?: { current: any }) => {
const cfg = getAppleConfig()
const body: any = await appleAmpGetTracked(
`/v1/catalog/${cfg.storefront}/songs/${songId}`,
{ extend: 'extendedAssetUrls' },
tracker,
)
const data = body?.data ?? []
if (!data.length) throw new Error(`apple song not found: ${songId}`)
return previewFromAttributes(data[0]?.attributes)
}
export const getMusicUrl = (songInfo: any, _type?: string) => {
const requestObj: any = { promise: null as any, cancelHttp() {} }
const tracker: { current: any } = { current: null }
requestObj.cancelHttp = () => {
try {
tracker.current?.cancelHttp?.()
} catch {
// ignore
}
tracker.current = null
}
requestObj.promise = (async() => {
// 上游 GetDownloadURL:Song.URL 已有直链时直接复用(搜索结果里的 previewUrl / 链接解析结果)
if (isAudioURL(songInfo?.previewUrl)) return { type: 'preview', url: String(songInfo.previewUrl), ext: 'm4a', preview: true }
if (isAudioURL(songInfo?.url)) return { type: 'preview', url: String(songInfo.url), ext: 'm4a', preview: true }
const songId = extractAppleID(String(songInfo?.songmid ?? songInfo?.id ?? ''), 'song')
if (!songId) throw new Error('apple 歌曲 id 无效(需要数字 song id 或 Apple Music 歌曲链接)')
return getPreviewUrl(songId, tracker)
})()
return requestObj
}
export default { getMusicUrl, getPreviewUrl, previewFromAttributes, supportQualitys }