-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (62 loc) · 1.84 KB
/
server.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
const express = require("express")
const _ = require("lodash")
const SerpApi = require("google-search-results-nodejs")
const api_key =
"526f7e72c69684a6f256eaffc86e10af557862ea850a7766d3bce74317514759"
const search = new SerpApi.GoogleSearch(api_key)
const app = express()
const port = process.env.PORT || 5000
app.listen(port, () => console.log(`Listening on port ${port}`))
const ranking = (results) => {
const links = results.map((result) => ({
url: result.link,
position: result.position,
title: result.title,
displayed_link: result.displayed_link,
snippet: result.snippet,
}))
let scoreLinks = {}
links.forEach((link) => {
scoreLinks = {
...scoreLinks,
[link.url]: {
score: (scoreLinks[link.url]?.score || 0) + 1 / (1 + link.position),
title: link.title,
link: link.url,
displayed_link: link.displayed_link,
snippet: link.snippet,
},
}
})
let gfg = _.sortBy(_.values(scoreLinks), ["score"])
gfg.reverse()
return gfg
}
const params = {
location: "Austin, Texas, United States",
hl: "en",
gl: "us",
google_domain: "google.com",
num: 20,
}
app.post("/express_backend", (req, res) => {
const results = []
const queryParam = req.query["0"]
search.json({ ...params, q: queryParam, engin: "google" }, (googleResult) => {
results.unshift(...googleResult["organic_results"])
search.json(
{ ...params, q: queryParam, p: queryParam, engin: "yahoo" },
(yahooResult) => {
results.unshift(...yahooResult["organic_results"])
search.json(
{ ...params, q: queryParam, p: queryParam, engin: "bing" },
(bingresult) => {
results.unshift(...bingresult["organic_results"])
const rankedResult = ranking(results)
res.send(rankedResult)
}
)
}
)
})
})