From 80d73027a6471a284d532d690f32fce811a37f94 Mon Sep 17 00:00:00 2001 From: wont-stream <143244075+wont-stream@users.noreply.github.com> Date: Thu, 17 Apr 2025 01:07:38 -0400 Subject: [PATCH] Add track downloading functionality and improve error handling --- index.ts | 28 +++++++++++++++++++++++++++- src/helpers/utils.ts | 2 -- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index a78af63..e63e4f2 100644 --- a/index.ts +++ b/index.ts @@ -51,4 +51,30 @@ Bun.serve({ } }, development: true -}) \ No newline at end of file +}) + +const tracks = await utils.fetchTracks(); + +for await (const track of tracks.items) { + const { id } = track.item + + const trackId = parseInt(id) + + if (await Bun.file(`downloaded/${trackId}.flac`).exists()) { + console.log(`Already downloaded ${trackId}.flac`) + continue + } + + const { manifestMimeType, manifest } = await utils.fetchTrack(parseInt(id)) + + try { + const audio = await utils.tagFlac(trackId, await utils.downloadFlac(manifestMimeType, manifest)) + + await Bun.write(`downloaded/${trackId}.flac`, audio) + console.log(`Downloaded ${trackId}.flac`) + } catch (e) { + console.error(`Failed to download ${trackId}.flac`) + } +} + +console.log("Done") \ No newline at end of file diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 4fdc732..3064894 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -113,8 +113,6 @@ export default class { ], } - console.log(track) - return FlacStreamTagger.fromBuffer(Buffer.from(audioBuffer), { tagMap: { title: track.title,