-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (105 loc) · 4.96 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
const puppeteer = require('puppeteer');
const io = require('socket.io')();
const md5 = require('md5');
// Listen to connections on port 3000
io.listen(3000);
var isSearching = false;
console.log('SERVER STARTED. Listening to port 3000...');
io.sockets.on('connection', function (socket) {
socket.on('search-order', (order, response) => {
if (order != null && 'keyword' in order) {
if (!isSearching) {
isSearching = true;
scrape(order.keyword, order.encrypted_url, order.search_engine_url, (callback) => {
response(callback);
});
} else response('Error');
}
});
// This method does the google search and finds the correct URL
let scrape = async (keyword, encryptedUrl, googelUrl, callback) => {
console.log(`Searching for keyword => ${keyword}`);
const browser = await puppeteer.launch({headless: true, timeout: 0, args: ['--no-sandbox', '--disabled-setuid-sandbox']});
const page = await browser.newPage();
try {
await page.goto(googelUrl, {waitUntil: 'load', timeout: 0 });
await page.type('input[name=q]', keyword, {delay: 100});
await page.keyboard.press('Enter');
await page.waitForSelector('.g h3 a', {visible: true, timeout: 10000 });
const elements = await page.evaluate("[].map.call(document.querySelectorAll('.g h3 a'), a => a.href)");
let endOfPageCheck = false;
let pageIndex = 1;
console.log(`Checking google page ${pageIndex}...`);
let noResult = async function () {
try {
console.log(`No matching URL found :(`);
setTimeout(() => {isSearching = false;}, 10000);
callback('NotFound');
await browser.close();
return;
} catch (e) { error(e,null); }
}
let error = async function (e, cause) {
try {
console.log(`Retrying ${cause}... ERROR => ${e}`);
setTimeout(() => {isSearching = false;}, 10000);
callback('Error');
await browser.close();
return;
} catch (e) { error(e,null); }
}
let findUrl = async (links) => {
pageIndex++;
for (let x = 0; x < links.length; x++) {
let href = links[x];
let pattern = /https?:\/\/(?:www\.)?(.+)/;
let pathname = pattern.exec(href)[1];
if (pathname.slice(-1) === '/') pathname = pathname.slice(0, -1);
if (md5(pathname) == encryptedUrl) {
console.log(`URL found => ${href}`);
urlFound = true;
setTimeout(() => {isSearching = false;}, 10000);
callback(href);
await browser.close();
return;
}
}
try {
if (endOfPageCheck) noResult();
else {
const nextHandle = await page.$('#pnnext');
if (nextHandle !== null) {
await page.waitFor(5000);
// Go to next page
console.log(`Checking google page ${pageIndex}...`);
await nextHandle.click();
getUrls();
} else {
// If not found, we have reached the end of page
await page.waitForSelector('#topstuff', {visible: true, timeout: 10000 });
let endOfPage = await page.evaluate(() => document.querySelector('#topstuff').childElementCount);
if (endOfPage > 0) noResult();
else {
endOfPageCheck = true;
getUrls();
}
}
}
} catch (e) { error(e,'handle'); }
}
let getUrls = async function () {
try {
await page.waitForSelector('.g h3 a', {visible: true, timeout: 10000 });
const newElements = await page.evaluate("links: [].map.call(document.querySelectorAll('.g h3 a'), a => a.href)");
findUrl(newElements);
} catch (e) { error(e,null); }
}
findUrl(elements);
} catch (e) {
console.log(`Retrying... ERROR => ${e}`);
setTimeout(() => {isSearching = false;}, 10000);
callback('Error');
await browser.close();
}
};
});