-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
178 lines (162 loc) · 5.89 KB
/
app.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
const express = require("express");
const https = require("https");
const app = express();
const ejs = require("ejs");
const bodyParser = require("body-parser");
const cheerio = require("cheerio");
const fs = require("fs");
const scrapingbee = require("scrapingbee");
require("dotenv").config();
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use(express.static("public"));
app.get("/", (req, res) => {
res.render("index", {
text1: "Text 1 from Url 1 will be shown here",
text2: "Text 2 from Url 2 will be shown here",
text3: "Text 3 from Url 3 will be shown here",
text4: "Text 4 from Url 4 will be shown here",
text5: "Text 5 from Url 5 will be shown here",
});
});
// app.post("/", async (req, res) => {
// const query = req.body.query;
// const queryURL = `https://www.googleapis.com/customsearch/v1?key=${process.env.CUSTOM_SEARCH_API_KEY}&cx=${process.env.SEARCH_ENGINE_ID}&q=${query}`;
// // let bodyText;
// https.get(queryURL, async (response) => {
// var status = response.statusCode;
// let rawData = "";
// response.on("data", async (data) => {
// rawData += data;
// });
// response.on("end", async () => {
// console.log(rawData);
// if (status === 200) {
// const scrappedSearchData = JSON.parse(rawData);
// const url1 = scrappedSearchData.items[0].link;
// const url2 = scrappedSearchData.items[1].link;
// const url3 = scrappedSearchData.items[2].link;
// const url4 = scrappedSearchData.items[3].link;
// const url5 = scrappedSearchData.items[4].link;
// function get(url1) {
// var client = new scrapingbee.ScrapingBeeClient(
// `${process.env.SCRAPPING_BEE_API_KEY}`
// );
// var response = client.get({
// url: url1,
// params: {
// render_js: "False",
// },
// });
// return response;
// }
// const bodyText = await get(url1)
// .then(function (response) {
// const $ = cheerio.load(response.data);
// const text = $("body").text();
// return text;
// })
// .catch((e) => console.log("A problem occurs in 1 : " + e.response.data));
// const bodyText2 = await get(url2)
// .then(function (response) {
// const $ = cheerio.load(response.data);
// const text = $("body").text();
// return text;
// })
// .catch((e) => console.log("A problem occurs in 2 : " + e.response.data));
// const bodyText3 = await get(url3)
// .then(function (response) {
// const $ = cheerio.load(response.data);
// const text = $("body").text();
// return text;
// })
// .catch((e) => console.log("A problem occurs in 3 : " + e.response.data));
// const bodyText4 = await get(url4)
// .then(function (response) {
// const $ = cheerio.load(response.data);
// const text = $("body").text();
// return text;
// })
// .catch((e) => console.log("A problem occurs in 4 : " + e.response.data));
// const bodyText5 = await get(url5)
// .then(function (response) {
// const $ = cheerio.load(response.data);
// const text = $("body").text();
// return text;
// })
// .catch((e) => console.log("A problem occurs in 5 : " + e.response.data));
// res.render("index", {
// text1: bodyText,
// text2: bodyText2,
// text3: bodyText3,
// text4: bodyText4,
// text5: bodyText5,
// });
// } else {
// console.log("Error!");
// res.redirect("/");
// }
// });
// });
// });
app.post('/', async (req, res) => {
const query = req.body.query;
const queryURL = `https://www.googleapis.com/customsearch/v1?key=${process.env.CUSTOM_SEARCH_API_KEY}&cx=${process.env.SEARCH_ENGINE_ID}&q=${query}`;
try {
https.get(queryURL, async (response) => {
var status = response.statusCode;
let rawData = '';
response.on('data', (data) => {
rawData += data;
});
response.on('end', async () => {
console.log(rawData);
if (status === 200) {
const scrappedSearchData = JSON.parse(rawData);
if (scrappedSearchData.items && scrappedSearchData.items.length >= 5) {
const urls = [
scrappedSearchData.items[0].link,
scrappedSearchData.items[1].link,
scrappedSearchData.items[2].link,
scrappedSearchData.items[3].link,
scrappedSearchData.items[4].link,
];
const scrapingPromises = urls.map(async (url) => {
const client = new scrapingbee.ScrapingBeeClient(process.env.SCRAPPING_BEE_API_KEY);
const response = await client.get({
url: url,
params: {
render_js: 'False',
},
});
const $ = cheerio.load(response.data);
return $('body').text();
});
const [bodyText1, bodyText2, bodyText3, bodyText4, bodyText5] = await Promise.all(
scrapingPromises
);
res.render('index', {
text1: bodyText1,
text2: bodyText2,
text3: bodyText3,
text4: bodyText4,
text5: bodyText5,
});
} else {
console.log('Error!');
res.redirect('/');
}
} else {
console.log('Error!');
res.redirect('/');
}
});
});
} catch (error) {
console.error('An error occurred:', error);
res.redirect('/');
}
});
app.listen(3000, () => {
console.log(`Server is running on port 3000`);
});