Add lyric search functions and query

This commit is contained in:
jeffvli 2023-06-08 03:40:34 -07:00 committed by Jeff
parent 43c11ab6e3
commit 0fa5b6496f
7 changed files with 210 additions and 23 deletions

View file

@ -1,11 +1,19 @@
import { InternetProviderLyricResponse, QueueSong } from '/@/renderer/api/types';
import { query as queryGenius } from './genius';
import { query as queryNetease } from './netease';
import {
InternetProviderLyricResponse,
InternetProviderLyricSearchResponse,
LyricSearchQuery,
QueueSong,
} from '/@/renderer/api/types';
import { query as queryGenius, getSearchResults as searchGenius } from './genius';
import { query as queryNetease, getSearchResults as searchNetease } from './netease';
import { LyricSource } from '../../../../renderer/types';
import { ipcMain } from 'electron';
import { store } from '../settings/index';
type SongFetcher = (song: QueueSong) => Promise<InternetProviderLyricResponse | null>;
type SongFetcher = (params: LyricSearchQuery) => Promise<InternetProviderLyricResponse | null>;
type SearchFetcher = (
params: LyricSearchQuery,
) => Promise<InternetProviderLyricSearchResponse[] | null>;
type CachedLyrics = Record<LyricSource, InternetProviderLyricResponse>;
@ -14,6 +22,11 @@ const FETCHERS: Record<LyricSource, SongFetcher> = {
[LyricSource.NETEASE]: queryNetease,
};
const SEARCH_FETCHERS: Record<LyricSource, SearchFetcher> = {
[LyricSource.GENIUS]: searchGenius,
[LyricSource.NETEASE]: searchNetease,
};
const MAX_CACHED_ITEMS = 10;
const lyricCache = new Map<string, CachedLyrics>();
@ -33,7 +46,9 @@ const getRemoteLyrics = async (song: QueueSong) => {
let lyricsFromSource = null;
for (const source of sources) {
const response = await FETCHERS[source](song);
const params = { artist: song.artistName, name: song.name };
const response = await FETCHERS[source](params);
if (response) {
const newResult = cached
? {
@ -57,7 +72,33 @@ const getRemoteLyrics = async (song: QueueSong) => {
return lyricsFromSource;
};
const searchRemoteLyrics = async (params: LyricSearchQuery) => {
const sources = store.get('lyrics', []) as LyricSource[];
const results: Record<LyricSource, InternetProviderLyricSearchResponse[]> = {
[LyricSource.GENIUS]: [],
[LyricSource.NETEASE]: [],
};
for (const source of sources) {
const response = await SEARCH_FETCHERS[source](params);
if (response) {
response.forEach((result) => {
results[source].push(result);
});
}
}
return results;
};
ipcMain.handle('lyric-fetch-manual', async (_event, song: QueueSong) => {
const lyric = await getRemoteLyrics(song);
return lyric;
});
ipcMain.handle('lyric-search', async (_event, params: LyricSearchQuery) => {
const lyricResults = await searchRemoteLyrics(params);
return lyricResults;
});