-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathgoogle.js
More file actions
106 lines (89 loc) · 3.24 KB
/
google.js
File metadata and controls
106 lines (89 loc) · 3.24 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
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
const express = require('express');
const cheerio = require('cheerio');
const app = express();
const bodyParser = require('body-parser');
const perfhooks = require('perf_hooks');
const performance = perfhooks.performance;
const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const chromedriver = require('chromedriver');
chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
const options = new chrome.Options()
.addArguments('headless')
.addArguments('disable-gpu')
.addArguments('start-maximized')
.addArguments('incognito')
.setUserPreferences({
'profile.managed_default_content_settings.images': 2,
'disk-cache-size': 4096
})
let driver;
let driverReady = false;
const init = async () => {
driver = await new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.setChromeOptions(options)
.build();
await driver.get('https://www.google.com.hk');
driverReady = true;
console.log('[MINION] Driver ready to serve');
// await driver.manage().setTimeouts({ pageLoad: 1000 })
}
init()
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const port = 8081; // set our port
// ROUTES FOR OUR API
// =============================================================================
const router = express.Router(); // get an instance of the express Router
// test route to make sure everything is working (accessed at GET http://localhost:PORT/api)
router.get('/', async (req, res) => {
const startLoad = performance.now()
const {
query,
secret,
} = req.query;
if (!driverReady) return res.json({ err: 'driver not ready' });
if (secret !== 'YOUR_GOOGLE_SECRET') return res.json({ err: 'some error' });
try {
await driver.get(`https://www.google.com.hk/search?q=${encodeURI(query)}`);
} catch {
await driver.executeScript('window.stop()')
}
const endLoad = performance.now();
console.log('done loading with time: ' + (endLoad - startLoad) + ' ms')
const startAwait = performance.now()
const body = await driver.getPageSource()
const endAwait = performance.now();
console.log('done waiting with time: ' + (endAwait - startAwait) + ' ms')
const $ = cheerio.load(body)
let results = []
$('.g').each((i, el) => {
// console.log(i, el)
let $result = cheerio.load(el);
if ($result('cite').text())
results.push({
title: $result('a h3').text(),
link: $result('a').attr('href'),
cite: $result('cite').text(),
preview: $result('.s').text(),
});
})
if(results.length == 0) console.log($('title'))
// return res.send(body);
return res.json({ results });
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);