Skip to content

Instantly share code, notes, and snippets.

@Davenchy
Last active February 25, 2019 10:20
Show Gist options
  • Save Davenchy/55ef79d7734baf67a1d6faa73faae673 to your computer and use it in GitHub Desktop.
Save Davenchy/55ef79d7734baf67a1d6faa73faae673 to your computer and use it in GitHub Desktop.
simple script to download stories from that website http://manga.alphantom.com
/*
* by Davenchy
* you need to modify the code to download your story
*/
const http = require('http')
const fs = require('fs')
const path = require('path')
// get the page url
const getURL = (c, p, isJPG) => `http://manga.alphantom.com/uploads/manga/ao-haru-ride/chapters/${c}/${fixPageNumber(p)}.${isJPG ? 'jpg' : 'png'}`;
// add zeros before the number if less than 10
const fixPageNumber = p => (p < 10 && p > 0 ? `0${p}` : p).toString();
// sometimes you will need a fixed addition even if less than 10 or not
// const fixPageNumber = p => `0${p}`
// get response object if the image exist in jpg or png ext.
function getRes (c, p) {
return new Promise((resolve, reject) => {
const jpg = getURL(c, p, true)
const png = getURL(c, p, false)
http.get(jpg, (res) => { if (res.statusCode === 200) {
return resolve(res, true)
} else {
http.get(png, (res) => {
if (res.statusCode === 200) {
return resolve(res, false)
} else { reject() }
})
} })
})
}
// download image p from chapter c
function download(c, p) {
return new Promise((resolve, reject) => {
getRes(c, p).then((res, isJPG) => {
const downloads = path.join(__dirname, 'downloads')
const dir = path.join(downloads, c.toString())
const name = path.join(dir, p.toString() + (isJPG ? '.jpg' : '.png'))
if (!fs.existsSync(downloads)) fs.mkdirSync(downloads)
if (!fs.existsSync(dir)) fs.mkdirSync(dir)
const file = fs.createWriteStream(name)
res.pipe(file)
file.on('finish', () => resolve())
}).catch(() => reject())
})
}
// start downloading
// start from chapter a
// ent after chapter b
// mp is the maximum number of pages to download
async function start (a=0, b=0, mp=100) {
for (let i = a - 1; i < b; i++) {
const c = i + 1
for (let p = 0; p < mp; p++) {
try {
await download(c, p + 1)
console.log('downloaded: ', c, ':', p + 1)
} catch (_) {
console.log('failed to download: ', c, ':', p + 1)
}
}
}
}
// start downloading chapter 5 only
(async function () { await start(5, 5) })()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment