56 lines
1.7 KiB
JavaScript
56 lines
1.7 KiB
JavaScript
// @ts-check
|
|
|
|
const fs = require("fs")
|
|
const os = require("os")
|
|
const {tag} = require("@cloudrac3r/html-template-tag")
|
|
|
|
const collection = JSON.parse(fs.readFileSync("scripts/collection.json", "utf8"))
|
|
|
|
const table = collection.items.map(item => {
|
|
const artist = item.band_name.replace(/[:,/[\]]/g, "-")
|
|
const title = item.item_title.replace(/[:/|[\]]/g, "-").replace(/^[-.]+|-+$/g, "").replace(/--+/g, "-").replaceAll("\u3000", " ")
|
|
const file = `${artist} - ${title}`
|
|
const exists = fs.existsSync(`/var/home/cadence/Music/${file}`)
|
|
|| fs.existsSync(`/var/home/cadence/Music/${artist}/${file}.mp3`)
|
|
const saleID = `${item.sale_item_type}${item.sale_item_id}`
|
|
const url = collection.redownload_urls[saleID]
|
|
return {
|
|
subdomain: item.url_hints.subdomain,
|
|
exists: exists ? "✅ " : "",
|
|
file: file,
|
|
url
|
|
}
|
|
})
|
|
|
|
table.sort((a, b) => {
|
|
if (a.file < b.file) return -1
|
|
else if (a.file === b.file) return 0
|
|
else return 1
|
|
})
|
|
|
|
const labels = Object.groupBy(table, x => x.subdomain)
|
|
|
|
const file = process.cwd() + "/download-selector.html"
|
|
const o = fs.createWriteStream(file, "utf8")
|
|
o.write(tag`<style>ul { list-style-type: none; }</style>`)
|
|
|
|
o.write(tag`<ul>`)
|
|
for (const [label, items] of Object.entries(labels).sort(([a], [b]) => {
|
|
if (a < b) return -1
|
|
else if (a === b) return 0
|
|
else return 1
|
|
})) {
|
|
const labelCompleted = items?.every(item => item.exists) ? "✅ " : ""
|
|
o.write(tag`<li style='list-style-type: "${labelCompleted}"'><details><summary>${label}</summary><ul>`)
|
|
if (items) {
|
|
for (const item of items) {
|
|
o.write(tag`<li style='list-style-type: "${item.exists}"'><a href="${item.url}">${item.file}</a></li>`)
|
|
}
|
|
}
|
|
o.write(tag`</ul></details></li>`)
|
|
}
|
|
o.write(tag`</ul>`)
|
|
|
|
o.close()
|
|
|
|
console.log(`Wrote: file://${file}`)
|