mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-01 10:23:33 +00:00
lyrics: add translation lyrics for netease.ts (#951)
* lyrics: add translation lyrics for netease.ts
This commit is contained in:
parent
e3751229b6
commit
ae41fe99bb
5 changed files with 90 additions and 2 deletions
|
|
@ -6,6 +6,7 @@ import {
|
|||
LyricSearchQuery,
|
||||
LyricSource,
|
||||
} from '.';
|
||||
import { store } from '../settings';
|
||||
import { orderSearchResults } from './shared';
|
||||
|
||||
const SEARCH_URL = 'https://music.163.com/api/search/get';
|
||||
|
|
@ -76,14 +77,20 @@ export async function getLyricsBySongId(songId: string): Promise<null | string>
|
|||
id: songId,
|
||||
kv: '-1',
|
||||
lv: '-1',
|
||||
tv: '-1',
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('NetEase lyrics request got an error!', e);
|
||||
return null;
|
||||
}
|
||||
|
||||
return result.data.klyric?.lyric || result.data.lrc?.lyric;
|
||||
const enableTranslation = store.get('enableNeteaseTranslation', true) as boolean;
|
||||
const originalLrc = result.data.lrc?.lyric;
|
||||
if (!enableTranslation) {
|
||||
return originalLrc || null;
|
||||
}
|
||||
const translatedLrc = result.data.tlyric?.lyric;
|
||||
return mergeLyrics(originalLrc, translatedLrc);
|
||||
}
|
||||
|
||||
export async function getSearchResults(
|
||||
|
|
@ -166,3 +173,54 @@ async function getMatchedLyrics(
|
|||
|
||||
return firstMatch;
|
||||
}
|
||||
|
||||
function mergeLyrics(original: string | undefined, translated: string | undefined): null | string {
|
||||
if (!original) {
|
||||
return null;
|
||||
}
|
||||
if (!translated) {
|
||||
return original;
|
||||
}
|
||||
|
||||
const lrcLineRegex = /\[(\d{2}:\d{2}\.\d{2,3})\](.*)/;
|
||||
const translatedMap = new Map<string, string>();
|
||||
|
||||
// Parse the translated LRC and store it in a Map for efficient timestamp-based lookups.
|
||||
translated.split('\n').forEach((line) => {
|
||||
const match = line.match(lrcLineRegex);
|
||||
if (match) {
|
||||
const timestamp = match[1];
|
||||
const text = match[2].trim();
|
||||
if (text) {
|
||||
translatedMap.set(timestamp, text);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (translatedMap.size === 0) {
|
||||
return original;
|
||||
}
|
||||
|
||||
// Iterate through each line of the original LRC. If a translation exists for
|
||||
// the same timestamp, insert it as a new, fully-formatted LRC line.
|
||||
const finalLines = original.split('\n').flatMap((line) => {
|
||||
const match = line.match(lrcLineRegex);
|
||||
|
||||
if (match) {
|
||||
const timestamp = match[1];
|
||||
const translatedText = translatedMap.get(timestamp);
|
||||
|
||||
if (translatedText) {
|
||||
// Return an array containing both the original line and the new translated line.
|
||||
// flatMap will flatten this into the final array of lines.
|
||||
const translatedLine = `[${timestamp}]${translatedText}`;
|
||||
return [line, translatedLine];
|
||||
}
|
||||
}
|
||||
|
||||
// If no match or no translation is found, return only the original line.
|
||||
return [line];
|
||||
});
|
||||
|
||||
return finalLines.join('\n');
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue