2023-06-11 19:45:50 +00:00
|
|
|
// Credits to https://github.com/tranxuanthang/lrcget for API implementation
|
|
|
|
|
import axios, { AxiosResponse } from 'axios';
|
2025-05-18 14:03:18 -07:00
|
|
|
|
2023-06-11 19:45:50 +00:00
|
|
|
import {
|
2023-07-01 19:10:05 -07:00
|
|
|
InternetProviderLyricResponse,
|
|
|
|
|
InternetProviderLyricSearchResponse,
|
|
|
|
|
LyricSearchQuery,
|
|
|
|
|
LyricSource,
|
2025-05-18 14:03:18 -07:00
|
|
|
} from '.';
|
|
|
|
|
import { orderSearchResults } from './shared';
|
2023-06-11 19:45:50 +00:00
|
|
|
|
|
|
|
|
const FETCH_URL = 'https://lrclib.net/api/get';
|
|
|
|
|
const SEEARCH_URL = 'https://lrclib.net/api/search';
|
|
|
|
|
|
|
|
|
|
const TIMEOUT_MS = 5000;
|
|
|
|
|
|
|
|
|
|
export interface LrcLibSearchResponse {
|
2023-07-01 19:10:05 -07:00
|
|
|
albumName: string;
|
|
|
|
|
artistName: string;
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
2023-06-11 19:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface LrcLibTrackResponse {
|
2023-07-01 19:10:05 -07:00
|
|
|
albumName: string;
|
|
|
|
|
artistName: string;
|
|
|
|
|
duration: number;
|
|
|
|
|
id: number;
|
|
|
|
|
instrumental: boolean;
|
|
|
|
|
isrc: string;
|
|
|
|
|
lang: string;
|
|
|
|
|
name: string;
|
2025-05-18 14:03:18 -07:00
|
|
|
plainLyrics: null | string;
|
2023-07-01 19:10:05 -07:00
|
|
|
releaseDate: string;
|
|
|
|
|
spotifyId: string;
|
2025-05-18 14:03:18 -07:00
|
|
|
syncedLyrics: null | string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getLyricsBySongId(songId: string): Promise<null | string> {
|
|
|
|
|
let result: AxiosResponse<LrcLibTrackResponse, any>;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
result = await axios.get<LrcLibTrackResponse>(`${FETCH_URL}/${songId}`);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('LrcLib lyrics request got an error!', e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.data.syncedLyrics || result.data.plainLyrics || null;
|
2023-06-11 19:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getSearchResults(
|
2023-07-01 19:10:05 -07:00
|
|
|
params: LyricSearchQuery,
|
2023-06-11 19:45:50 +00:00
|
|
|
): Promise<InternetProviderLyricSearchResponse[] | null> {
|
2023-07-01 19:10:05 -07:00
|
|
|
let result: AxiosResponse<LrcLibSearchResponse[]>;
|
|
|
|
|
|
|
|
|
|
if (!params.name) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
result = await axios.get<LrcLibSearchResponse[]>(SEEARCH_URL, {
|
|
|
|
|
params: {
|
|
|
|
|
q: params.name,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('LrcLib search request got an error!', e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result.data) return null;
|
|
|
|
|
|
|
|
|
|
const songResults: InternetProviderLyricSearchResponse[] = result.data.map((song) => {
|
|
|
|
|
return {
|
|
|
|
|
artist: song.artistName,
|
|
|
|
|
id: String(song.id),
|
|
|
|
|
name: song.name,
|
|
|
|
|
source: LyricSource.LRCLIB,
|
|
|
|
|
};
|
2023-06-11 19:45:50 +00:00
|
|
|
});
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return orderSearchResults({ params, results: songResults });
|
2023-06-11 19:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function query(
|
2023-07-01 19:10:05 -07:00
|
|
|
params: LyricSearchQuery,
|
2023-06-11 19:45:50 +00:00
|
|
|
): Promise<InternetProviderLyricResponse | null> {
|
2023-07-01 19:10:05 -07:00
|
|
|
let result: AxiosResponse<LrcLibTrackResponse, any>;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
result = await axios.get<LrcLibTrackResponse>(FETCH_URL, {
|
|
|
|
|
params: {
|
|
|
|
|
album_name: params.album,
|
|
|
|
|
artist_name: params.artist,
|
|
|
|
|
duration: params.duration,
|
|
|
|
|
track_name: params.name,
|
|
|
|
|
},
|
|
|
|
|
timeout: TIMEOUT_MS,
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('LrcLib search request got an error!', e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lyrics = result.data.syncedLyrics || result.data.plainLyrics || null;
|
|
|
|
|
|
|
|
|
|
if (!lyrics) {
|
|
|
|
|
console.error(`Could not get lyrics on LrcLib!`);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
artist: result.data.artistName,
|
|
|
|
|
id: String(result.data.id),
|
|
|
|
|
lyrics,
|
|
|
|
|
name: result.data.name,
|
|
|
|
|
source: LyricSource.LRCLIB,
|
|
|
|
|
};
|
2023-06-11 19:45:50 +00:00
|
|
|
}
|