refactor(sdk): musicSdk 全量迁移至 TypeScript

This commit is contained in:
snowy-yang committed 2026-08-29 00:23:50 +08:00
1 parent f289df4cd7
commit 1dfb7a59ca
83 files changed
+924 -732

No files matched your search

@@ -1,6 +1,7 @@
import { httpFetch } from '../request.js'
import { dateFormat2 } from '../index.js'
import getMusicInfo from './musicInfo.js'
import type { HttpRequestObject } from '../types.ts'
import { httpFetch } from '../request.ts'
import { dateFormat2 } from '../index.ts'
import getMusicInfo from './musicInfo.ts'
const emojis = {
e400846: '😘',
@@ -75,15 +76,16 @@ const songIdMap = new Map()
const promises = new Map()
export default {
_requestObj: null,
_requestObj2: null,
_requestObj: null as HttpRequestObject | null,
_requestObj2: null as HttpRequestObject | null,
async getSongId({ songId, songmid }) {
if (songId) return songId
if (songIdMap.has(songmid)) return songIdMap.get(songmid)
if (promises.has(songmid)) return (await promises.get(songmid)).songId
const promise = getMusicInfo(songmid)
promises.set(promise)
promises.set(songmid, promise)
const info = await promise
if (!info) throw new Error('获取歌曲信息失败')
songIdMap.set(songmid, info.songId)
promises.delete(songmid)
return info.songId
@@ -1,7 +1,8 @@
import { httpFetch } from '../request.js'
import type { HttpRequestObject } from '../types.ts'
import { httpFetch } from '../request.ts'
export default {
_requestObj: null,
_requestObj: null as HttpRequestObject | null,
async getList(retryNum = 0) {
if (this._requestObj) this._requestObj.cancelHttp()
if (retryNum > 2) return Promise.reject(new Error('try max num'))
@@ -1,11 +1,11 @@
import leaderboard from './leaderboard.js'
import lyric from './lyric.js'
import songList from './songList.js'
import musicSearch from './musicSearch.js'
import { apis } from '../api-source.js'
import hotSearch from './hotSearch.js'
import comment from './comment.js'
// import tipSearch from './tipSearch.js'
import leaderboard from './leaderboard.ts'
import lyric from './lyric.ts'
import songList from './songList.ts'
import musicSearch from './musicSearch.ts'
import { apis } from '../api-source.ts'
import hotSearch from './hotSearch.ts'
import comment from './comment.ts'
// import tipSearch from './tipSearch.ts'
const tx = {
// tipSearch,
@@ -16,7 +16,7 @@ const tx = {
comment,
getMusicUrl(songInfo, type) {
return apis('tx').getMusicUrl(songInfo, type)
return apis('tx')!.getMusicUrl(songInfo, type)
},
getLyric(songInfo) {
// let singer = songInfo.singer.indexOf('、') > -1 ? songInfo.singer.split('、')[0] : songInfo.singer
@@ -1,6 +1,7 @@
import { httpFetch } from '../request.js'
import { formatPlayTime, sizeFormate } from '../index.js'
import { formatSingerName } from '../utils.js'
import type { HttpRequestObject, SongInfo, SongType, Quality, QualityInfo } from '../types.ts'
import { httpFetch } from '../request.ts'
import { formatPlayTime, sizeFormate } from '../index.ts'
import { formatSingerName } from '../utils.ts'
let boardList = [{ id: 'tx__4', name: '流行指数榜', bangid: '4' }, { id: 'tx__26', name: '热歌榜', bangid: '26' }, { id: 'tx__27', name: '新歌榜', bangid: '27' }, { id: 'tx__62', name: '飙升榜', bangid: '62' }, { id: 'tx__58', name: '说唱榜', bangid: '58' }, { id: 'tx__57', name: '喜力电音榜', bangid: '57' }, { id: 'tx__28', name: '网络歌曲榜', bangid: '28' }, { id: 'tx__5', name: '内地榜', bangid: '5' }, { id: 'tx__3', name: '欧美榜', bangid: '3' }, { id: 'tx__59', name: '香港地区榜', bangid: '59' }, { id: 'tx__16', name: '韩国榜', bangid: '16' }, { id: 'tx__60', name: '抖快榜', bangid: '60' }, { id: 'tx__29', name: '影视金曲榜', bangid: '29' }, { id: 'tx__17', name: '日本榜', bangid: '17' }, { id: 'tx__52', name: '腾讯音乐人原创榜', bangid: '52' }, { id: 'tx__36', name: 'K歌金曲榜', bangid: '36' }, { id: 'tx__61', name: '台湾地区榜', bangid: '61' }, { id: 'tx__63', name: 'DJ舞曲榜', bangid: '63' }, { id: 'tx__64', name: '综艺新歌榜', bangid: '64' }, { id: 'tx__65', name: '国风热歌榜', bangid: '65' }, { id: 'tx__67', name: '听歌识曲榜', bangid: '67' }, { id: 'tx__72', name: '动漫音乐榜', bangid: '72' }, { id: 'tx__73', name: '游戏音乐榜', bangid: '73' }, { id: 'tx__75', name: '有声榜', bangid: '75' }, { id: 'tx__131', name: '校园音乐人排行榜', bangid: '131' }]
@@ -10,57 +11,57 @@ export default {
{
id: 'txlxzsb',
name: '流行榜',
bangid: 4,
bangid: '4',
},
{
id: 'txrgb',
name: '热歌榜',
bangid: 26,
bangid: '26',
},
{
id: 'txwlhgb',
name: '网络榜',
bangid: 28,
bangid: '28',
},
{
id: 'txdyb',
name: '抖音榜',
bangid: 60,
bangid: '60',
},
{
id: 'txndb',
name: '内地榜',
bangid: 5,
bangid: '5',
},
{
id: 'txxgb',
name: '香港榜',
bangid: 59,
bangid: '59',
},
{
id: 'txtwb',
name: '台湾榜',
bangid: 61,
bangid: '61',
},
{
id: 'txoumb',
name: '欧美榜',
bangid: 3,
bangid: '3',
},
{
id: 'txhgb',
name: '韩国榜',
bangid: 16,
bangid: '16',
},
{
id: 'txrbb',
name: '日本榜',
bangid: 17,
bangid: '17',
},
{
id: 'txtybb',
name: 'YouTube榜',
bangid: 128,
bangid: '128',
},
],
listDetailRequest(id, period, limit) {
@@ -95,7 +96,7 @@ export default {
},
periods: {},
periodUrl: 'https://c.y.qq.com/node/pc/wk_v15/top.html',
_requestBoardsObj: null,
_requestBoardsObj: null as HttpRequestObject | null,
getBoardsData() {
if (this._requestBoardsObj) this._requestBoardsObj.cancelHttp()
this._requestBoardsObj = httpFetch('https://c.y.qq.com/v8/fcg-bin/fcg_myqq_toplist.fcg?g_tk=1928093487&inCharset=utf-8&outCharset=utf-8&notice=0&format=json&uin=0&needNewCode=1&platform=h5')
@@ -108,8 +109,8 @@ export default {
filterData(rawList) {
// console.log(rawList)
return rawList.map(item => {
let types = []
let _types = {}
let types: SongType[] = []
let _types: Partial<Record<Quality, QualityInfo>> = {}
if (item.file.size_128mp3 !== 0) {
let size = sizeFormate(item.file.size_128mp3)
types.push({ type: '128k', size })
@@ -180,7 +181,7 @@ export default {
},
filterBoardsData(rawList) {
// console.log(rawList)
let list = []
let list: any[] = []
for (const board of rawList) {
// 排除 MV榜
if (board.id == 201) continue
+17 -15
View File
@@ -1,7 +1,8 @@
import { httpFetch } from '../request.js'
import getMusicInfo from './musicInfo.js'
import { rendererInvoke } from '../../src/common/rendererIpc.js'
import { WIN_MAIN_RENDERER_EVENT_NAME } from '../../src/common/ipcNames.js'
import type { HttpRequestObject } from '../types.ts'
import { httpFetch } from '../request.ts'
import getMusicInfo from './musicInfo.ts'
import { rendererInvoke } from '../../src/common/rendererIpc.ts'
import { WIN_MAIN_RENDERER_EVENT_NAME } from '../../src/common/ipcNames.ts'
const songIdMap = new Map()
const promises = new Map()
@@ -21,9 +22,9 @@ const parseTools = {
if (Number.isNaN(timeMs)) return ''
let ms = timeMs % 1000
timeMs /= 1000
let m = parseInt(timeMs / 60).toString().padStart(2, '0')
let m = Math.floor(timeMs / 60).toString().padStart(2, '0')
timeMs %= 60
let s = parseInt(timeMs).toString().padStart(2, '0')
let s = Math.floor(timeMs).toString().padStart(2, '0')
return `[${m}:${s}.${String(ms).padStart(3, '0')}]`
},
parseLyric(lrc) {
@@ -32,8 +33,8 @@ const parseTools = {
if (!lrc) return { lyric: '', lxlyric: '' }
const lines = lrc.split('\n')
const lxlrcLines = []
const lrcLines = []
const lxlrcLines: string[] = []
const lrcLines: string[] = []
for (let line of lines) {
line = line.trim()
@@ -61,7 +62,7 @@ const parseTools = {
let times = words.match(this.rxps.wordTimeAll)
if (!times) continue
times = times.map(time => {
const result = /\((\d+),(\d+)\)/.exec(time)
const result = /\((\d+),(\d+)\)/.exec(time)!
return `<${Math.max(parseInt(result[1]) - startMsTime, 0)},${result[2]}>`
})
const wordArr = words.split(this.rxps.wordTime)
@@ -79,7 +80,7 @@ const parseTools = {
if (!lrc) return { lyric: '', lxlyric: '' }
const lines = lrc.split('\n')
const lrcLines = []
const lrcLines: string[] = []
for (let line of lines) {
line = line.trim()
@@ -112,8 +113,8 @@ const parseTools = {
// console.log(rlrc)
const rlrcLines = rlrc.split('\n')
let lrcLines = lrc.split('\n')
// let temp = []
let newLrc = []
// let temp: string[] = []
let newLrc: string[] = []
rlrcLines.forEach((line) => {
const result = this.rxps.lineTime2.exec(line)
if (!result) return
@@ -142,8 +143,8 @@ const parseTools = {
// console.log(tlrc)
const tlrcLines = tlrc.split('\n')
let lrcLines = lrc.split('\n')
// let temp = []
let newLrc = []
// let temp: string[] = []
let newLrc: string[] = []
tlrcLines.forEach((line) => {
const result = this.rxps.lineTime2.exec(line)
if (!result) return
@@ -203,8 +204,9 @@ export default {
if (songIdMap.has(songmid)) return songIdMap.get(songmid)
if (promises.has(songmid)) return (await promises.get(songmid)).songId
const promise = getMusicInfo(songmid)
promises.set(promise)
promises.set(songmid, promise)
const info = await promise
if (!info) throw new Error('获取歌曲信息失败')
songIdMap.set(songmid, info.songId)
promises.delete(songmid)
return info.songId
@@ -1,8 +1,9 @@
import { httpFetch } from '../request.js'
import { formatPlayTime, sizeFormate } from '../index.js'
import type { HttpRequestObject, SongType, Quality, QualityInfo } from '../types.ts'
import { httpFetch } from '../request.ts'
import { formatPlayTime, sizeFormate } from '../index.ts'
const getSinger = (singers) => {
let arr = []
let arr: string[] = []
singers.forEach(singer => {
arr.push(singer.name)
})
@@ -37,8 +38,8 @@ export default (songmid) => {
const item = body.req.data.track_info
if (!item.file?.media_mid) return null
let types = []
let _types = {}
let types: SongType[] = []
let _types: Partial<Record<Quality, QualityInfo>> = {}
const file = item.file
if (file.size_128mp3 != 0) {
let size = sizeFormate(file.size_128mp3)
@@ -1,6 +1,7 @@
import { formatPlayTime, sizeFormate } from '../index.js'
import { formatSingerName } from '../utils.js'
import { signRequest } from './utils/index.js'
import { formatPlayTime, sizeFormate } from '../index.ts'
import { formatSingerName } from '../utils.ts'
import { signRequest } from './utils/index.ts'
import type { SongInfo, SongType, Quality, QualityInfo } from '../types.ts'
export default {
limit: 50,
@@ -81,12 +82,12 @@ export default {
handleResult(rawList) {
// console.log(rawList)
if (!rawList || !Array.isArray(rawList)) return []
const list = []
const list: SongInfo[] = []
rawList.forEach(item => {
if (!item.file?.media_mid) return
let types = []
let _types = {}
let types: SongType[] = []
let _types: Partial<Record<Quality, QualityInfo>> = {}
const file = item.file
if (file.size_128mp3 != 0) {
let size = sizeFormate(file.size_128mp3)
@@ -1,11 +1,12 @@
import { httpFetch } from '../request.js'
import type { HttpRequestObject, SongType, Quality, QualityInfo } from '../types.ts'
import { httpFetch } from '../request.ts'
import { formatPlayTime, sizeFormate } from '../index.js'
import { formatSingerName } from '../utils.js'
import { formatPlayTime, sizeFormate } from '../index.ts'
import { formatSingerName } from '../utils.ts'
export const filterMusicInfoItem = item => {
const types = []
const _types = {}
const types: SongType[] = []
const _types: Partial<Record<Quality, QualityInfo>> = {}
if (item.file.size_128mp3 != 0) {
let size = sizeFormate(item.file.size_128mp3)
types.push({ type: '128k', size })
@@ -65,7 +66,7 @@ export const filterMusicInfoItem = item => {
* @param {*} options
* @param {*} retryNum
*/
const createMusicuFetch = async(data, options, retryNum = 0) => {
const createMusicuFetch = async(data, options?, retryNum = 0) => {
if (retryNum > 2) throw new Error('try max num')
let result
@@ -1,11 +1,12 @@
import { httpFetch } from '../request.js'
import { decodeName, formatPlayTime, sizeFormate, dateFormat, formatPlayCount } from '../index.js'
import { formatSingerName } from '../utils.js'
import type { HttpRequestObject, SongType, Quality, QualityInfo } from '../types.ts'
import { httpFetch } from '../request.ts'
import { decodeName, formatPlayTime, sizeFormate, dateFormat, formatPlayCount } from '../index.ts'
import { formatSingerName } from '../utils.ts'
export default {
_requestObj_tags: null,
_requestObj_hotTags: null,
_requestObj_list: null,
_requestObj_tags: null as HttpRequestObject | null,
_requestObj_hotTags: null as HttpRequestObject | null,
_requestObj_list: null as HttpRequestObject | null,
limit_list: 36,
limit_song: 100000,
successCode: 0,
@@ -68,7 +69,7 @@ export default {
if (this._requestObj_tags) this._requestObj_tags.cancelHttp()
if (tryNum > 2) return Promise.reject(new Error('try max num'))
this._requestObj_tags = httpFetch(this.tagsUrl)
return this._requestObj_tags.promise.then(({ body }) => {
return this._requestObj_tags!.promise.then(({ body }) => {
if (body.code !== this.successCode) return this.getTag(++tryNum)
return this.filterTagInfo(body.tags.data.v_group)
})
@@ -78,14 +79,14 @@ export default {
if (this._requestObj_hotTags) this._requestObj_hotTags.cancelHttp()
if (tryNum > 2) return Promise.reject(new Error('try max num'))
this._requestObj_hotTags = httpFetch(this.hotTagUrl)
return this._requestObj_hotTags.promise.then(({ statusCode, body }) => {
return this._requestObj_hotTags!.promise.then(({ statusCode, body }) => {
if (statusCode !== 200) return this.getHotTag(++tryNum)
return this.filterInfoHotTag(body)
})
},
filterInfoHotTag(html) {
let hotTag = html.match(this.regExps.hotTagHtml)
const hotTags = []
const hotTags: any[] = []
if (!hotTag) return hotTags
hotTag.forEach(tagHtml => {
@@ -120,7 +121,7 @@ export default {
this.getListUrl(sortId, tagId, page),
)
// console.log(this.getListUrl(sortId, tagId, page))
return this._requestObj_list.promise.then(({ body }) => {
return this._requestObj_list!.promise.then(({ body }) => {
if (body.code !== this.successCode) return this.getList(sortId, tagId, page, ++tryNum)
return tagId ? this.filterList2(body.playlist.data, page) : this.filterList(body.playlist.data, page)
})
@@ -226,8 +227,8 @@ export default {
filterListDetail(rawList) {
// console.log(rawList)
return rawList.map(item => {
let types = []
let _types = {}
let types: SongType[] = []
let _types: Partial<Record<Quality, QualityInfo>> = {}
if (item.file.size_128mp3 !== 0) {
let size = sizeFormate(item.file.size_128mp3)
types.push({ type: '128k', size })
@@ -1,10 +1,11 @@
import { httpFetch } from '../request.js'
import type { HttpRequestObject } from '../types.ts'
import { httpFetch } from '../request.ts'
export default {
// regExps: {
// relWord: /RELWORD=(.+)/,
// },
requestObj: null,
requestObj: null as HttpRequestObject | null,
tipSearch(str) {
this.cancelTipSearch()
this.requestObj = httpFetch(`https://c.y.qq.com/splcloud/fcgi-bin/smartbox_new.fcg?is_xml=0&format=json&key=${encodeURIComponent(str)}&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0`, {
File renamed without changes.
@@ -1,6 +1,7 @@
import type { HttpRequestObject } from '../../types.ts'
import { httpFetch } from '../../request.js'
import { zzcSign } from './crypto.js'
import { httpFetch } from '../../request.ts'
import { zzcSign } from './crypto.ts'
export const signRequest = async(data) => {
// console.log(data)