完善支持wy歌词获取
parent
7e27ec488b
commit
165832e703
|
@ -2,6 +2,8 @@ const { getNow, TimeoutTools } = require('./utils')
|
|||
|
||||
const timeFieldExp = /^(?:\[[\d:.]+\])+/g
|
||||
const timeExp = /[\d:.]+/g
|
||||
const timeLabelRxp = /^(\[[\d:]+\.)0+(\d+\])/
|
||||
const timeLabelFixRxp = /(?:\.0+|0+)$/
|
||||
const tagRegMap = {
|
||||
title: 'ti',
|
||||
artist: 'ar',
|
||||
|
@ -24,8 +26,9 @@ const parseExtendedLyric = (lrcLinesMap, extendedLyric) => {
|
|||
const times = timeField.match(timeExp)
|
||||
if (times == null) continue
|
||||
for (let time of times) {
|
||||
if (!time.includes('.')) time += '.0'
|
||||
const timeStr = time.replace(/(?:\.0+|0+)$/, '')
|
||||
if (time.includes('.')) time = time.replace(timeLabelRxp, '$1$2')
|
||||
else time += '.0'
|
||||
const timeStr = time.replace(timeLabelFixRxp, '')
|
||||
const targetLine = lrcLinesMap[timeStr]
|
||||
if (targetLine) targetLine.extendedLyrics.push(text)
|
||||
}
|
||||
|
@ -84,8 +87,9 @@ module.exports = class LinePlayer {
|
|||
const times = timeField.match(timeExp)
|
||||
if (times == null) continue
|
||||
for (let time of times) {
|
||||
if (!time.includes('.')) time += '.0'
|
||||
const timeStr = time.replace(/(?:\.0+|0+)$/, '')
|
||||
if (time.includes('.')) time = time.replace(timeLabelRxp, '$1$2')
|
||||
else time += '.0'
|
||||
const timeStr = time.replace(timeLabelFixRxp, '')
|
||||
if (linesMap[timeStr]) {
|
||||
linesMap[timeStr].extendedLyrics.push(text)
|
||||
continue
|
||||
|
|
|
@ -99,8 +99,8 @@ const parseTools = {
|
|||
lxlrcLines.push(`${startTimeStr}${newWords}`)
|
||||
}
|
||||
return {
|
||||
lyric: lrcLines.join('\n'),
|
||||
lxlyric: lxlrcLines.join('\n'),
|
||||
lyricLines: lrcLines,
|
||||
lxlyricLines: lxlrcLines,
|
||||
}
|
||||
},
|
||||
parseHeaderInfo(str) {
|
||||
|
@ -119,7 +119,15 @@ const parseTools = {
|
|||
}
|
||||
})
|
||||
},
|
||||
parse(ylrc, lrc, tlrc, rlrc) {
|
||||
fixTimeTag(lrcLines, targetLrcLines) {
|
||||
const timeTagRxp = /^\[[\d:.]+\]/
|
||||
return targetLrcLines.map((line, index) => {
|
||||
const timeTag = timeTagRxp.exec(lrcLines[index])
|
||||
if (!timeTag) return line
|
||||
return line.replace(timeTagRxp, timeTag[0])
|
||||
})
|
||||
},
|
||||
parse(ylrc, ytlrc, yrlrc, lrc, tlrc, rlrc) {
|
||||
const info = {
|
||||
lyric: '',
|
||||
tlyric: '',
|
||||
|
@ -129,16 +137,32 @@ const parseTools = {
|
|||
if (ylrc) {
|
||||
let lines = this.parseHeaderInfo(ylrc)
|
||||
if (lines) {
|
||||
const result = this.parseLyric(lines)
|
||||
if (ytlrc) {
|
||||
const lines = this.parseHeaderInfo(ytlrc)
|
||||
if (lines) {
|
||||
if (lines.length == result.lyricLines.length) {
|
||||
info.tlyric = this.fixTimeTag(result.lyricLines, lines).join('\n')
|
||||
} else info.tlyric = lines.join('\n')
|
||||
}
|
||||
}
|
||||
if (yrlrc) {
|
||||
const lines = this.parseHeaderInfo(yrlrc)
|
||||
if (lines) {
|
||||
if (lines.length == result.lyricLines.length) {
|
||||
info.rlyric = this.fixTimeTag(result.lyricLines, lines).join('\n')
|
||||
} else info.rlyric = lines.join('\n')
|
||||
}
|
||||
}
|
||||
|
||||
const timeRxp = /^\[[\d:.]+\]/
|
||||
const headers = lines.filter(l => timeRxp.test(l)).join('\n')
|
||||
const result = this.parseLyric(lines)
|
||||
info.lxlyric = result.lxlyric
|
||||
info.lyric = `${headers}\n${result.lyric}`
|
||||
} else if (lrc) {
|
||||
lines = this.parseHeaderInfo(lrc)
|
||||
if (lines) info.lyric = lines.join('\n')
|
||||
info.lyric = `${headers}\n${result.lyricLines.join('\n')}`
|
||||
info.lxlyric = result.lxlyricLines.join('\n')
|
||||
return info
|
||||
}
|
||||
} else if (lrc) {
|
||||
}
|
||||
if (lrc) {
|
||||
const lines = this.parseHeaderInfo(lrc)
|
||||
if (lines) info.lyric = lines.join('\n')
|
||||
}
|
||||
|
@ -203,7 +227,10 @@ export default songmid => {
|
|||
requestObj.promise = requestObj.promise.then(({ body }) => {
|
||||
// console.log(body)
|
||||
if (body.code !== 200 || !body?.lrc?.lyric) return Promise.reject(new Error('Get lyric failed'))
|
||||
return parseTools.parse(body.yrc?.lyric, body.lrc.lyric, body.tlyric?.lyric, body.romalrc?.lyric)
|
||||
const info = parseTools.parse(body.yrc?.lyric, body.ytlrc?.lyric, body.yromalrc?.lyric, body.lrc.lyric, body.tlyric?.lyric, body.romalrc?.lyric)
|
||||
// console.log(info)
|
||||
if (!info.lyric) return Promise.reject(new Error('Get lyric failed'))
|
||||
return info
|
||||
})
|
||||
return requestObj
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue