仰望星辰工作室

starmusic-mobile

1/**
2 * 网易云音乐 (wy) 歌单导入
3 *
4 * 实现说明(迁移自 lx-music-mobile/src/utils/musicSdk/wy/songList.js):
5 * - 原项目使用 linuxapi 加密接口(/api/linux/forward),但本环境与 starmusic 的
6 * wy.js 一致,加密接口会被风控,因此改用网易明文老接口:
7 * 1. /api/v6/playlist/detail?id=xxx 获取歌单信息 + 全部 trackIds
8 * 2. /api/song/detail?id=&ids=[...] 按 trackIds 分批拉取完整歌曲详情
9 * 明文老接口免登录、无需加密,实测可用。
10 * - 支持歌单链接(含短链接)与纯数字歌单 id
11 */
12import { httpFetch } from '../request.js'
13import { decodeName, formatPlayTime } from '../../utils.js'
14
15const SUCCESS_CODE = 200
16const UA = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
17const HEADERS = { 'User-Agent': UA, Referer: 'https://music.163.com/' }
18
19const regExps = {
20 // https://music.163.com/#/playlist?id=xxx / ?id=xxx
21 listDetailLink: /^.+(?:\?|&)id=(\d+)(?:&.*$|#.*$|$)/,
22 // https://music.163.com/#/playlist/xxx/yyy
23 listDetailLink2: /^.+\/playlist\/(\d+)(?:\/\d+\/.*$|#.*$|$)/,
24}
25
26/** 歌手拼接 */
27const getSinger = (singers) => (singers || []).map((s) => s.name).join('、')
28
29/**
30 * 解析歌单链接 → 歌单 id
31 * 支持:
32 * - 纯数字 id
33 * - https://music.163.com/#/playlist?id=xxx
34 * - https://music.163.com/#/playlist/xxx/yyy
35 * - 短链接(163cn.tv 等):请求跟随跳转后从最终地址提取
36 */
37const getListId = async (id) => {
38 if (/[?&:/]/.test(id)) {
39 if (regExps.listDetailLink.test(id)) {
40 id = id.replace(regExps.listDetailLink, '$1')
41 } else if (regExps.listDetailLink2.test(id)) {
42 id = id.replace(regExps.listDetailLink2, '$1')
43 } else {
44 // 短链接:跟随重定向
45 const { url } = await httpFetch(id, { headers: HEADERS }).promise
46 if (regExps.listDetailLink.test(url)) id = url.replace(regExps.listDetailLink, '$1')
47 else if (regExps.listDetailLink2.test(url)) id = url.replace(regExps.listDetailLink2, '$1')
48 else throw new Error('无法从链接中解析歌单 id')
49 }
50 }
51 return id
52}
53
54/**
55 * 按 trackIds 分批拉取歌曲详情(明文 /api/song/detail)
56 * 每批 300 个 id(避免 URL 过长),失败批次跳过
57 */
58const getSongDetails = async (ids) => {
59 const songs = []
60 for (let i = 0; i < ids.length; i += 300) {
61 const chunk = ids.slice(i, i + 300)
62 const idsStr = encodeURIComponent('[' + chunk.join(',') + ']')
63 try {
64 const { body } = await httpFetch(`https://music.163.com/api/song/detail/?id=${chunk[0]}&ids=${idsStr}`, {
65 headers: HEADERS,
66 }).promise
67 if (body && body.code === SUCCESS_CODE && body.songs) songs.push(...body.songs)
68 } catch (e) {
69 console.log('[wy songList] song detail batch failed', e.message)
70 }
71 }
72 return songs
73}
74
75/** 格式化歌曲(结构对齐 starmusic 其他源) */
76const filterListDetail = (songs) => {
77 const list = []
78 for (const item of songs) {
79 if (!item || !item.id) continue
80 list.push({
81 singer: decodeName(getSinger(item.artists)),
82 name: decodeName(item.name || ''),
83 albumName: item.album && item.album.name ? decodeName(item.album.name) : '',
84 albumId: (item.album && item.album.id) || '',
85 source: 'wy',
86 interval: formatPlayTime((item.duration || 0) / 1000),
87 songmid: item.id,
88 img: (item.album && item.album.picUrl) || '',
89 lrc: null,
90 otherSource: null,
91 types: [],
92 _types: {},
93 typeUrl: {},
94 })
95 }
96 return list
97}
98
99export default {
100 /** 获取歌单内歌曲列表 */
101 async getListDetail(rawId) {
102 const id = await getListId(rawId)
103
104 // 1. 歌单信息 + trackIds(明文 v6 接口)
105 const { body } = await httpFetch(`https://music.163.com/api/v6/playlist/detail?id=${id}`, {
106 headers: HEADERS,
107 }).promise
108 if (!body || body.code !== SUCCESS_CODE || !body.playlist) throw new Error('获取歌单失败')
109
110 const playlist = body.playlist
111 const trackIds = (playlist.trackIds || []).map((t) => t.id)
112
113 // 2. tracks 可能只返回前 10 首,trackIds 是全量 → 分批补全
114 let songs = playlist.tracks || []
115 if (trackIds.length > songs.length) {
116 songs = await getSongDetails(trackIds)
117 }
118
119 return {
120 list: filterListDetail(songs),
121 page: 1,
122 limit: songs.length,
123 total: trackIds.length || songs.length,
124 source: 'wy',
125 info: {
126 name: playlist.name,
127 img: playlist.coverImgUrl,
128 desc: playlist.description,
129 author: playlist.creator && playlist.creator.nickname,
130 play_count: playlist.playCount,
131 },
132 }
133 },
134}