Use sortable filenames, and fix output filenames
This commit is contained in:
parent
a820bc517b
commit
a4e5a12fe4
3 changed files with 17 additions and 22 deletions
|
|
@ -3,6 +3,7 @@ const path = require("path");
|
|||
const fs = require("mz/fs");
|
||||
const mkdirp = require("mkdirp-then");
|
||||
const request = require("requisition");
|
||||
const zfill = require("zfill");
|
||||
const jsdom = require("./jsdom.js");
|
||||
|
||||
const FILENAME_PREFIX = "chapter";
|
||||
|
|
@ -11,19 +12,9 @@ const URL_SUFFIX = "-url.txt";
|
|||
module.exports = function (startChapterUrl, cachePath) {
|
||||
return fs.readdir(cachePath).then(
|
||||
function (filenames) {
|
||||
const lastChapter = filenames.filter(function (f) {
|
||||
return f.endsWith(URL_SUFFIX);
|
||||
})
|
||||
.map(function (f) {
|
||||
return [getChapterNumber(f), f];
|
||||
})
|
||||
.sort(function (a, b) {
|
||||
return b[0] - a[0];
|
||||
})
|
||||
[0];
|
||||
|
||||
const lastChapterNumber = lastChapter[0];
|
||||
const lastChapterUrlFile = lastChapter[1];
|
||||
const sorted = filenames.filter(function (f) { return f.endsWith(URL_SUFFIX); }).sort();
|
||||
const lastChapterUrlFile = sorted[sorted.length - 1];
|
||||
const lastChapterNumber = getChapterNumber(lastChapterUrlFile);
|
||||
|
||||
return fs.readFile(path.resolve(cachePath, lastChapterUrlFile), { encoding: "utf-8" }).then(function (url) {
|
||||
return downloadAllChapters(url, lastChapterNumber, cachePath);
|
||||
|
|
@ -46,7 +37,7 @@ function downloadAllChapters(startChapterUrl, startChapterNumber, cachePath) {
|
|||
return mkdirp(cachePath).then(loop);
|
||||
|
||||
function loop() {
|
||||
const filename = `${FILENAME_PREFIX}${chapterCounter}.html`;
|
||||
const filename = `${FILENAME_PREFIX}${zfill(chapterCounter, 3)}.html`;
|
||||
|
||||
console.log(`Downloading ${currentChapter}`);
|
||||
|
||||
|
|
@ -117,5 +108,5 @@ function urlFilename(filename) {
|
|||
}
|
||||
|
||||
function getChapterNumber(filename) {
|
||||
return Number(filename.substring(FILENAME_PREFIX.length, filename.indexOf(".html")));
|
||||
return Number(/0?0?(\d+)\./.exec(filename)[1]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,21 +38,24 @@ function getChapterFilePaths() {
|
|||
});
|
||||
}
|
||||
|
||||
function convertChapter(filePath, i) {
|
||||
console.log(`- Reading ${filePath}`);
|
||||
function convertChapter(filePath) {
|
||||
const filename = path.basename(filePath);
|
||||
|
||||
console.log(`- Reading ${filename}`);
|
||||
return fs.readFile(filePath, { encoding: "utf-8" }).then(function (contents) {
|
||||
console.log(`- Read ${filePath}`);
|
||||
console.log(`- Read ${filename}`);
|
||||
const rawChapterDoc = jsdom(contents);
|
||||
const output = getChapterString(rawChapterDoc);
|
||||
|
||||
// TODO: this should probably not be necessary... jsdom bug I guess!?
|
||||
rawChapterDoc.defaultView.close();
|
||||
|
||||
const destFilename = path.resolve(contentPath, `chapter${i + 1}.xhtml`);
|
||||
return fs.writeFile(destFilename, output);
|
||||
const destFileName = `${path.basename(filename, ".html")}.xhtml`;
|
||||
const destFilePath = path.resolve(contentPath, destFileName);
|
||||
return fs.writeFile(destFilePath, output);
|
||||
})
|
||||
.then(function () {
|
||||
console.log(`- Finished converting ${filePath}`);
|
||||
console.log(`- Finished converting ${filename}`);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,8 @@
|
|||
"mkdirp-then": "^1.0.1",
|
||||
"requisition": "^1.5.0",
|
||||
"rimraf-then": "^1.0.0",
|
||||
"xtend": "^4.0.0"
|
||||
"xtend": "^4.0.0",
|
||||
"zfill": "0.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "0.20.0",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue