-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
223 lines (193 loc) · 6.86 KB
/
index.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const IO = require("fs-extra");
const PDFMerger = require("pdf-merger-js");
const Throttle = require("promise-parallel-throttle");
const Puppeteer = require("puppeteer");
const { keepTemp, maxInProgress, maxRetries, minBytes, outputPath, overwrite } = require("./config.json");
async function newPage(browser, cookies) {
let page = await browser.newPage();
await page.setJavaScriptEnabled(true);
await page.setDefaultNavigationTimeout(90000);
await page.setCookie(...cookies.map(c => ({ "name": c.name, "value": c.value, "domain": c.domain, "path": c.path })));
if (page.emulateMedia) {
await page.emulateMedia("print");
} else if (page.emulateMediaType) {
await page.emulateMediaType("print");
}
return page;
}
async function ensureGoTo(page, url, retries = 0) {
let retry = retries;
let response = await page.goto(url, { "waitUntil": "networkidle0" }).catch(() => false);
while (response && response.status() !== 200 && retry < maxRetries) {
if (page.waitForTimeout) {
await page.waitForTimeout(10000);
} else {
await page.wait(10000);
}
retry++;
response = await page.reload().catch(() => false);
}
if (!response && retry < maxRetries) {
if (page.waitForTimeout) {
await page.waitForTimeout(10000);
} else {
await page.wait(10000);
}
let newPage = await ensureGoTo(page, url, ++retry);
return newPage;
}
return retry < maxRetries ? page : false;
}
async function ensurePDFSize(page, path, height, width) {
if (page.waitForTimeout) {
await page.waitForTimeout(1000);
} else {
await page.wait(1000);
}
await page.pdf({ path, height, width, "printBackground": true });
let retries = 0;
let { size } = await IO.stat(path);
while (size < minBytes && retries < maxRetries) {
if (page.waitForTimeout) {
await page.waitForTimeout(1000);
} else {
await page.wait(1000);
}
await page.pdf({ path, height, width, "printBackground": true, "timeout": 300000 });
retries++;
({ size } = await IO.stat(path));
}
return retries >= maxRetries;
}
async function convertToPDF(tab, url, name, i, stylesheet) {
let filename = `${i}.pdf`;
let path = `${outputPath}/${name}/${filename}`;
if (!overwrite && await IO.pathExists(path)) {
return path;
}
await IO.ensureDir(path.replace(filename, ""));
let page = await ensureGoTo(tab, url);
if (!page) {
return false;
}
await page.addStyleTag({ "content": stylesheet });
let { height, width } = await page.evaluate(() => {
let result = {
"height": {
"value": 0,
"estimated": false
},
"width": {
"value": 0,
"estimated": false
}
};
let article = document.querySelector("#content article");
if (article) {
result.height.value = article.scrollHeight;
result.width.value = article.scrollWidth;
} else {
result.height.estimated = true;
result.width.estimated = true;
let main = document.querySelector("[tabindex=\"0\"]");
let content = document.querySelector("#content");
// best height estimate:
result.height.value = Math.max(
main ? main.scrollHeight : 0,
content ? content.scrollHeight : 0,
document.body.scrollHeight
);
// best width estimate:
result.width.value = Math.max(
main ? main.scrollWidth : 0,
content ? content.scrollWidth : 0,
document.body.scrollWidth
);
}
let header = document.querySelector("body > header");
if (header) {
result.height.value += header.scrollHeight;
} else {
result.height.estimated = true;
result.height.value += 90; // header estimated height
}
result.height.value += 35; // 35 is bottom margin of article
result.height.value += "px";
result.width.value += "px";
return result;
});
if (height.estimated) {
console.log(`Notice - The following page has a non-standard height: ${url}`);
}
if (width.estimated) {
console.log(`Notice - The following page has a non-standard width: ${url}`);
}
await ensurePDFSize(page, path, height.value, width.value);
return path;
}
async function scrapeGuide(guide, browser, cookies, stylesheet) {
let { url, title } = guide;
let path = `${outputPath}/${title}.pdf`;
if (!overwrite && await IO.pathExists(path)) {
console.log(path);
return;
}
let merger = new PDFMerger();
merger.loadOptions = {
"ignoreEncryption": true,
"throwOnInvalidObject": false
};
let page = await newPage(browser, cookies);
page = await ensureGoTo(page, url);
if (!page) {
console.log(`Failed to fetch ${url}`);
return;
}
let pages = await page.evaluate(() => [...document.querySelectorAll("#toc a[data-section-id]")].map(e => e.href));
if (pages.length === 0) {
console.log(`No pages found for ${title}`);
return;
}
for (let i = 1; i <= pages.length; i++) {
let path = await convertToPDF(page, pages[i - 1], title, i, stylesheet);
if (path) {
merger.add(path);
console.log(path);
} else {
console.log(`Failed to convert to pdf ${pages[i - 1]}`);
}
}
if (page.waitForTimeout) {
await page.waitForTimeout(2000);
} else {
await page.wait(2000);
}
await merger.save(path);
await page.close();
console.log(path);
if (!keepTemp) {
await IO.remove(`${outputPath}/${title}/`);
}
}
(async() => {
const stylesheet = await IO.readFile("stylesheet.css", "utf8");
const cookies = await IO.readJSON("cookies.json");
const browser = await Puppeteer.launch();
let page = await newPage(browser, cookies);
page = await ensureGoTo(page, "https://eguides.primagames.com/accounts/account/my_guides");
if (!page) {
throw new Error("Unable to fetch PrimaGames eGuides");
}
let guides = await page.evaluate(() => [...document.querySelectorAll("a.cover")].map(e => ({
"url": e.href,
"title": e.nextSiblings(".title")[0].innerText.replace(/[^A-Za-z0-9 ]+/g, "").replace(/[ ]+/g, " ")
})));
await page.close();
console.log(`Found ${guides.length} eGuides`);
await Throttle.all(guides.map(guide => () => scrapeGuide(guide, browser, cookies, stylesheet)), { maxInProgress });
await browser.close();
process.exit(0);
})().catch(err => {
console.log(err);
process.exit(1);
});