-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScraperSSR.js
364 lines (291 loc) · 13.7 KB
/
ScraperSSR.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
let
puppeteer = require('puppeteer-core'),
chromium = require('chrome-aws-lambda'),
browser,
{ took } = require('../helpers/timer'),
loadTime = new Date(),
launchBrowser = async () => {
return puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
};
class ScraperSSR {
async generateBrowser() {
let startTime = new Date();
// browser = await puppeteer.launch({headless: true});
if(!browser) {
// console.log('Generating browser...');
browser = await launchBrowser();
browser.on('disconnected', async () => {
console.log('browser is disconnected.');
await launchBrowser();
});
}
// Profiling Launch
let launchedTime = new Date();
took(startTime, launchedTime, 'Puppeteer launch');
}
// Hold up until the page loads
async waitTillPageLoads(url) {
try {
let startTime = new Date();
const page = await browser.newPage();
// Profiling new page
let newPageLoaded = new Date();
took(startTime, newPageLoaded, 'New Page took');
await page.goto(url, {waitUntil: 'domcontentloaded'});
// Profiling page.goTo
let goTo = new Date();
took(newPageLoaded, goTo, 'page.goTo');
return page;
} catch (error) {
console.log('could not load page: ', { error, url });
}
}
// async closeBrowser() {
// await browser.close();
// }
async getInnerText(page, selector) {
return page.$$eval(selector, async nodes => {
if(nodes.length === 1) return nodes[0].innerText;
return nodes.map(n => n.innerText);
});
}
async getInnerAttributes(page, selector) {
const selectorQuery = selector.query || selector;
let data = await page.$$eval(selectorQuery, async (nodes,attr) => {
let info = nodes.map(n => n.getAttribute(attr));
if(info.length === 1) info = info[0];
return info;
}, selector.attribute);
if(selector.json) {
data = data.map(n => JSON.parse(n))
}
return data;
}
// Extract data from an element
async evaluateElelemt(page, _selector) {
const selector = _selector.query || _selector;
// console.log('[Scraper SSR] evaluateElelemt', { selectorType: typeof selector, pageIsDefined: !!page });
if (!page) {
console.trace('page is not defined', { page, selector });
return;
}
if(typeof selector === 'string') return this.getInnerText(page, selector);
return this.getInnerAttributes(page, selector);
}
// Turn pages and selectors into data
async getSelection(page, query, depth = 1) {
const queries = Array.isArray(query) ? query : [query];
// console.log('[Scraper SSR] getSelection top level', { query, pageIsDefined: !!page })
const baseCaseResults = [];
let recursionResults = [];
for(let i = 0; i < queries.length; i++) {
const curQuery = queries[i];
const queryVal = curQuery.val || curQuery;
// Base case
if(!curQuery.query) {
// console.log('[Scraper SSR] getSelection basecase1', { queryVals })
// console.log('[Scraper SSR] getSelection basecase1.1', { curQuery, pageIsDefined: !!page, pageText: page.innerText })
// const promise = await this.evaluateElelemt(page, curQuery);
const promise = this.evaluateElelemt(page, curQuery);
baseCaseResults.push(promise);
// console.log('[Scraper SSR] getSelection basecase2', { queries, res })
}
// Recursion case
else {
let primaryElements = await page.$$(queryVal);
// Process each matching element
for(let i = 0; i < primaryElements.length;i++) {
// for(let i = 0; i < 1;i++) {
let elem = primaryElements[i];
// console.log('[Scraper SSR] getSelection debug recurse', { elem: elem.innerText, queryVal, len: primaryElements.length, i })
let recursionResult = this.getSelection(elem, curQuery.query, depth + 1);
// let recursionResult = await this.getSelection(elem, curQuery.query, depth + 1);
// console.log('[Scraper SSR] getSelection debug recurse2', { elem: elem.innerText, recursionResult, len: primaryElements.length, i })
// console.log('[Scraper SSR] getSelection debug recursion popping', { elem: elem.innerText, queryVal, len: primaryElements.length, i, recursionResult })
recursionResults.push(recursionResult);
}
};
}
const [baseCasesResolved, recurionCasesResolved] = await Promise.all([
Promise.all(baseCaseResults),
Promise.all(recursionResults),
]);
// const [baseCasesResolved, recurionCasesResolved] = [baseCaseResults, recursionResults];
const combinedResults = this.arr1ByArr2(baseCasesResolved, recurionCasesResolved);
const flattened = this.flattenToTwoDinesionalArray(combinedResults);
// console.log('[Scraper SSR] getSelection debug recursion popping', JSON.stringify({ baseCasesResolved, recurionCasesResolved, combinedResults, flattened, depth }, null, 2));
return combinedResults;
// return [...baseCaseResults, ...recursionResults];
}
// Flatten two arrays together
// arr1 = [1,2,3] arr2 = [4,5]
// result = [[1,2,3,4], [1,2,3,5]]
arr1ByArr2(arr1, _arr2) {
let result = [];
// console.log('flattenArrays debug1', JSON.stringify({ arr1, _arr2, arr1IsEmpty: this.isEmptyArrayOrArrayOfEmptyArrays(arr1), arr2IsEmpty: this.isEmptyArrayOrArrayOfEmptyArrays(_arr2) }, null, 2));
if (this.isEmptyArrayOrArrayOfEmptyArrays(arr1)) return _arr2;
if (this.isEmptyArrayOrArrayOfEmptyArrays(_arr2)) return arr1;
const arr2 = this.isArrayOfArrays(_arr2) ? _arr2 : [_arr2];
for(let j = 0; j < arr2.length; j++) {
let arr2Array = arr2[j];
// console.log('flattenArrays debug2', JSON.stringify({ arr1,arr2, _arr2, isArrofArrs: this.isArrayOfArrays(_arr2), arr2Array, res: [...arr1, ...arr2Array] }, null, 2))
result.push([...arr1, ...arr2Array]);
}
if (this.isArrayOfArraysButOnlyOneElementNested(result)) return result[0];
return result;
}
// flattens X dimensional array to 2 dimensional array if X is greater than 2
flattenToTwoDinesionalArray(arr, maxArrayDimensions = 2, depth = 1) {
// console.log('flattenToTwoDinesionalArray debug0', JSON.stringify({ arr, maxArrayDimensions, depth }, null, 2));
if (!Array.isArray(arr)) return arr;
let toRet = [];
for(let i = 0; i < arr.length; i++) {
const elem = arr[i];
if (!Array.isArray(elem)) toRet.push(elem);
else {
const arrDepth = this.getDepthOfArray(elem);
const depthGreaterThanDesired = arrDepth > maxArrayDimensions - 1;
let flattened = false;
// console.log('flattenToTwoDinesionalArray debug1', JSON.stringify({ elem, toRet, depth, maxArrayDimensions, depthGreaterThanDesired, arrDepth, maxArrayDimensions }, null, 2))
if (depthGreaterThanDesired) {
flattened = this.flattenToTwoDinesionalArray(elem, maxArrayDimensions, depth + 1);
toRet = toRet.concat(flattened);
}
else {
toRet.push(elem);
}
// console.log('flattenToTwoDinesionalArray debug2', JSON.stringify({ arr, elem, toRet, depth, i, flattened, maxArrayDimensions, depthGtDesired: depth > 2, arrDepth, maxArrayDimensions }, null, 2))
}
}
return toRet;
const arrayDepth = this.getDepthOfArray(arr);
console.log('flattenToTwoDinesionalArray debug1', { arr, depth, depthGt2: depth > 2, arrayDepth })
if (arrayDepth < desiredDepth + 1) return arr;
const flattenedOneLevel = this.flattenArray(arr);
const res = this.flattenToTwoDinesionalArray(flattenedOneLevel, desiredDepth, depth + 1);
console.log('flattenToTwoDinesionalArray debug2', { arr, depth, depthGt2: depth > 2, arrayDepth, res })
return res;
}
getDepthOfArray(arr, depth = 0) {
if (!Array.isArray(arr)) return depth;
return this.getDepthOfArray(arr[0], depth + 1);
}
// flattens 2 dimensional array to 1 dimensional array
flattenArray(arr) {
console.log('flattenArray debug1', JSON.stringify({ arr }, null, 2))
if (!Array.isArray(arr)) return arr;
console.log('flattenArray debug2', { arr, reduced: arr.reduce((acc, val) => acc.concat(val), []) })
return arr.reduce((acc, val) => {
const depthOfVal = this.getDepthOfArray(val);
if (depthOfVal < 2) {
console.log('flattenArray debug3', { arr, val, depthOfVal, acc, res: [...acc, val] })
return acc.push(val);
}
console.log('flattenArray debug4', { arr, val, depthOfVal, acc, res: acc.concat(val) })
return acc.concat(val);
}, []);
}
isArrayOfArraysButOnlyOneElementNested(arr) {
return Array.isArray(arr) && Array.isArray(arr[0]) && arr.length === 1;
}
isArrayOfArrays(arr) {
return Array.isArray(arr) && Array.isArray(arr[0]);
}
isEmptyArrayOrArrayOfEmptyArrays(arr) {
return Array.isArray(arr) && (arr.length === 0 || (Array.isArray(arr[0]) && arr[0].length === 0));;
};
async generateEventsFromUrl(url, query, pageNumber = 1) {
// Profile lambdaLoad
let startTime = new Date();
took(loadTime, startTime, 'page: ' + pageNumber + '-------- Lambda loading --------');
let page = await this.waitTillPageLoads(url);
if (!page) {
console.trace('Page is not defined!!', { page, url, query });
return;
}
// Profile pageLoad
let pageLoaded = new Date();
took(startTime, pageLoaded, 'page: ' + pageNumber + '-------- Function waitTillPageLoads --------');
// Wait for first selector before proceeding
await page.waitFor(query.val || query);
// Profile waitFor
let waitFor = new Date();
took(pageLoaded, waitFor, 'page: ' + pageNumber + '-------- Function waitForSelector --------');
// console.log('[Scraper SSR] run debug1', { url, query });
let result = await this.getSelection(page, query);
// console.log('[Scraper SSR] run debug2', { result });
// Profile getSelection
let getSelection = new Date();
took(waitFor, getSelection, 'page: ' + pageNumber + '-------- Function getSelection --------');
// Close browser and return
// this.closeBrowser();
return result;
}
generatePageUrls(url, query, pages = 1, nextPageFunct) {
const month = new Date().getMonth() + 1;
const year = new Date().getFullYear();
let context = { month, year };
let urls = [];
for (let i = 1; i <= pages; i++) {
const urlWithPageNumber = url.replace('{{pageNumber}}', i);
const urlWithPageNumberAndMonth = urlWithPageNumber.replace('{{month}}', context.month);
const urlWithPageNumberAndMonthAndYear = urlWithPageNumberAndMonth.replace('{{year}}', context.year);
urls.push(urlWithPageNumberAndMonthAndYear);
if (nextPageFunct) {
const nextPageObj = nextPageFunct(context);
context = { ...context, ...nextPageObj };
}
}
return urls;
}
// Main Method
async run({ url, query, pages = 1, nextPage }) {
try {
await this.generateBrowser();
const urls = this.generatePageUrls(url, query, pages, nextPage);
const promises = [];
for(let i = 0; i < urls.length; i++) {
let url = urls[i];
let promise = this.generateEventsFromUrl(url, query, i + 1);
promises.push(promise);
// console.log('[Scraper SSR] run debug1', { url, query, pages, urls });
}
const resolved = await Promise.all(promises);
let combinedEvents = [];
for(let i = 0; i < resolved.length; i++) {
let result = resolved[i];
combinedEvents = [...combinedEvents, ...result];
// console.log('[Scraper SSR] run debug2', { result });
}
return combinedEvents;
} catch (error) {
console.trace('Error in SSR run', { error, url, query, pages, nextPage });
}
}
}
const ScraperSSRInstance = new ScraperSSR();
let scraper;
module.exports = {
scraper: ScraperSSRInstance,
lambdaHandler: async (event, context, _this) => {
let result = null;
let browser = null;
if(!scraper) scraper = new ScraperSSR();
try {
let params = JSON.parse(event.body);
result = await scraper.run(...params);
} catch (error) {
return context.fail(error);
} finally {
if (browser !== null) {
await browser.close();
}
}
return context.succeed({statusCode: 200, body: JSON.stringify(result)});
}
}