完善支持wy歌词获取

pull/1168/head
lyswhut 2023-02-01 11:24:58 +08:00
parent 7e27ec488b
commit 165832e703
2 changed files with 46 additions and 15 deletions

View File

@ -2,6 +2,8 @@ const { getNow, TimeoutTools } = require('./utils')
const timeFieldExp = /^(?:\[[\d:.]+\])+/g const timeFieldExp = /^(?:\[[\d:.]+\])+/g
const timeExp = /[\d:.]+/g const timeExp = /[\d:.]+/g
const timeLabelRxp = /^(\[[\d:]+\.)0+(\d+\])/
const timeLabelFixRxp = /(?:\.0+|0+)$/
const tagRegMap = { const tagRegMap = {
title: 'ti', title: 'ti',
artist: 'ar', artist: 'ar',
@ -24,8 +26,9 @@ const parseExtendedLyric = (lrcLinesMap, extendedLyric) => {
const times = timeField.match(timeExp) const times = timeField.match(timeExp)
if (times == null) continue if (times == null) continue
for (let time of times) { for (let time of times) {
if (!time.includes('.')) time += '.0' if (time.includes('.')) time = time.replace(timeLabelRxp, '$1$2')
const timeStr = time.replace(/(?:\.0+|0+)$/, '') else time += '.0'
const timeStr = time.replace(timeLabelFixRxp, '')
const targetLine = lrcLinesMap[timeStr] const targetLine = lrcLinesMap[timeStr]
if (targetLine) targetLine.extendedLyrics.push(text) if (targetLine) targetLine.extendedLyrics.push(text)
} }
@ -84,8 +87,9 @@ module.exports = class LinePlayer {
const times = timeField.match(timeExp) const times = timeField.match(timeExp)
if (times == null) continue if (times == null) continue
for (let time of times) { for (let time of times) {
if (!time.includes('.')) time += '.0' if (time.includes('.')) time = time.replace(timeLabelRxp, '$1$2')
const timeStr = time.replace(/(?:\.0+|0+)$/, '') else time += '.0'
const timeStr = time.replace(timeLabelFixRxp, '')
if (linesMap[timeStr]) { if (linesMap[timeStr]) {
linesMap[timeStr].extendedLyrics.push(text) linesMap[timeStr].extendedLyrics.push(text)
continue continue

View File

@ -99,8 +99,8 @@ const parseTools = {
lxlrcLines.push(`${startTimeStr}${newWords}`) lxlrcLines.push(`${startTimeStr}${newWords}`)
} }
return { return {
lyric: lrcLines.join('\n'), lyricLines: lrcLines,
lxlyric: lxlrcLines.join('\n'), lxlyricLines: lxlrcLines,
} }
}, },
parseHeaderInfo(str) { 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 = { const info = {
lyric: '', lyric: '',
tlyric: '', tlyric: '',
@ -129,16 +137,32 @@ const parseTools = {
if (ylrc) { if (ylrc) {
let lines = this.parseHeaderInfo(ylrc) let lines = this.parseHeaderInfo(ylrc)
if (lines) { 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 timeRxp = /^\[[\d:.]+\]/
const headers = lines.filter(l => timeRxp.test(l)).join('\n') const headers = lines.filter(l => timeRxp.test(l)).join('\n')
const result = this.parseLyric(lines) info.lyric = `${headers}\n${result.lyricLines.join('\n')}`
info.lxlyric = result.lxlyric info.lxlyric = result.lxlyricLines.join('\n')
info.lyric = `${headers}\n${result.lyric}` return info
} else if (lrc) {
lines = this.parseHeaderInfo(lrc)
if (lines) info.lyric = lines.join('\n')
} }
} else if (lrc) { }
if (lrc) {
const lines = this.parseHeaderInfo(lrc) const lines = this.parseHeaderInfo(lrc)
if (lines) info.lyric = lines.join('\n') if (lines) info.lyric = lines.join('\n')
} }
@ -203,7 +227,10 @@ export default songmid => {
requestObj.promise = requestObj.promise.then(({ body }) => { requestObj.promise = requestObj.promise.then(({ body }) => {
// console.log(body) // console.log(body)
if (body.code !== 200 || !body?.lrc?.lyric) return Promise.reject(new Error('Get lyric failed')) 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 return requestObj
} }