83 lines
No EOL
2.6 KiB
TypeScript
83 lines
No EOL
2.6 KiB
TypeScript
import Auth from "./src/helpers/auth";
|
|
import Utils from "./src/helpers/utils";
|
|
import { utimes } from "fs/promises";
|
|
|
|
const utils = new Utils(await Auth());
|
|
|
|
Bun.serve({
|
|
routes: {
|
|
"/api/track/:id": async req => {
|
|
const trackId = parseInt(req.params.id)
|
|
|
|
const file = Bun.file(`downloaded/${trackId}.flac`)
|
|
|
|
if (await file.exists()) {
|
|
return new Response(await file.arrayBuffer(), {
|
|
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(trackId)
|
|
|
|
const audio = await utils.tagFlac(trackId, await utils.downloadFlac(manifestMimeType, manifest))
|
|
|
|
await Bun.write(`downloaded/${trackId}.flac`, audio)
|
|
|
|
return new Response(audio, {
|
|
headers: {
|
|
"Content-Type": "audio/flac",
|
|
"Content-Disposition": `attachment; filename="${trackId}.flac"`,
|
|
"Cache-Control": "public, max-age=31536000",
|
|
"ETag": trackId.toString(),
|
|
}
|
|
})
|
|
},
|
|
"/api/@me/tracks": async () => {
|
|
const tracks = await utils.fetchTracks();
|
|
|
|
return new Response(Bun.gzipSync(JSON.stringify(tracks)), {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Content-Encoding": "gzip",
|
|
}
|
|
})
|
|
}
|
|
},
|
|
development: true
|
|
})
|
|
|
|
/*
|
|
|
|
const tracks = await utils.fetchTracks();
|
|
|
|
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()) {
|
|
//console.log(`Already downloaded ${trackId}.flac`)
|
|
continue
|
|
}
|
|
|
|
const trackData = await utils.fetchTrack(parseInt(id))
|
|
|
|
try {
|
|
const audio = await utils.tagFlac(trackId, await utils.downloadFlac(trackData.manifestMimeType, trackData.manifest))
|
|
|
|
await Bun.write(`downloaded/${trackId}.flac`, audio)
|
|
await utimes(`downloaded/${trackId}.flac`, createdAt, createdAt)
|
|
//console.log(`Downloaded ${trackId}.flac`)
|
|
} catch (e) {
|
|
console.error(`Failed to download ${trackId}.flac`)
|
|
}
|
|
}
|
|
|
|
console.log("Done")
|
|
*/ |