-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_browser.js
198 lines (158 loc) · 7.26 KB
/
server_browser.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
const express = require('express');
const app = express();
const puppeteer = require('puppeteer');
const axios = require('axios');
import getStaticUrl from './generateUrl'; //Referred from static7/121
import { fetchDropdownLabel } from './dropdown'; //Referred from static7/121
const args = process.argv;
var port = args[2] || 8888;
let pendingRequest = 0;
process.setMaxListeners(Infinity); // <== Important line
app.get('*', async (req, res) => {
let browser = null;
try {
pendingRequest++;
console.log('New Request is received......');
console.log(req.originalUrl);
if (req.originalUrl.match('favicon')) {
res.sendStatus(404);
return;
}
browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--ignore-certificate-errors', '--enable-features=NetworkService'] });
console.log('New Browser is opened........................')
let page = await browser.newPage();
//let requestedUrl = req.protocol + '://' + req.get('Host') + req.url;
let requestedUrl = 'https://www.naukri.com' + req.originalUrl;
//let requestedUrl='https://www.naukri.com/job-listings-211019906694';
//let requestedUrl = 'https://service-v8.resdex.com' + req.originalUrl;
//let requestedUrl = 'http://172.10.130.151:8080' + req.originalUrl;
//console.log("Requested Url:- ", requestedUrl);
// Intercept API response and pass mock data for Puppeteer
await page.setRequestInterception(true);
let sendHTML = true;
page.on('request', request => {
//JD
//console.log(request.resourceType());
let resType = request.resourceType();
let reqUrl = request.url();
//Aborting all the images/font/other/uba requests
if (resType === 'image' || resType === 'font' || resType === 'other' ||
reqUrl.match('naukri.com/uba') || reqUrl.match('googletagmanager.com/gtm.js') || reqUrl.match('nLoggerJB')) {
request.abort();
}
else if (request.url().match(/job-listings-*/)) {
const jobId = request.url().match(/job-listings-[a-zA-Z0-9\-]*([0-9]{12}).*/);
if (jobId == null) {
sendHTML = false;
let requestedDomain = 'https://www.naukri.com';
console.log("redirect " + requestedDomain);
res.redirect(301, requestedDomain);
}
else {
//console.log(request.url());
request.continue();
}
}
else {
//console.log(request.url());
request.continue();
}
});
page.on('response', response => {
//console.log('response url ' + response.url());
///jobapi|jd-api -> JD API
if (response.url().match(/jobapi/)) {
const status = response.status();
//console.log("status" + status);
if ((status >= 300) && (status <= 399)) {
sendHTML = false;
let reqHeaders = response.request().headers();
let headers = {};
for (let header in reqHeaders) {
if (header == 'appid') {
headers[header] = reqHeaders[header];
}
if (header == 'systemid') {
headers[header] = reqHeaders[header];
}
}
axios.get(response.url(), { headers })
.then(response => {
//Will never come here
console.log(response.data);
})
.catch(error => {
console.log('3xx error')
//console.log(error.response["data"])
if (error.response && error.response["data"] && error.response["data"].metaSearch) {
let kw = '';
let metaSearch = error.response["data"].metaSearch;
if (metaSearch.hasOwnProperty('keywords')
&& metaSearch.keywords.length == 0
&& metaSearch.roleId) {
let obj = {
key: 'FA_ROLE',
value: metaSearch.roleId
};
kw = fetchDropdownLabel(obj);
}
else {
kw = metaSearch.keywords;
}
let redirectUrl = getStaticUrl(kw, metaSearch.city);
console.log("redirectUrl " + redirectUrl);
let requestedDomain = 'https://www.naukri.com';
//console.log(requestedUrl);
res.redirect(301, requestedDomain + "/" + redirectUrl);
console.log('response is send');
console.log();
}
else {
console.log(error);
console.log("redirecting to jobs in india");
res.redirect(301, "https://www.naukri.com/jobs-in-india");
}
});
}
else if ((status >= 400) && (status <= 599)) {
console.log(status + ' error');
sendHTML = false;
res.sendStatus(status);
console.log();
}
}
});
page.on('console', msg => {
//console.log('message ' + msg.text());
});
try {
await page.goto(requestedUrl, { waitUntil: 'networkidle2' });
//await page.screenshot({ path: '/data/apps/jobsearch/jd-screenshot.png' });
await page.evaluate(() => {
return document.documentElement.innerHTML;
});
const body = await page.content();
if (sendHTML) {
res.send(body);
console.log('response is send');
await page.close();
console.log('closing the tab.....');
console.log();
}
}
catch (e) {
//console.log(e);
}
} catch (err) {
console.log(err);
//await browser.close();
}
finally {
await browser && browser.close();
console.log('closing the browser.............');
pendingRequest--;
console.log("No of pending request " + pendingRequest);
console.log();
}
});
app.listen(port, () => console.log(`Server is up`))