forked from AABoyles/LessWrong-Portable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·187 lines (169 loc) · 5.31 KB
/
build.js
File metadata and controls
executable file
·187 lines (169 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env nodejs
const cheerio = require('cheerio');
const makepub = require('nodepub');
const jetpack = require('fs-jetpack');
const { request } = require('graphql-request')
const { execSync } = require('child_process');
var sanitizeHtml = require('sanitize-html');
var version = process.argv.length > 2 ? process.argv[2] : 'default';
const config = JSON.parse(jetpack.read('meta/' + version + '.json'));
var scrapeError = false;
var epub = makepub.document(config.metadata, config.img);
epub.addCSS(jetpack.read('style/base.css'));
epub.addSection('Title Page', "<h1>[[TITLE]]</h1><h3>by [[AUTHOR]]</h3>", true, true);
var base_content = jetpack.read('template.xhtml');
function addChapterToBook(html, url, cache_path){
let $ = cheerio.load(html);
let title = $(config.titleSelector).first().text();
let content = $(config.contentSelector).html();
if(config.withoutSelector) $(config.withoutSelector).remove();
let path = url;
if(typeof url === 'object'){
path = url.url;
if(url.titleSelector) title = $(url.titleSelector).text();
if(url.contentSelector) content = $(url.contentSelector).text();
}
if(title === ''){
console.log('Couldn\'t correctly scrape', path);
jetpack.remove(cache_path);
scrapeError = true;
}
let safe_title = title.toLowerCase().replace(/ /g, '-');
let newDoc = cheerio.load(base_content);
newDoc('body').append('<div id="'+safe_title+'"></div>');
newDoc('div').append('<h1>'+title+'</h1>').append(content);
epub.addSection(title, newDoc('body').html());
}
function buildEbookFromCollection(collection) {
collection.books.forEach((book, bookCount) => {
const bookDoc = createDocFromLWContents(book.title || bookCount, book)
book.posts && book.posts.forEach((post, bookPostCount) => {
createDocFromLWContents(post.title || `${bookCount}.posts.${bookPostCount}`, post)
})
book.sequences && book.sequences.forEach(sequence => {
buildEbookFromSequence(sequence)
})
})
}
function buildEbookFromSequence(sequence) {
createDocFromLWContents(sequence.title, sequence)
sequence.chapters && sequence.chapters.forEach((chapter) => {
if (chapter.title) {
createDocFromLWContents(chapter.title, chapter)
}
chapter.posts && chapter.posts.forEach((post, postCount) => {
createDocFromLWContents(post.title, post)
})
})
}
function createDocFromLWContents(title, content) {
let newDoc = cheerio.load(base_content)
if (content.title) {
let safe_title = content.title.toLowerCase().replace(/ /g, '-');
newDoc('body').append('<div id="'+safe_title+'"></div>');
newDoc('div').append('<h1>'+content.title+'</h1>')
}
newDoc('div').append(content.contents.html)
epub.addSection(title, sanitizeHtml(newDoc('body').html(), {parser: {xmlMode: true}}))
}
if (config.urls) {
config.urls.forEach(url => {
if(typeof url === 'string'){
path = url;
} else {
path = url.url;
}
let stem = path.trim().split('/').pop();
cache_path = './cache/' + stem + (stem.split('.').pop() !== 'html' ? '.html' : '');
if(!jetpack.exists(cache_path)){
console.log('Scraping', config.metadata.source + path);
execSync('wget ' + config.metadata.source + path + ' -nc -q -O ' + cache_path);
}
addChapterToBook(jetpack.read(cache_path), url, cache_path);
});
if(scrapeError){
console.log('Scrape errors occurred: No book produced.');
} else {
epub.writeEPUB(console.error, 'output', config.shorttitle, ()=>{
console.log('Book successfully written to output/' + config.shorttitle + '.epub');
});
}
} else if (config.collectionId) {
const query = `{
collection(input: {selector: {_id: "${config.collectionId}"}}) {
result {
books {
title
contents {
html
}
sequences {
title
contents {
html
}
_id
chapters {
title
contents {
html
}
posts {
title
contents {
html
}
_id
}
}
}
}
}
}
}`
request('https://www.lessestwrong.com/graphql?', query)
.then(data => {
buildEbookFromCollection(data.collection.result)
epub.writeEPUB(console.error, 'output', config.shorttitle, ()=>{
console.log('Book successfully written to output/' + config.shorttitle + '.epub');
});
})
.catch(err => {
console.log(err)
})
} else if (config.sequenceId) {
const query = `{
sequence(input: {selector: {_id: "${config.sequenceId}"}}) {
result {
title
contents {
html
}
chapters {
title
contents {
html
}
posts {
title
contents {
html
}
_id
}
}
}
}
}
`
request('https://www.lessestwrong.com/graphql?', query)
.then(data => {
buildEbookFromSequence(data.sequence.result)
epub.writeEPUB(console.error, 'output', config.shorttitle, ()=>{
console.log('Book successfully written to output/' + config.shorttitle + '.epub');
});
})
.catch(err => {
console.log(err)
})
}