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:
Kendall Garner 2023-06-04 23:15:36 -07:00 committed by Jeff
parent 9622cd346c
commit 007a099951
11 changed files with 314 additions and 61 deletions

View file

@ -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,
};
}