A decentralized music tracking and discovery platform built on AT Protocol 🎵

fix: implement retry logic for fetching genres and pictures of artists

+50 -41
+50 -41
apps/api/src/scripts/genres.ts
··· 41 41 42 42 async function getGenresAndPicture(artists) { 43 43 for (const artist of artists) { 44 - const token = await getSpotifyToken(); 45 - // search artist by name on spotify 46 - const result = await fetch( 47 - `https://api.spotify.com/v1/search?q=${encodeURIComponent(artist.name)}&type=artist&limit=1`, 48 - { 49 - headers: { 50 - Authorization: `Bearer ${token}`, 51 - }, 52 - } 53 - ) 54 - .then( 55 - (res) => 56 - res.json() as Promise<{ 57 - artists: { 58 - items: Array<{ 59 - id: string; 60 - name: string; 61 - genres: string[]; 62 - images: Array<{ url: string }>; 63 - }>; 64 - }; 65 - }> 66 - ) 67 - .then(async (data) => _.get(data, "artists.items.0")); 44 + do { 45 + try { 46 + const token = await getSpotifyToken(); 47 + // search artist by name on spotify 48 + const result = await fetch( 49 + `https://api.spotify.com/v1/search?q=${encodeURIComponent(artist.name)}&type=artist&limit=1`, 50 + { 51 + headers: { 52 + Authorization: `Bearer ${token}`, 53 + }, 54 + } 55 + ) 56 + .then( 57 + (res) => 58 + res.json() as Promise<{ 59 + artists: { 60 + items: Array<{ 61 + id: string; 62 + name: string; 63 + genres: string[]; 64 + images: Array<{ url: string }>; 65 + }>; 66 + }; 67 + }> 68 + ) 69 + .then(async (data) => _.get(data, "artists.items.0")); 68 70 69 - if (result) { 70 - console.log(JSON.stringify(result, null, 2), "\n"); 71 - if (result.genres && result.genres.length > 0) { 72 - await ctx.db 73 - .update(tables.artists) 74 - .set({ genres: result.genres }) 75 - .where(eq(tables.artists.id, artist.id)) 76 - .execute(); 77 - } 78 - // update artist picture if not set 79 - if (!artist.picture && result.images && result.images.length > 0) { 80 - await ctx.db 81 - .update(tables.artists) 82 - .set({ picture: result.images[0].url }) 83 - .where(eq(tables.artists.id, artist.id)) 84 - .execute(); 71 + if (result) { 72 + console.log(JSON.stringify(result, null, 2), "\n"); 73 + if (result.genres && result.genres.length > 0) { 74 + await ctx.db 75 + .update(tables.artists) 76 + .set({ genres: result.genres }) 77 + .where(eq(tables.artists.id, artist.id)) 78 + .execute(); 79 + } 80 + // update artist picture if not set 81 + if (!artist.picture && result.images && result.images.length > 0) { 82 + await ctx.db 83 + .update(tables.artists) 84 + .set({ picture: result.images[0].url }) 85 + .where(eq(tables.artists.id, artist.id)) 86 + .execute(); 87 + } 88 + } 89 + break; // exit the retry loop on success 90 + } catch (error) { 91 + console.error("Error fetching genres for artist:", artist.name, error); 92 + // wait for a while before retrying 93 + await new Promise((resolve) => setTimeout(resolve, 1000)); 85 94 } 86 - } 95 + } while (true); 87 96 88 97 // sleep for a while to avoid rate limiting 89 98 await new Promise((resolve) => setTimeout(resolve, 1000));