From a4e5a12fe4b8eb4ceb858698c5795a339b96341f Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Thu, 7 May 2015 23:17:12 +0200 Subject: [PATCH] Use sortable filenames, and fix output filenames --- lib/download.js | 21 ++++++--------------- lib/worm-scraper.js | 15 +++++++++------ package.json | 3 ++- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/download.js b/lib/download.js index 30fa7ff..3664092 100644 --- a/lib/download.js +++ b/lib/download.js @@ -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]); } diff --git a/lib/worm-scraper.js b/lib/worm-scraper.js index 6fb45f3..44b904a 100644 --- a/lib/worm-scraper.js +++ b/lib/worm-scraper.js @@ -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}`); }); } diff --git a/package.json b/package.json index e92f975..903f4e3 100644 --- a/package.json +++ b/package.json @@ -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",