2023-05-28 14:31:49 -07:00
|
|
|
import axios, { AxiosResponse } from 'axios';
|
|
|
|
|
import { load } from 'cheerio';
|
2023-06-04 23:15:36 -07:00
|
|
|
import type { InternetProviderLyricResponse, QueueSong } from '/@/renderer/api/types';
|
2023-06-05 02:42:25 -07:00
|
|
|
import { LyricSource } from '../../../../renderer/types';
|
2023-05-28 14:31:49 -07:00
|
|
|
|
2023-06-03 07:15:02 -07:00
|
|
|
const SEARCH_URL = 'https://genius.com/api/search/song';
|
|
|
|
|
|
|
|
|
|
// Adapted from https://github.com/NyaomiDEV/Sunamu/blob/master/src/main/lyricproviders/genius.ts
|
2023-05-28 14:31:49 -07:00
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
interface GeniusResponse {
|
|
|
|
|
artist: string;
|
2023-06-05 02:42:25 -07:00
|
|
|
name: string;
|
2023-06-04 23:15:36 -07:00
|
|
|
url: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getSongURL(metadata: QueueSong): Promise<GeniusResponse | undefined> {
|
2023-05-28 14:31:49 -07:00
|
|
|
let result: AxiosResponse<any, any>;
|
|
|
|
|
try {
|
2023-06-03 07:15:02 -07:00
|
|
|
result = await axios.get(SEARCH_URL, {
|
2023-05-28 14:31:49 -07:00
|
|
|
params: {
|
|
|
|
|
per_page: '1',
|
|
|
|
|
q: `${metadata.artistName} ${metadata.name}`,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Genius search request got an error!', e);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
const hit = result.data.response?.sections?.[0]?.hits?.[0]?.result;
|
|
|
|
|
|
|
|
|
|
if (!hit) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
artist: hit.artist_names,
|
2023-06-05 02:42:25 -07:00
|
|
|
name: hit.full_title,
|
2023-06-04 23:15:36 -07:00
|
|
|
url: hit.url,
|
|
|
|
|
};
|
2023-05-28 14:31:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
export async function query(metadata: QueueSong): Promise<InternetProviderLyricResponse | null> {
|
|
|
|
|
const response = await getSongURL(metadata);
|
|
|
|
|
if (!response) {
|
2023-05-28 14:31:49 -07:00
|
|
|
console.error('Could not find the song on Genius!');
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
const lyrics = await getLyricsFromGenius(response.url);
|
2023-05-28 14:31:49 -07:00
|
|
|
if (!lyrics) {
|
|
|
|
|
console.error('Could not get lyrics on Genius!');
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 23:15:36 -07:00
|
|
|
return {
|
|
|
|
|
artist: response.artist,
|
|
|
|
|
lyrics,
|
2023-06-05 02:42:25 -07:00
|
|
|
name: response.name,
|
|
|
|
|
source: LyricSource.GENIUS,
|
2023-06-04 23:15:36 -07:00
|
|
|
};
|
2023-05-28 14:31:49 -07:00
|
|
|
}
|