import Auth from "./src/helpers/auth"; import Utils from "./src/helpers/utils"; const utils = new Utils(await Auth()); Bun.serve({ routes: { "/track": async (req: { url: string }) => { const url = new URL(req.url) const id = url.searchParams.get("id") if (!id) { return new Response("Missing id", { status: 400 }) } const trackId = parseInt(id) const file = Bun.file(`downloaded/${trackId}.flac`) if (await file.exists()) { return new Response(file, { headers: { "Content-Type": "audio/flac", "Content-Disposition": `attachment; filename="${trackId}.flac"`, "Cache-Control": "public, max-age=31536000", "ETag": trackId.toString(), } }) } const { manifestMimeType, manifest } = await utils.fetchTrack(parseInt(id)) const audio = await utils.tagFlac(trackId, await utils.downloadFlac(manifestMimeType, manifest)) await Bun.write(`downloaded/${trackId}.flac`, audio) return Response.json(audio, { headers: { "Content-Type": "audio/flac", "Content-Disposition": `attachment; filename="${trackId}.flac"`, "Cache-Control": "public, max-age=31536000", "ETag": trackId.toString(), } }) }, "/tracks": async () => { const tracks = await utils.fetchTracks(); return Response.json(tracks); } }, development: true }) 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")