fix: Improve iTunes API call success rate by increasing rate limit delay and adding a User-Agent header.
This commit is contained in:
@@ -36,8 +36,8 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
for (const song of songs) {
|
for (const song of songs) {
|
||||||
try {
|
try {
|
||||||
// Rate limiting: wait 500ms between requests to be safe
|
// Rate limiting: wait 2000ms between requests to be safe (iTunes can be strict)
|
||||||
await sleep(500);
|
await sleep(2000);
|
||||||
|
|
||||||
const year = await getReleaseYearFromItunes(song.artist, song.title);
|
const year = await getReleaseYearFromItunes(song.artist, song.title);
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,11 @@ export async function getReleaseYearFromItunes(artist: string, title: string): P
|
|||||||
const term = encodeURIComponent(`${artist} ${title}`);
|
const term = encodeURIComponent(`${artist} ${title}`);
|
||||||
const url = `https://itunes.apple.com/search?term=${term}&entity=song&limit=10`;
|
const url = `https://itunes.apple.com/search?term=${term}&entity=song&limit=10`;
|
||||||
|
|
||||||
const response = await fetch(url);
|
const response = await fetch(url, {
|
||||||
|
headers: {
|
||||||
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
console.error(`iTunes API error: ${response.status} ${response.statusText}`);
|
console.error(`iTunes API error: ${response.status} ${response.statusText}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user