mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-10 04:30:25 +02:00
update lyrics autofetcher to async search
This commit is contained in:
@@ -78,6 +78,30 @@ const MAX_CACHED_ITEMS = 10;
|
|||||||
|
|
||||||
const lyricCache = new Map<string, CachedLyrics>();
|
const lyricCache = new Map<string, CachedLyrics>();
|
||||||
|
|
||||||
|
const searchAllSources = async (
|
||||||
|
params: LyricSearchQuery,
|
||||||
|
): Promise<InternetProviderLyricSearchResponse[]> => {
|
||||||
|
const sources = store.get('lyrics', []) as LyricSource[];
|
||||||
|
|
||||||
|
const searchPromises = sources.map((source) =>
|
||||||
|
SEARCH_FETCHERS[source](params).then((searchResults) => ({ searchResults, source })),
|
||||||
|
);
|
||||||
|
|
||||||
|
const settled = await Promise.allSettled(searchPromises);
|
||||||
|
|
||||||
|
const allSearchResults: InternetProviderLyricSearchResponse[] = [];
|
||||||
|
|
||||||
|
for (const result of settled) {
|
||||||
|
if (result.status === 'fulfilled' && result.value.searchResults) {
|
||||||
|
allSearchResults.push(...result.value.searchResults);
|
||||||
|
} else if (result.status === 'rejected') {
|
||||||
|
const index = settled.indexOf(result);
|
||||||
|
console.error(`Error searching ${sources[index]} for lyrics:`, result.reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return allSearchResults;
|
||||||
|
};
|
||||||
|
|
||||||
const getRemoteLyrics = async (song: Song) => {
|
const getRemoteLyrics = async (song: Song) => {
|
||||||
const sources = store.get('lyrics', []) as LyricSource[];
|
const sources = store.get('lyrics', []) as LyricSource[];
|
||||||
|
|
||||||
@@ -97,18 +121,7 @@ const getRemoteLyrics = async (song: Song) => {
|
|||||||
name: song.name,
|
name: song.name,
|
||||||
};
|
};
|
||||||
|
|
||||||
const allSearchResults: InternetProviderLyricSearchResponse[] = [];
|
const allSearchResults = await searchAllSources(params);
|
||||||
|
|
||||||
for (const source of sources) {
|
|
||||||
try {
|
|
||||||
const searchResults = await SEARCH_FETCHERS[source](params);
|
|
||||||
if (searchResults) {
|
|
||||||
allSearchResults.push(...searchResults);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error searching ${source} for lyrics:`, error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (allSearchResults.length === 0) {
|
if (allSearchResults.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -172,24 +185,16 @@ const getRemoteLyrics = async (song: Song) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const searchRemoteLyrics = async (params: LyricSearchQuery) => {
|
const searchRemoteLyrics = async (params: LyricSearchQuery) => {
|
||||||
const sources = store.get('lyrics', []) as LyricSource[];
|
const allSearchResults = await searchAllSources(params);
|
||||||
|
|
||||||
const results: Record<LyricSource, InternetProviderLyricSearchResponse[]> = {
|
const results: Record<LyricSource, InternetProviderLyricSearchResponse[]> = {
|
||||||
[LyricSource.GENIUS]: [],
|
[LyricSource.GENIUS]: [],
|
||||||
[LyricSource.LRCLIB]: [],
|
[LyricSource.LRCLIB]: [],
|
||||||
[LyricSource.NETEASE]: [],
|
[LyricSource.NETEASE]: [],
|
||||||
};
|
};
|
||||||
|
for (const item of allSearchResults) {
|
||||||
for (const source of sources) {
|
results[item.source].push(item);
|
||||||
const response = await SEARCH_FETCHERS[source](params);
|
|
||||||
|
|
||||||
if (response) {
|
|
||||||
response.forEach((result) => {
|
|
||||||
results[source].push(result);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user