From 51e22f6d489ef1a068f5aaaf2024fab8ecd16d79 Mon Sep 17 00:00:00 2001 From: seth Date: Thu, 8 May 2025 07:11:55 -0400 Subject: [PATCH] Enhance track fetching and downloading logic: - Added support for checking existence of unknown file types during download. - Improved user feedback by logging downloaded track progress. - Increased the limit of fetched favorite tracks from 50 to 10,000. --- index.ts | 11 +++++++++-- src/helpers/utils.ts | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index 59f4c41..3398c71 100644 --- a/index.ts +++ b/index.ts @@ -53,21 +53,23 @@ Bun.serve({ const tracks = await utils.fetchTracks(); +let i = 1; for await (const track of tracks.items) { const { id } = track.item const createdAt = new Date(track.created) const trackId = parseInt(id) - if (await Bun.file(`downloaded/${trackId}.flac`).exists() || await Bun.file(`downloaded/${trackId}.m4a`).exists()) { + if (await Bun.file(`downloaded/${trackId}.flac`).exists() || await Bun.file(`downloaded/${trackId}.m4a`).exists() || await Bun.file(`downloaded/${trackId}.unknown`).exists()) { //console.log(`Already downloaded ${trackId}.flac`) + i++; continue } const trackData = await utils.fetchTrack(trackId) if (trackData?.status === 404 || trackData === null) { console.error(`track ${trackId} not available`) - await Bun.sleep(1000) // wait 1 second before next track + i++; continue } try { @@ -89,6 +91,11 @@ for await (const track of tracks.items) { } catch (e) { console.error(`Failed to download ${trackId}.flac`, e) } + + // Downloaded track 1 of 10, etc + + console.log(`Downloaded track ${i} of ${tracks.items.length}`) + i++; } console.log("Done") diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index a0c9845..31152eb 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -45,7 +45,7 @@ export default class { } async fetchTracks() { - const tracks = await fetch(`https://desktop.tidal.com/v1/users/${this._userId}/favorites/tracks?offset=0&limit=50&order=DATE&orderDirection=DESC&countryCode=US&locale=en_US&deviceType=DESKTOP`, { + const tracks = await fetch(`https://desktop.tidal.com/v1/users/${this._userId}/favorites/tracks?offset=0&limit=10000&order=DATE&orderDirection=DESC&countryCode=US&locale=en_US&deviceType=DESKTOP`, { headers: this._authHeaders })