Skip to content

Instantly share code, notes, and snippets.

@fallen90
Last active June 17, 2021 17:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fallen90/8d4e7fadd723995d27e3e8350199d29a to your computer and use it in GitHub Desktop.
Save fallen90/8d4e7fadd723995d27e3e8350199d29a to your computer and use it in GitHub Desktop.
Download books in m.wuxiaworld.co, not to be confused with the official Wuxiaworld.
/**
* Fetches book contents from m.wuxiaworld.co,
* and prepare it for Calibre import.
*
* To create great eBooks with Calibre, do the ff:
* - Open Calibre
* - Ctrl-Shift-E or click `Add books` Dropdown and select `Add Empty Book`
* - Set Details to your book, and select AZW3 (for kindle) or ePub, I set it to AZW3 for my Paperwhite
* - Right-click the book you just created and click `Edit book`
* - In the File Browser panel, remove `part0000.html` in the Text node.
* - Click `File` -> `Import files into book` -> select all the files inside build folder, just wait for it to update.
* - Click `Tools` -> `Table of Contents` -> `Edit ...`
* - Click `Generate TOC from all Headings`, then `Ok`
* - Save book, then on the list of books, right click the book then send to device.
**/
const cheerio = require('cheerio');
const req = require('req-fast');
const fs = require('fs');
const mkdirp = require('mkdirp');
const baseFolder = 'only-i-level-up';
const buildLocation = './builds/' + baseFolder;
const basePage = 'https://m.wuxiaworld.co/Only-I-level-up/';
const indexPage = basePage + '/all.html';
mkdirp(buildLocation);
const fetchHtml = (url) => {
return new Promise((resolve) => {
req(url, function(err, resp){
if(err) resolve({ status : 'error', error : err });
else resolve({ status : 'success', data : resp.body });
});
});
}
const chapterList = [];
/** build TOC */
(async () => {
const body = await fetchHtml(indexPage);
if(body.status === 'success'){
const $ = cheerio.load(body.data);
$('head').empty();
$('body')
.find('#chapterlist p > a')
.each(function(index, element){
if($(element).attr('href') !== '#bottom'){
const title = $(element).text();
const href = $(element).attr('href');
chapterList.push({ title, href });
}
});
/** download chaper list */
chapterList.forEach(async (chapter, index) => {
const body = await fetchHtml(basePage + chapter.href);
if(body.status === 'success'){
const $ = cheerio.load(body.data);
$('head').empty();
$('script').remove();
$('amp-auto-ads').remove();
$('.adsbygoogle').remove();
const content = $('#chaptercontent').html();
$('body')
.html(`<h1>${chapter.title}</h1>`)
.append(content);
// this would create a file with part0001.html-> part000N.html that can be
// used for building ASW3 books in calibre.
const partName = `part${("" + (index + 1)).padStart(4, '0')}.html`;
fs.writeFileSync(buildLocation + `/${partName}`, $.html());
}
})
} else {
console.log('ERROR', body);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment