starmusic-pc
1/**
2 * QQ音乐 (tx) 歌单导入
3 *
4 * 迁移自 lx-music-mobile/src/utils/musicSdk/tx/songList.js
5 * 接口:https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?disstid=xxx
6 * - 明文 GET,需带 Origin/Referer 头
7 * - 一次全量返回歌单内所有歌曲
8 * 支持歌单链接(含短链接)与纯数字歌单 id
9 */
10import { httpFetch } from '../request.js'
11import { decodeName, formatPlayTime, sizeFormate } from '../../utils.js'
12
13const SUCCESS_CODE = 0
14
15const regExps = {
16 // https://y.qq.com/n/yqq/playlist/7217720898.html
17 listDetailLink: /\/playlist\/(\d+)/,
18 // https://i.y.qq.com/n2/m/share/details/taoge.html?platform=11&id=7217720898&ADTAG=qfshare
19 listDetailLink2: /(?:^|[?&])id=(\d+)/,
20}
21
22/** 歌手数组 → "、" 拼接 */
23const formatSingerName = (singers) => (singers || []).map((s) => s.name).join('、')
24
25/** 从链接中直接提取歌单 id(不发请求) */
26const extractId = (url) => {
27 const result = regExps.listDetailLink.exec(url) || regExps.listDetailLink2.exec(url)
28 return result ? result[1] : null
29}
30
31/**
32 * 解析歌单链接 → 歌单 id
33 * 优先正则直取 id:QQ 音乐分享页(i.y.qq.com/taoge.html)在部分环境下会触发
34 * 302 重定向循环,Android 网络层超过 20 次后报 "Too many follow-up requests"
35 * 仅短链(如 c6.y.qq.com/base/fcgi-bin/u?__=xxx)才请求跟随跳转
36 */
37const getListId = async (id) => {
38 if (!/[?&:/]/.test(id)) return id
39
40 let result = extractId(id)
41 if (result) return result
42
43 // 短链:跟随跳转拿真实地址(限制等待时间,避免重定向循环长时间卡住)
44 let finalUrl = id
45 try {
46 const resp = await httpFetch(id, { timeout: 8000 }).promise
47 finalUrl = resp.url || id
48 } catch (err) {
49 throw new Error('歌单链接解析失败,请改用歌单页完整链接(需包含 id=数字 或 /playlist/数字)')
50 }
51 result = extractId(finalUrl)
52 if (!result) throw new Error('无法从链接中解析歌单 id')
53 return result
54}
55
56/** 格式化歌曲 */
57const filterListDetail = (rawList) =>
58 rawList.map((item) => {
59 const types = []
60 const _types = {}
61 if (item.file && item.file.size_128mp3 !== 0) {
62 const size = sizeFormate(item.file.size_128mp3)
63 types.push({ type: '128k', size })
64 _types['128k'] = { size }
65 }
66 if (item.file && item.file.size_320mp3 !== 0) {
67 const size = sizeFormate(item.file.size_320mp3)
68 types.push({ type: '320k', size })
69 _types['320k'] = { size }
70 }
71 if (item.file && item.file.size_flac !== 0) {
72 const size = sizeFormate(item.file.size_flac)
73 types.push({ type: 'flac', size })
74 _types.flac = { size }
75 }
76 if (item.file && item.file.size_hires !== 0) {
77 const size = sizeFormate(item.file.size_hires)
78 types.push({ type: 'flac24bit', size })
79 _types.flac24bit = { size }
80 }
81 return {
82 singer: formatSingerName(item.singer),
83 name: decodeName(item.title || ''),
84 albumName: item.album && item.album.name ? decodeName(item.album.name) : '',
85 albumId: (item.album && item.album.mid) || '',
86 source: 'tx',
87 interval: formatPlayTime(item.interval || 0),
88 songId: item.id,
89 albumMid: item.album && item.album.mid,
90 strMediaMid: item.file && item.file.media_mid,
91 songmid: item.mid,
92 img:
93 item.album && item.album.name !== '' && item.album.name !== '空' && item.album.mid
94 ? `https://y.gtimg.cn/music/photo_new/T002R500x500M000${item.album.mid}.jpg`
95 : item.singer && item.singer.length && item.singer[0].mid
96 ? `https://y.gtimg.cn/music/photo_new/T001R500x500M000${item.singer[0].mid}.jpg`
97 : '',
98 lrc: null,
99 otherSource: null,
100 types,
101 _types,
102 typeUrl: {},
103 }
104 })
105
106export default {
107 /** 获取歌单内歌曲列表 */
108 async getListDetail(rawId) {
109 const id = await getListId(rawId)
110
111 const { body } = await httpFetch(
112 `https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&json=1&utf8=1&onlysong=0&new_format=1&disstid=${id}&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0`,
113 {
114 headers: {
115 Origin: 'https://y.qq.com',
116 Referer: `https://y.qq.com/n/yqq/playsquare/${id}.html`,
117 },
118 },
119 ).promise
120
121 if (!body || body.code !== SUCCESS_CODE || !body.cdlist || !body.cdlist[0]) {
122 throw new Error('获取歌单失败')
123 }
124 const cdlist = body.cdlist[0]
125
126 return {
127 list: filterListDetail(cdlist.songlist || []),
128 page: 1,
129 limit: (cdlist.songlist || []).length + 1,
130 total: (cdlist.songlist || []).length,
131 source: 'tx',
132 info: {
133 name: cdlist.dissname,
134 img: cdlist.logo,
135 desc: decodeName(cdlist.desc || '').replace(/<br>/g, '\n'),
136 author: cdlist.nickname,
137 play_count: cdlist.visitnum,
138 },
139 }
140 },
141}