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.
This commit is contained in:
parent
9275f2160c
commit
51e22f6d48
2 changed files with 10 additions and 3 deletions
11
index.ts
11
index.ts
|
@ -53,21 +53,23 @@ Bun.serve({
|
||||||
|
|
||||||
const tracks = await utils.fetchTracks();
|
const tracks = await utils.fetchTracks();
|
||||||
|
|
||||||
|
let i = 1;
|
||||||
for await (const track of tracks.items) {
|
for await (const track of tracks.items) {
|
||||||
const { id } = track.item
|
const { id } = track.item
|
||||||
const createdAt = new Date(track.created)
|
const createdAt = new Date(track.created)
|
||||||
|
|
||||||
const trackId = parseInt(id)
|
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`)
|
//console.log(`Already downloaded ${trackId}.flac`)
|
||||||
|
i++;
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
const trackData = await utils.fetchTrack(trackId)
|
const trackData = await utils.fetchTrack(trackId)
|
||||||
if (trackData?.status === 404 || trackData === null) {
|
if (trackData?.status === 404 || trackData === null) {
|
||||||
console.error(`track ${trackId} not available`)
|
console.error(`track ${trackId} not available`)
|
||||||
await Bun.sleep(1000) // wait 1 second before next track
|
i++;
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -89,6 +91,11 @@ for await (const track of tracks.items) {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`Failed to download ${trackId}.flac`, 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")
|
console.log("Done")
|
||||||
|
|
|
@ -45,7 +45,7 @@ export default class {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchTracks() {
|
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
|
headers: this._authHeaders
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue