Last active
January 1, 2024 23:11
-
-
Save mojoaxel/f6f0330226d8fcd1b407b609f6b5049f to your computer and use it in GitHub Desktop.
node.js script to download Bibi Blocksberg Audiobook covers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/node | |
/** | |
* This script downloads audiobook covers for the "Bibi Blocksberg" audio-play series. | |
* | |
* @example ``` | |
* node getBibiBlocksbergCovers.js | |
* ``` | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
const https = require('https'); | |
const MAX_EPISODE_NUMBER = 148; | |
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"); | |
/** | |
* The official page "bibiblocksberg.de" only works for the first 116 episodes | |
* because after that the naming scheme changes :-( | |
* Also episode 068 has a different name. | |
*/ | |
//const url = `https://www.bibiblocksberg.de/sites/default/files/styles/detail_episode_image_desktop/public/hoerspiele/ep_image/Bibi_Blocksberg_${numberFormatted}.jpg`; | |
const url = `https://www.hoerspiel-paradies.de/images/cover/kid/bib/hsp/4/${numberFormatted}.jpg`; | |
const filename = `Bibi_Blocksberg_${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