mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-02 10:53:33 +00:00
Improved lyric syncing, fetch
- uses a somewhat more sane way to parse lyrics and teardown timeouts - adds 'seeked' to setCurrentTime to make detecting seeks in lyric much easier - adds ability to fetch lyrics from genius/netease (desktop only)
This commit is contained in:
parent
23f9bd4e9f
commit
85d2576bdc
25 changed files with 907 additions and 118 deletions
59
src/main/features/core/lyrics/genius.ts
Normal file
59
src/main/features/core/lyrics/genius.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import axios, { AxiosResponse } from 'axios';
|
||||
import { load } from 'cheerio';
|
||||
import type { QueueSong } from '/@/renderer/api/types';
|
||||
|
||||
const search_url = 'https://genius.com/api/search/song';
|
||||
|
||||
async function getSongURL(metadata: QueueSong) {
|
||||
let result: AxiosResponse<any, any>;
|
||||
try {
|
||||
result = await axios.get(search_url, {
|
||||
params: {
|
||||
per_page: '1',
|
||||
q: `${metadata.artistName} ${metadata.name}`,
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('Genius search request got an error!', e);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return result.data.response?.sections?.[0]?.hits?.[0]?.result?.url;
|
||||
}
|
||||
|
||||
async function getLyricsFromGenius(url: string): Promise<string | null> {
|
||||
let result: AxiosResponse<string, any>;
|
||||
try {
|
||||
result = await axios.get<string>(url, { responseType: 'text' });
|
||||
} catch (e) {
|
||||
console.error('Genius lyrics request got an error!', e);
|
||||
return null;
|
||||
}
|
||||
|
||||
const $ = load(result.data.split('<br/>').join('\n'));
|
||||
const lyricsDiv = $('div.lyrics');
|
||||
|
||||
if (lyricsDiv.length > 0) return lyricsDiv.text().trim();
|
||||
|
||||
const lyricSections = $('div[class^=Lyrics__Container]')
|
||||
.map((_, e) => $(e).text())
|
||||
.toArray()
|
||||
.join('\n');
|
||||
return lyricSections;
|
||||
}
|
||||
|
||||
export async function query(metadata: QueueSong): Promise<string | null> {
|
||||
const songId = await getSongURL(metadata);
|
||||
if (!songId) {
|
||||
console.error('Could not find the song on Genius!');
|
||||
return null;
|
||||
}
|
||||
|
||||
const lyrics = await getLyricsFromGenius(songId);
|
||||
if (!lyrics) {
|
||||
console.error('Could not get lyrics on Genius!');
|
||||
return null;
|
||||
}
|
||||
|
||||
return lyrics;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue