Created
July 11, 2020 09:21
-
-
Save tjohnman/c54b30052a3bf1e0bd8aba724eb05d85 to your computer and use it in GitHub Desktop.
Quick and dirty Node script for downloading and packaging CBZ archives from Mangazuki.
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
const child_process = require('child_process'); | |
const https = require('https'); | |
const fs = require('fs'); | |
function get(url) { | |
console.log('Get ' + url); | |
return new Promise((resolve, reject) => { | |
const req = https.request(url, { | |
headers: { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0' | |
} | |
}, async res => { | |
if(res.statusCode == 301 || res.statusCode == 302) { | |
console.log('Redirected to ' + res.headers.location); | |
const result = await get(res.headers.location); | |
resolve(result); | |
return; | |
} | |
let data = new Buffer.from(''); | |
res.on('data', d => { | |
data = Buffer.concat([ data, d ]); | |
}); | |
res.on('end', d => { | |
resolve(data); | |
}); | |
res.on('error', error => { | |
console.error(error); | |
reject(error); | |
}); | |
}); | |
req.on('error', error => reject(error)); | |
req.end(); | |
}); | |
} | |
const manga_slug = process.argv[2]; | |
const manga_url = 'https://mangazuki.me/manga/' + manga_slug + '/'; | |
get(manga_url).then(async data => { | |
const regexp = new RegExp('class="wp-manga-chapter.+?href="(.+?)"', 'igms'); | |
let m; | |
while((m = regexp.exec(data)) != null) { | |
if(m.index === regexp.lastIndex) { | |
regexp.lastIndex++; | |
} | |
const ch_data = await get(m[1]); | |
const chapter_number = m[1].match(/-chapter(\d+)/im)[1]; | |
console.log('Chapter ' + chapter_number); | |
const ch_regexp = /id="image-.+?".+?src="(.+?)"/igm; | |
let ch_m; | |
while((ch_m = ch_regexp.exec(ch_data)) != null) { | |
if(ch_m.index == ch_regexp.lastIndex) ch_regexp.lastIndex++; | |
const image_name = ch_m[1].match(/.+\/([^\/]+)$/ims)[1]; | |
const image_data = await get(ch_m[1]); | |
if(!fileExists(manga_slug + '/' + chapter_number + '/' + image_name)) { | |
saveImage(image_data, chapter_number, image_name); | |
} | |
} | |
child_process.execSync('zip -r -y ' + manga_slug + '/' + manga_slug + '-' + chapter_number + '.cbz ' + manga_slug + '/' + chapter_number); | |
child_process.execSync('rm -rf ' + manga_slug + '/' + chapter_number); | |
} | |
}); | |
function saveImage(data, chapter, image) { | |
const path = manga_slug + '/' + chapter + '/' + image; | |
createDirectory(manga_slug); | |
createDirectory(manga_slug + '/' + chapter); | |
fs.writeFileSync(path, data); | |
} | |
function fileExists(path) { | |
try { | |
fs.accessSync(path); | |
} catch(err) { | |
return false; | |
} | |
return true; | |
} | |
function createDirectory(path) { | |
if(!fileExists(path)) { | |
fs.mkdirSync(path); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment