Restructure to be a bit more efficient

This commit is contained in:
Domenic Denicola 2015-05-06 01:15:47 +02:00
commit 45b5b9d6fd
2 changed files with 72 additions and 47 deletions

View file

@ -2,26 +2,25 @@
const path = require("path"); 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 rimraf = require("rimraf-then");
const request = require("requisition"); const request = require("requisition");
const jsdom = require("jsdom"); const jsdom = require("jsdom");
require("./track-rejections.js"); require("./track-rejections.js");
const START_CHAPTER = "http://parahumans.wordpress.com/category/stories-arcs-1-10/arc-1-gestation/1-01/"; const START_CHAPTER = "https://parahumans.wordpress.com/2011/06/11/1-1/";
const cachePath = path.resolve("cache"); const cachePath = path.resolve("cache");
const outPath = path.resolve("out"); const outPath = path.resolve("out");
const contentPath = path.resolve(outPath, "OEBPS"); const contentPath = path.resolve(outPath, "OEBPS");
let chapters; const rawChapterDocsCache = new Map();
getChapters()
.then(function (theChapters) { rimraf(outPath)
chapters = theChapters; .then(function () {
return mkdirp(contentPath); return mkdirp(contentPath);
}) })
.then(function () { .then(getAllRawChapterDocs)
return Promise.all(chapters.map(getRawChapterDoc));
})
.then(function (rawChapterDocs) { .then(function (rawChapterDocs) {
console.log("Extracting content into EPUB chapter files"); console.log("Extracting content into EPUB chapter files");
return Promise.all(rawChapterDocs.map(function (rawChapterDoc, i) { return Promise.all(rawChapterDocs.map(function (rawChapterDoc, i) {
@ -34,15 +33,27 @@ getChapters()
console.log("All done!"); console.log("All done!");
}); });
function getChapters() { function getAllRawChapterDocs() {
return fs.readdir(cachePath).catch(function (e) { return fs.readdir(cachePath).then(
function (filenames) {
return filenames.map(getRawChapterDoc);
},
function (e) {
if (e.code === "ENOENT") { if (e.code === "ENOENT") {
return downloadAllChapters();
} else {
throw e;
}
}
);
}
function downloadAllChapters() {
let currentChapter = START_CHAPTER; let currentChapter = START_CHAPTER;
let chapterCounter = 1; let chapterCounter = 1;
const filenames = [];
return loop().then(function () { return mkdirp(cachePath).then(loop).then(function () {
return filenames; return toArray(rawChapterDocsCache.values());
}); });
function loop() { function loop() {
@ -51,17 +62,15 @@ function getChapters() {
console.log(`Downloading ${currentChapter}`); console.log(`Downloading ${currentChapter}`);
return request(currentChapter).redirects(10).then(function (response) { return request(currentChapter).redirects(10).then(function (response) {
return response.saveTo(path.resolve(cachePath, filename)); return response.text();
}) })
.then(function () { .then(function (contents) {
filenames.push(filename); const rawChapterDoc = setRawChapterDoc(filename, contents);
// This is inefficient in a number of ways; whatever.
return getRawChapterDoc(filename);
})
.then(function (rawChapterDoc) {
currentChapter = getNextChapterUrl(rawChapterDoc); currentChapter = getNextChapterUrl(rawChapterDoc);
return fs.writeFile(path.resolve(cachePath, filename), jsdom.serializeDocument(rawChapterDoc));
})
.then(function () {
if (currentChapter === null) { if (currentChapter === null) {
return; return;
} }
@ -70,18 +79,25 @@ function getChapters() {
return loop(); return loop();
}); });
} }
} else {
throw e;
}
});
} }
function getRawChapterDoc(filename) { function getRawChapterDoc(filename) {
const doc = rawChapterDocsCache.get(filename);
if (doc !== undefined) {
return Promise.resolve(doc);
}
return fs.readFile(path.resolve(cachePath, filename), { encoding: "utf-8" }).then(function (contents) { return fs.readFile(path.resolve(cachePath, filename), { encoding: "utf-8" }).then(function (contents) {
return jsdom.jsdom(contents); return setRawChapterDoc(filename, contents);
}); });
} }
function setRawChapterDoc(filename, contents) {
const doc = jsdom.jsdom(contents);
rawChapterDocsCache.set(filename, doc);
return doc;
}
function getChapterString(rawChapterDoc) { function getChapterString(rawChapterDoc) {
const title = rawChapterDoc.querySelector("h1.entry-title").textContent; const title = rawChapterDoc.querySelector("h1.entry-title").textContent;
const body = cleanContentEl(rawChapterDoc.querySelector(".entry-content")).innerHTML; const body = cleanContentEl(rawChapterDoc.querySelector(".entry-content")).innerHTML;
@ -129,3 +145,11 @@ function getNextChapterUrl(rawChapterDoc) {
return null; return null;
} }
function toArray(iterable) {
const array = [];
for (const x of iterable) {
array.push(x);
}
return array;
}

View file

@ -21,7 +21,8 @@
"dependencies": { "dependencies": {
"jsdom": "^5.3.0", "jsdom": "^5.3.0",
"mkdirp-then": "^1.0.1", "mkdirp-then": "^1.0.1",
"requisition": "^1.5.0" "requisition": "^1.5.0",
"rimraf-then": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"eslint": "0.20.0", "eslint": "0.20.0",