Add directory to store the tracks in the zip

This commit is contained in:
Fabien O 2025-08-03 08:43:08 +02:00
parent 5ef0a1c22d
commit 2185c21101

View file

@ -187,13 +187,14 @@ const promptQuality = function () {
* @param {ZipWriter} zipWriter
* @param {string} strUrl URL for the image or track
* @param {string} filename Name of the file to add
* @param {string} dirName Name of the directory to store the image or track
*/
const addUrlToZip = async function (zipWriter, strUrl, filename) {
const addUrlToZip = async function (zipWriter, strUrl, filename, dirName) {
await fetch(strUrl)
.then(async function (res) { return await res.blob();})
.then(async function (blob) {
const reader = new zip.BlobReader(blob);
await zipWriter.add(filename, reader);
await zipWriter.add(`${dirName}/${filename}`, reader);
});
}
@ -205,13 +206,14 @@ const addUrlToZip = async function (zipWriter, strUrl, filename) {
* @param {Element} link Anchor to the track
* @param {string} filename Name of the file to add
* @param {string} q The format type of the tracks (mp3)
* @param {string} dirName Name of the directory to store the tracks
*/
const addTrackToZip = async function (zipWriter, link, filename, q) {
const addTrackToZip = async function (zipWriter, link, filename, q, dirName) {
const qualityCode = arrayQuality[q];
await fetch(link.dataset.uri.replace('/xx', `/${qualityCode}`))
.then(function (res) { return res.json(); })
.then(async function (json) {
await addUrlToZip(zipWriter, json.url, `${filename}.${q}`);
await addUrlToZip(zipWriter, json.url, `${filename}.${q}`, dirName);
});
}
@ -222,16 +224,15 @@ const addTrackToZip = async function (zipWriter, link, filename, q) {
* You need to call addTrackToZip() or addUrlToZip() before using it.
*
* @param {BlobWriter} zipFileWriter Contain all tracks and images
* @param {string} nameArtist Name of the artist
* @param {string} nameAlbum Name of the album
* @param {string} zipName The name of the zip file
*/
const generateZip = async function (zipFileWriter, nameArtist, nameAlbum) {
const generateZip = async function (zipFileWriter, zipName) {
const zipFileBlob = await zipFileWriter.getData();
const blobURL = window.URL.createObjectURL(zipFileBlob);
const link = document.createElement('a');
link.href = blobURL;
link.download = `${nameArtist}-${nameAlbum}.zip`;
link.download = `${zipName}.zip`;
link.click();
}
@ -277,13 +278,23 @@ const qoAllDown = async function () {
const zipFileWriter = new zip.BlobWriter();
const zipWriter = new zip.ZipWriter(zipFileWriter);
// Fetch the name of artist and album
const productTitleContainer = document.querySelector('.product-title-container');
let nameArtist = productTitleContainer.querySelector('a:nth-child(2)').textContent.trim();
let nameAlbum = productTitleContainer.querySelector('a:nth-child(1) strong').textContent;
nameArtist = nameArtist.replaceAll(' ', '_');
nameAlbum = nameAlbum.replaceAll(' ', '_');
const dirName = `${nameArtist}-${nameAlbum}`;
// Download and add the album cover to the archive
const allCoverButton = document.querySelectorAll('a[href^="/account/download/cover/"]');
for (button of allCoverButton) {
displayString(trans('downloading_cover'));
await addUrlToZip(zipWriter, button.href, 'cover.jpeg');
await addUrlToZip(zipWriter, button.href, 'cover.jpeg', dirName);
}
// Download and add the tracks to the archive
@ -297,7 +308,7 @@ const qoAllDown = async function () {
i = addZero(i, nbTotalTracks);
const filename = sanitizeFilename(`${i} - ${title}`);
displayString(trans('downloading_track') + `: ${filename}.${q}`);
await addTrackToZip(zipWriter, button, filename, q);
await addTrackToZip(zipWriter, button, filename, q, dirName);
}
await zipWriter.close();
@ -306,14 +317,7 @@ const qoAllDown = async function () {
displayString(trans('generating_zip'));
const productTitleContainer = document.querySelector('.product-title-container');
let nameArtist = productTitleContainer.querySelector('a:nth-child(2)').textContent.trim();
let nameAlbum = productTitleContainer.querySelector('a:nth-child(1) strong').textContent;
nameArtist = nameArtist.replaceAll(' ', '_');
nameAlbum = nameAlbum.replaceAll(' ', '_');
await generateZip(zipFileWriter, nameArtist, nameAlbum);
await generateZip(zipFileWriter, dirName);
displayString(trans('all_done'));
return 0;