Skip to content

Commit

Permalink
Add retry method to iTunes API request
Browse files Browse the repository at this point in the history
This adds a method to retry the iTunes API request up to 3 times. For whatever reason, the API will sometimes fail and return a "Service Unavailable" error but will go through on the second attempt.

Co-authored-by: NextFire <[email protected]>
  • Loading branch information
shiibe and NextFire committed Aug 30, 2024
1 parent ee65d17 commit 609179f
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions music-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AppleMusicDiscordRPC {
console.error(err);
}
console.log("Reconnecting in %dms", this.defaultTimeout);
await AppleMusicDiscordRPC.sleep(this.defaultTimeout);
await sleep(this.defaultTimeout);
}
}

Expand All @@ -39,7 +39,7 @@ class AppleMusicDiscordRPC {
while (true) {
const timeout = await this.setActivity();
console.log("Next setActivity in %dms", timeout);
await AppleMusicDiscordRPC.sleep(timeout);
await sleep(timeout);
}
} finally {
// Ensure the connection is properly closed
Expand Down Expand Up @@ -145,17 +145,17 @@ class AppleMusicDiscordRPC {
return version;
}

static sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

static truncateString(value: string, maxLength = 128): string {
return value.length <= maxLength
? value
: `${value.slice(0, maxLength - 3)}...`;
}
}

function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const client = await AppleMusicDiscordRPC.create();
await client.run();
//#endregion
Expand Down Expand Up @@ -216,11 +216,10 @@ async function fetchTrackExtras(props: iTunesProps): Promise<TrackExtras> {
};
}

async function iTunesSearch({
name,
artist,
album,
}: iTunesProps): Promise<iTunesSearchResponse | undefined> {
async function iTunesSearch(
{ name, artist, album }: iTunesProps,
retryCount: number = 3
): Promise<iTunesSearchResponse | undefined> {
// Asterisks tend to result in no songs found, and songs are usually able to be found without it
const query = `${name} ${artist} ${album}`.replace("*", "");
const params = new URLSearchParams({
Expand All @@ -229,15 +228,23 @@ async function iTunesSearch({
term: query,
});
const url = `https://itunes.apple.com/search?${params}`;
const resp = await fetch(url);

if (!resp.ok) {
console.error("iTunes API error:", resp.statusText, url);
resp.body?.cancel();
return;
for (let i = 0; i < retryCount; i++) {
const resp = await fetch(url);
if (!resp.ok) {
console.error(
"Failed to fetch from iTunes API: %s %s (Attempt %d/%d)",
resp.statusText,
url,
i + 1,
retryCount
);
resp.body?.cancel();
await sleep(200);
continue;
}
return (await resp.json()) as iTunesSearchResponse;
}

return (await resp.json()) as iTunesSearchResponse;
}

async function musicBrainzArtwork({
Expand Down

0 comments on commit 609179f

Please sign in to comment.