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 fs = require("mz/fs");
|
||||||
const mkdirp = require("mkdirp-then");
|
const mkdirp = require("mkdirp-then");
|
||||||
const request = require("requisition");
|
const request = require("requisition");
|
||||||
|
const zfill = require("zfill");
|
||||||
const jsdom = require("./jsdom.js");
|
const jsdom = require("./jsdom.js");
|
||||||
|
|
||||||
const FILENAME_PREFIX = "chapter";
|
const FILENAME_PREFIX = "chapter";
|
||||||
|
|
@ -11,19 +12,9 @@ const URL_SUFFIX = "-url.txt";
|
||||||
module.exports = function (startChapterUrl, cachePath) {
|
module.exports = function (startChapterUrl, cachePath) {
|
||||||
return fs.readdir(cachePath).then(
|
return fs.readdir(cachePath).then(
|
||||||
function (filenames) {
|
function (filenames) {
|
||||||
const lastChapter = filenames.filter(function (f) {
|
const sorted = filenames.filter(function (f) { return f.endsWith(URL_SUFFIX); }).sort();
|
||||||
return f.endsWith(URL_SUFFIX);
|
const lastChapterUrlFile = sorted[sorted.length - 1];
|
||||||
})
|
const lastChapterNumber = getChapterNumber(lastChapterUrlFile);
|
||||||
.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];
|
|
||||||
|
|
||||||
return fs.readFile(path.resolve(cachePath, lastChapterUrlFile), { encoding: "utf-8" }).then(function (url) {
|
return fs.readFile(path.resolve(cachePath, lastChapterUrlFile), { encoding: "utf-8" }).then(function (url) {
|
||||||
return downloadAllChapters(url, lastChapterNumber, cachePath);
|
return downloadAllChapters(url, lastChapterNumber, cachePath);
|
||||||
|
|
@ -46,7 +37,7 @@ function downloadAllChapters(startChapterUrl, startChapterNumber, cachePath) {
|
||||||
return mkdirp(cachePath).then(loop);
|
return mkdirp(cachePath).then(loop);
|
||||||
|
|
||||||
function loop() {
|
function loop() {
|
||||||
const filename = `${FILENAME_PREFIX}${chapterCounter}.html`;
|
const filename = `${FILENAME_PREFIX}${zfill(chapterCounter, 3)}.html`;
|
||||||
|
|
||||||
console.log(`Downloading ${currentChapter}`);
|
console.log(`Downloading ${currentChapter}`);
|
||||||
|
|
||||||
|
|
@ -117,5 +108,5 @@ function urlFilename(filename) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getChapterNumber(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) {
|
function convertChapter(filePath) {
|
||||||
console.log(`- Reading ${filePath}`);
|
const filename = path.basename(filePath);
|
||||||
|
|
||||||
|
console.log(`- Reading ${filename}`);
|
||||||
return fs.readFile(filePath, { encoding: "utf-8" }).then(function (contents) {
|
return fs.readFile(filePath, { encoding: "utf-8" }).then(function (contents) {
|
||||||
console.log(`- Read ${filePath}`);
|
console.log(`- Read ${filename}`);
|
||||||
const rawChapterDoc = jsdom(contents);
|
const rawChapterDoc = jsdom(contents);
|
||||||
const output = getChapterString(rawChapterDoc);
|
const output = getChapterString(rawChapterDoc);
|
||||||
|
|
||||||
// TODO: this should probably not be necessary... jsdom bug I guess!?
|
// TODO: this should probably not be necessary... jsdom bug I guess!?
|
||||||
rawChapterDoc.defaultView.close();
|
rawChapterDoc.defaultView.close();
|
||||||
|
|
||||||
const destFilename = path.resolve(contentPath, `chapter${i + 1}.xhtml`);
|
const destFileName = `${path.basename(filename, ".html")}.xhtml`;
|
||||||
return fs.writeFile(destFilename, output);
|
const destFilePath = path.resolve(contentPath, destFileName);
|
||||||
|
return fs.writeFile(destFilePath, output);
|
||||||
})
|
})
|
||||||
.then(function () {
|
.then(function () {
|
||||||
console.log(`- Finished converting ${filePath}`);
|
console.log(`- Finished converting ${filename}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,8 @@
|
||||||
"mkdirp-then": "^1.0.1",
|
"mkdirp-then": "^1.0.1",
|
||||||
"requisition": "^1.5.0",
|
"requisition": "^1.5.0",
|
||||||
"rimraf-then": "^1.0.0",
|
"rimraf-then": "^1.0.0",
|
||||||
"xtend": "^4.0.0"
|
"xtend": "^4.0.0",
|
||||||
|
"zfill": "0.0.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "0.20.0",
|
"eslint": "0.20.0",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue