mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-01 18:33: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,12 +1,18 @@
|
|||
import axios, { AxiosResponse } from 'axios';
|
||||
import { load } from 'cheerio';
|
||||
import type { QueueSong } from '/@/renderer/api/types';
|
||||
import type { InternetProviderLyricResponse, QueueSong } from '/@/renderer/api/types';
|
||||
|
||||
const SEARCH_URL = 'https://genius.com/api/search/song';
|
||||
|
||||
// Adapted from https://github.com/NyaomiDEV/Sunamu/blob/master/src/main/lyricproviders/genius.ts
|
||||
|
||||
async function getSongURL(metadata: QueueSong) {
|
||||
interface GeniusResponse {
|
||||
artist: string;
|
||||
title: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
async function getSongURL(metadata: QueueSong): Promise<GeniusResponse | undefined> {
|
||||
let result: AxiosResponse<any, any>;
|
||||
try {
|
||||
result = await axios.get(SEARCH_URL, {
|
||||
|
|
@ -20,7 +26,17 @@ async function getSongURL(metadata: QueueSong) {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
return result.data.response?.sections?.[0]?.hits?.[0]?.result?.url;
|
||||
const hit = result.data.response?.sections?.[0]?.hits?.[0]?.result;
|
||||
|
||||
if (!hit) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
artist: hit.artist_names,
|
||||
title: hit.full_title,
|
||||
url: hit.url,
|
||||
};
|
||||
}
|
||||
|
||||
async function getLyricsFromGenius(url: string): Promise<string | null> {
|
||||
|
|
@ -44,18 +60,22 @@ async function getLyricsFromGenius(url: string): Promise<string | null> {
|
|||
return lyricSections;
|
||||
}
|
||||
|
||||
export async function query(metadata: QueueSong): Promise<string | null> {
|
||||
const songId = await getSongURL(metadata);
|
||||
if (!songId) {
|
||||
export async function query(metadata: QueueSong): Promise<InternetProviderLyricResponse | null> {
|
||||
const response = await getSongURL(metadata);
|
||||
if (!response) {
|
||||
console.error('Could not find the song on Genius!');
|
||||
return null;
|
||||
}
|
||||
|
||||
const lyrics = await getLyricsFromGenius(songId);
|
||||
const lyrics = await getLyricsFromGenius(response.url);
|
||||
if (!lyrics) {
|
||||
console.error('Could not get lyrics on Genius!');
|
||||
return null;
|
||||
}
|
||||
|
||||
return lyrics;
|
||||
return {
|
||||
artist: response.artist,
|
||||
lyrics,
|
||||
title: response.title,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,18 @@
|
|||
import axios, { AxiosResponse } from 'axios';
|
||||
import type { QueueSong } from '/@/renderer/api/types';
|
||||
import type { InternetProviderLyricResponse, QueueSong } from '/@/renderer/api/types';
|
||||
|
||||
const SEARCH_URL = 'https://music.163.com/api/search/get';
|
||||
const LYRICS_URL = 'https://music.163.com/api/song/lyric';
|
||||
|
||||
// Adapted from https://github.com/NyaomiDEV/Sunamu/blob/master/src/main/lyricproviders/netease.ts
|
||||
|
||||
async function getSongId(metadata: QueueSong) {
|
||||
interface NetEaseResponse {
|
||||
artist: string;
|
||||
id: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
async function getSongId(metadata: QueueSong): Promise<NetEaseResponse | undefined> {
|
||||
let result: AxiosResponse<any, any>;
|
||||
try {
|
||||
result = await axios.get(SEARCH_URL, {
|
||||
|
|
@ -22,10 +28,20 @@ async function getSongId(metadata: QueueSong) {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
return result?.data.result?.songs?.[0].id;
|
||||
const song = result?.data.result?.songs?.[0];
|
||||
|
||||
if (!song) return undefined;
|
||||
|
||||
const artist = song.artists ? song.artists.map((artist: any) => artist.name).join(', ') : '';
|
||||
|
||||
return {
|
||||
artist,
|
||||
id: song.id,
|
||||
title: song.name,
|
||||
};
|
||||
}
|
||||
|
||||
async function getLyricsFromSongId(songId: string) {
|
||||
async function getLyricsFromSongId(songId: string): Promise<string | undefined> {
|
||||
let result: AxiosResponse<any, any>;
|
||||
try {
|
||||
result = await axios.get(LYRICS_URL, {
|
||||
|
|
@ -43,18 +59,22 @@ async function getLyricsFromSongId(songId: string) {
|
|||
return result.data.klyric?.lyric || result.data.lrc?.lyric;
|
||||
}
|
||||
|
||||
export async function query(metadata: QueueSong): Promise<string | null> {
|
||||
const songId = await getSongId(metadata);
|
||||
if (!songId) {
|
||||
export async function query(metadata: QueueSong): Promise<InternetProviderLyricResponse | null> {
|
||||
const response = await getSongId(metadata);
|
||||
if (!response) {
|
||||
console.error('Could not find the song on NetEase!');
|
||||
return null;
|
||||
}
|
||||
|
||||
const lyrics = await getLyricsFromSongId(songId);
|
||||
const lyrics = await getLyricsFromSongId(response.id);
|
||||
if (!lyrics) {
|
||||
console.error('Could not get lyrics on NetEase!');
|
||||
return null;
|
||||
}
|
||||
|
||||
return lyrics;
|
||||
return {
|
||||
artist: response.artist,
|
||||
lyrics,
|
||||
title: response.title,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue