mirror of
https://github.com/antebudimir/feishin.git
synced 2025-12-31 10:03:33 +00:00
Lyrics Improvements
- Make the settings text actually consistent with behavior - Add metadata (artist/track name) for fetched tracks - Add ability to remove incorrectly fetched lyric - Add lyric fetch cache; save the last 10 fetches - Add ability to change offset in full screen, add more comments
This commit is contained in:
parent
9622cd346c
commit
007a099951
11 changed files with 314 additions and 61 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { QueueSong } from '/@/renderer/api/types';
|
||||
import { InternetProviderLyricResponse, QueueSong } from '/@/renderer/api/types';
|
||||
import { query as queryGenius } from './genius';
|
||||
import { query as queryNetease } from './netease';
|
||||
import { LyricSource } from '../../../../renderer/types';
|
||||
|
|
@ -6,19 +6,52 @@ import { ipcMain } from 'electron';
|
|||
import { getMainWindow } from '../../../main';
|
||||
import { store } from '../settings/index';
|
||||
|
||||
type SongFetcher = (song: QueueSong) => Promise<string | null>;
|
||||
type SongFetcher = (song: QueueSong) => Promise<InternetProviderLyricResponse | null>;
|
||||
|
||||
type CachedLyrics = Record<LyricSource, InternetProviderLyricResponse>;
|
||||
|
||||
const FETCHERS: Record<LyricSource, SongFetcher> = {
|
||||
[LyricSource.GENIUS]: queryGenius,
|
||||
[LyricSource.NETEASE]: queryNetease,
|
||||
};
|
||||
|
||||
const MAX_CACHED_ITEMS = 10;
|
||||
|
||||
const lyricCache = new Map<string, CachedLyrics>();
|
||||
|
||||
ipcMain.on('lyric-fetch', async (_event, song: QueueSong) => {
|
||||
const sources = store.get('lyrics', []) as LyricSource[];
|
||||
|
||||
const cached = lyricCache.get(song.id);
|
||||
|
||||
if (cached) {
|
||||
for (const source of sources) {
|
||||
const data = cached[source];
|
||||
|
||||
if (data) {
|
||||
getMainWindow()?.webContents.send('lyric-get', song.name, source, data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const source of sources) {
|
||||
const lyric = await FETCHERS[source](song);
|
||||
if (lyric) {
|
||||
const newResult = cached
|
||||
? {
|
||||
...cached,
|
||||
[source]: lyric,
|
||||
}
|
||||
: ({ [source]: lyric } as CachedLyrics);
|
||||
|
||||
if (lyricCache.size === MAX_CACHED_ITEMS && cached === undefined) {
|
||||
const toRemove = lyricCache.keys().next().value;
|
||||
lyricCache.delete(toRemove);
|
||||
}
|
||||
|
||||
lyricCache.set(song.id, newResult);
|
||||
|
||||
getMainWindow()?.webContents.send('lyric-get', song.name, source, lyric);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue