MusicControls: TidalLyrics Set Retry amount
This commit is contained in:
parent
1ab6743545
commit
02329ead03
1 changed files with 26 additions and 14 deletions
|
|
@ -8,23 +8,35 @@ import { settings } from "../../settings";
|
|||
import { Track } from "../TidalStore";
|
||||
import { EnhancedLyric } from "./types";
|
||||
|
||||
export async function getLyrics(track: Track | null, url?: string): Promise<EnhancedLyric[] | null> {
|
||||
if (!track) return null;
|
||||
if (!track.id) return null;
|
||||
export async function getLyrics(track: Track | null, url?: string, retries = 3): Promise<EnhancedLyric[] | null> {
|
||||
if (!track?.id) return null;
|
||||
|
||||
const { TidalLyricFetch } = settings.store;
|
||||
const res = await fetch((url ?? TidalLyricFetch) + "lyrics?tidal_id=" + track.id);
|
||||
const defaultUrl = settings.def.TidalLyricFetch.default;
|
||||
if (!res.ok) {
|
||||
if (TidalLyricFetch !== defaultUrl) {
|
||||
return getLyrics(track, defaultUrl);
|
||||
const fetchUrl = (url ?? TidalLyricFetch) + "lyrics?tidal_id=" + track.id;
|
||||
|
||||
try {
|
||||
const res = await fetch(fetchUrl);
|
||||
if (!res.ok) {
|
||||
if (retries > 1) {
|
||||
return getLyrics(track, url ?? defaultUrl, retries - 1);
|
||||
}
|
||||
console.error("Failed to fetch lyrics after retries:", res.status, res.statusText);
|
||||
return null;
|
||||
}
|
||||
console.error("Failed to fetch lyrics", res.status, res.statusText);
|
||||
|
||||
const data = await res.json();
|
||||
if (!data?.enhancedLyrics || !Array.isArray(data.enhancedLyrics)) {
|
||||
console.error("Invalid lyrics data", data);
|
||||
return null;
|
||||
}
|
||||
|
||||
return data.enhancedLyrics;
|
||||
} catch (err) {
|
||||
if (retries > 1) {
|
||||
return getLyrics(track, url ?? defaultUrl, retries - 1);
|
||||
}
|
||||
console.error("Error fetching lyrics after retries:", err);
|
||||
return null;
|
||||
}
|
||||
const data = await res.json();
|
||||
if (!data || !data.enhancedLyrics || !Array.isArray(data.enhancedLyrics)) {
|
||||
console.error("Invalid lyrics data", data);
|
||||
return null;
|
||||
}
|
||||
return data.enhancedLyrics;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue