fuzzyworld

hawk / commit

Added pagination for album and track API calls.

4d32aa4127dc27ad6811723d521106b1cfa308ec · Pavel Safronov · 2023-10-13 16:32:31 -0700 · parents 0d3f21676c74

Added pagination for album and track API calls.
 public/api.js  | 60 +++++++++++++++++++++++++++++++++++++---------------------
 public/main.js |  8 ++++----
 2 files changed, 42 insertions(+), 26 deletions(-)

diff --git a/public/api.js b/public/api.js
index 8aad24a..db2ac28 100644
--- a/public/api.js
+++ b/public/api.js
@@ -3,41 +3,53 @@ const apiExpiry = 3600 * 1000; // 1 hour
 async function getAlbums() {
     const db = await openDb();
     const cacheKey = 'albums';
-    const albums = await getWithExpiry(db, cacheKey);
-    if (albums) {
-        return albums;
+    const cachedAlbums = await getWithExpiry(db, cacheKey);
+    if (cachedAlbums) {
+        return cachedAlbums;
     }
 
     const accessToken = await getAccessToken();
-    const response = await fetch('https://api.spotify.com/v1/me/albums', {
-        headers: {
-            Authorization: 'Bearer ' + accessToken
-        }
-    });
+    const albums = [];
+    let url = 'https://api.spotify.com/v1/me/albums?limit=50';
+    while (url) {
+        const response = await fetch(url, {
+            headers: {
+                Authorization: 'Bearer ' + accessToken
+            }
+        });
+        const data = await response.json();
+        albums.push(...data.items);
+        url = data.next;
+    }
 
-    const data = await response.json();
-    await setWithExpiry(db, cacheKey, data, apiExpiry);
-    return data;
+    await setWithExpiry(db, cacheKey, albums, apiExpiry);
+    return albums;
 }
 
 async function getTracks(albumId) {
     const db = await openDb();
     const cacheKey = `tracks-${albumId}`;
-    const tracks = await getWithExpiry(db, cacheKey);
-    if (tracks) {
-        return tracks;
+    const cachedTracks = await getWithExpiry(db, cacheKey);
+    if (cachedTracks) {
+        return cachedTracks;
     }
 
+    const tracks = [];
     const accessToken = await getAccessToken();
-    const response = await fetch(`https://api.spotify.com/v1/albums/${albumId}/tracks`, {
-        headers: {
-            Authorization: 'Bearer ' + accessToken
-        }
-    });
+    let url = `https://api.spotify.com/v1/albums/${albumId}/tracks?limit=50`;
+    while (url) {
+        const response = await fetch(url, {
+            headers: {
+                Authorization: 'Bearer ' + accessToken
+            }
+        });
+        const data = await response.json();
+        tracks.push(...data.items);
+        url = data.next;
+    }
 
-    const data = await response.json();
-    await setWithExpiry(db, cacheKey, data, apiExpiry);
-    return data;
+    await setWithExpiry(db, cacheKey, tracks, apiExpiry);
+    return tracks;
 }
 
 async function playTrack(trackId) {
@@ -62,6 +74,10 @@ async function getPlayingTrack() {
         }
     });
 
+    if (response.status == 204) {
+        return null;
+    }
+
     const data = await response.json();
     return data;
 }
diff --git a/public/main.js b/public/main.js
index c3c1c25..c424407 100644
--- a/public/main.js
+++ b/public/main.js
@@ -96,13 +96,13 @@ async function search(minutes, seconds) {
     console.debug(albums);
 
     const foundTracks = [];
-    for (let i = 0; i < albums.items.length; i++) {
-        const album = albums.items[i];
+    for (let i = 0; i < albums.length; i++) {
+        const album = albums[i];
         const tracks = await getTracks(album.album.id);
         console.debug(tracks)
 
-        for (let j = 0; j < tracks.items.length; j++) {
-            const track = tracks.items[j];
+        for (let j = 0; j < tracks.length; j++) {
+            const track = tracks[j];
             const duration = track.duration_ms;
             const durationInSeconds = Math.floor(duration / 1000);
             const durationInMinutes = Math.floor(durationInSeconds / 60);