Skip to content

Commit 320e41e

Browse files
committed
Better requests
1 parent 8ed1bf5 commit 320e41e

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

src/lib/api/api.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class SongAPI {
150150

151151
return await res.json();
152152
} catch (e) {
153-
console.warn("HTTP error", e);
153+
console.warn("HTTP error", e, url);
154154
}
155155
}
156156

@@ -179,28 +179,19 @@ export class SongAPI {
179179

180180
return search.recordings[0];
181181
}
182-
183-
private async queryCoverArtData(releaseId: string): Promise<CAARelease | undefined> {
184-
const url = encodeURI(`${COVERARCH}/release/${releaseId}`);
185-
186-
const data: CAARelease | undefined = await this.get(url);
187-
188-
return data;
189-
}
190-
182+
191183
private async querySongCoverArt(song: MBRecordingSearchResult): Promise<string | undefined> {
192184
for(let i = 0; i < song.releases.length && i < 5; i++) {
193185
const rel = song.releases[i];
194186

195-
const data = await this.queryCoverArtData(rel.id);
187+
try {
188+
const url = `${COVERARCH}/release/${rel.id}/front`;
189+
const image = await this.httpGet(url);
196190

197-
if(!data)
198-
continue;
191+
if(image.ok)
192+
return url;
193+
} catch {
199194

200-
for(const img of data.images) {
201-
if(img.front) {
202-
return img.thumbnails.small ?? img.image;
203-
}
204195
}
205196
}
206197
}

src/lib/util/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function wait(ms: number): Promise<undefined> {
2+
return new Promise((resolve) => {
3+
setTimeout(resolve, ms);
4+
});
5+
}

src/routes/+page.svelte

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import StatsView from "$lib/layout/StatsView.svelte";
77
import { SongAPI } from "$lib/api/api";
88
import ProgressBar from "$lib/comp/ProgressBar.svelte";
9+
import { wait } from "$lib/util";
910
1011
const api = new SongAPI();
1112
@@ -28,12 +29,19 @@
2829
const songs = stats.data.songs.slice(0, config.songRankCount);
2930
progressMax = songs.length * 2;
3031
31-
for(const song of songs) {
32+
for(let i = 0; i < songs.length; i++) {
33+
const song = songs[i];
34+
3235
const artist = await api.queryArtistByName(song.artist);
3336
progress++;
3437
3538
await api.querySongByName(song.title, artist?.id);
3639
progress++;
40+
41+
// Account for rate limit
42+
if(i % 20 == 0) {
43+
await wait(1000);
44+
}
3745
}
3846
3947
statsProcessed = true;

0 commit comments

Comments
 (0)