Skip to content

Instantly share code, notes, and snippets.

@mojoaxel
Created January 1, 2024 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojoaxel/08a50b4c785bd9af344fbabeceedacf0 to your computer and use it in GitHub Desktop.
Save mojoaxel/08a50b4c785bd9af344fbabeceedacf0 to your computer and use it in GitHub Desktop.
node.js script to download cover art for the audio series "Die Drei Fragezeichen"
#!/usr/bin/node
/**
* This script downloads audiobook covers for the "Die Drei Fragezeichen" audio-play series.
*
* @example ```
* node getDieDreiFragezeichenCovers.js
* ```
*/
const fs = require('fs');
const path = require('path');
const https = require('https');
const MAX_EPISODE_NUMBER = 224;
const TARGET_FOLDER = './covers';
async function downloadFile(url, filename) {
return new Promise((resolve, reject) => {
fs.access(filename, fs.constants.F_OK, (err) => {
if (err) {
// file does not exist
process.stdout.write(`Downloading "${filename}"...`);
https.get(url, (res) => {
if (res.statusCode !== 200) {
console.log(`Status Code: ${res.statusCode}`)
return reject();
}
const fileStream = fs.createWriteStream(filename);
res.pipe(fileStream);
fileStream.on("finish", () => {
fileStream.close();
console.log('Done')
return resolve();
});
});
} else {
// file exists
console.log(`File "${filename}" already exists`);
resolve();
}
});
});
}
async function getCover(number) {
const numberFormatted = number.toString().padStart(3, "0");
const url = `https://www.hoerspiel-paradies.de/images/cover/eur/ddf/hsp/4/${numberFormatted}.jpg`;
const filename = `Die_Drei_Fragezeichen_${numberFormatted}.jpg`;
const targetFile = path.join(TARGET_FOLDER, filename);
await downloadFile(url, targetFile);
}
(async function () {
// create target folder if it does not exist
fs.mkdirSync(TARGET_FOLDER, { recursive: true });
// download all covers
const allEpisodes = Array.from({length: MAX_EPISODE_NUMBER}, (_, i) => i + 1);
const errors = [];
for await (const num of allEpisodes) {
try {
await getCover(num);
} catch (error) {
errors.push(num);
}
}
// show errors if there are any
if (errors.length > 0) {
console.log(`Could not download covers for episodes: ${errors.join(', ')}`);
} else {
console.log('Done!');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment