Add lyric search functions and query

This commit is contained in:
jeffvli 2023-06-08 03:40:34 -07:00 committed by Jeff
parent 43c11ab6e3
commit 0fa5b6496f
7 changed files with 210 additions and 23 deletions

View file

@ -1,6 +1,10 @@
import axios, { AxiosResponse } from 'axios';
import { load } from 'cheerio';
import type { InternetProviderLyricResponse, QueueSong } from '/@/renderer/api/types';
import type {
InternetProviderLyricResponse,
InternetProviderLyricSearchResponse,
LyricSearchQuery,
} from '/@/renderer/api/types';
import { LyricSource } from '../../../../renderer/types';
const SEARCH_URL = 'https://genius.com/api/search/song';
@ -13,13 +17,86 @@ interface GeniusResponse {
url: string;
}
async function getSongURL(metadata: QueueSong): Promise<GeniusResponse | undefined> {
let result: AxiosResponse<any, any>;
interface GeniusSearchResponse {
response: {
sections: {
hits: {
highlights: any[];
index: string;
result: {
_type: string;
annotation_count: number;
api_path: string;
artist_names: string;
featured_artits: any[];
full_title: string;
header_image_thumbnail_url: string;
header_image_url: string;
id: number;
instrumental: boolean;
language: string;
lyrics_owner_id: number;
lyrics_state: string;
lyrics_updated_at: number;
path: string;
primary_artist: Record<any, any>;
pyongs_count: number;
relationships_index_url: string;
release_date_components: Record<any, any>;
release_date_for_display: string;
release_date_with_abbreviated_month_for_display: string;
song_art_image_thumbnail_url: string;
song_art_image_url: string;
stats: Record<any, any>;
title: string;
title_with_featured: string;
updated_by_human_at: number;
url: string;
};
type: string;
}[];
type: string;
}[];
};
}
export async function getSearchResults(
params: LyricSearchQuery,
): Promise<InternetProviderLyricSearchResponse[] | null> {
let result: AxiosResponse<GeniusSearchResponse>;
try {
result = await axios.get(SEARCH_URL, {
params: {
per_page: '5',
q: `${params.artist} ${params.name}`,
},
});
} catch (e) {
console.error('Genius search request got an error!', e);
return null;
}
const songs = result.data.response?.sections?.[0]?.hits?.map((hit) => hit.result);
if (!songs) return null;
return songs.map((song: any) => {
return {
artist: song.artist_names,
id: song.url,
name: song.full_title,
source: LyricSource.GENIUS,
};
});
}
async function getSongURL(params: LyricSearchQuery): Promise<GeniusResponse | undefined> {
let result: AxiosResponse<GeniusSearchResponse>;
try {
result = await axios.get(SEARCH_URL, {
params: {
per_page: '1',
q: `${metadata.artistName} ${metadata.name}`,
q: `${params.artist} ${params.name}`,
},
});
} catch (e) {
@ -61,8 +138,10 @@ async function getLyricsFromGenius(url: string): Promise<string | null> {
return lyricSections;
}
export async function query(metadata: QueueSong): Promise<InternetProviderLyricResponse | null> {
const response = await getSongURL(metadata);
export async function query(
params: LyricSearchQuery,
): Promise<InternetProviderLyricResponse | null> {
const response = await getSongURL(params);
if (!response) {
console.error('Could not find the song on Genius!');
return null;