-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
372 lines (316 loc) · 12.2 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
require('dotenv').config()
const http = require('http');
const url = require('url');
const fetch = require('cross-fetch');
const serveStatic = require('serve-static');
const finalhandler = require('finalhandler');
const {parse,json,text,from} = require('get-body');
const puppeteer = require('puppeteer');
const proxyChain = require('proxy-chain');
const db = require('./database');
const fs = require('fs');
function log(message) {
// console.log(message);
message = message+'\n';
// Append to a file if it exists, otherwise create a new file
fs.writeFile('./logs/log.txt', message, { flag: 'a' }, (err) => {
if (err) {
console.log('error writing to logfile...');
console.error(err);
return;
}
});
}
function escapeSpaces(str) {
if (str.includes(" ")) {
console.log("Space found in the string");
return str.split('').map(char => (char === ' ' ? '\\\\' + char : char)).join('');
} else {
console.log("No space in the string");
return str;
}
}
function replaceSpacesWithPlus(str){
if (str.includes(" ")) {
console.log("Space found in the string");
return str.split(' ').join('+');
} else {
console.log("No space in the string");
return str;
}
}
async function checkIfCached(arg) {
console.log(arg);
console.log('lol', await db(arg,'fetch'));
const result = await db(arg,'fetch');
return result;
}
async function updateData(arg) {
console.log(arg);
console.log(await db(arg,'update'));
}
function isErrorObject(obj) {
if (!(obj instanceof Error)) {
obj = new Error(obj);
}
return obj;
}
const uaData = require('./ua.js');
let browser;
let page;
async function start(){
browser = await puppeteer.launch(
{
headless : true,
args:[
'--window-size=1920,1080',
'--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
]
});
page = await browser.newPage();
}
start();
//serve our file to avoid cors errors
var serve = serveStatic('build');
const requestListener = async function (req, res) {
if (req.method == 'GET') {
console.log('it is a get request');
//console.log(req);
let parameters = url.parse(req.url,true).query;
if (req.url.startsWith('/makes')) {
const cachedData = await checkIfCached('makes');
console.log('cache:',cachedData.length);
if (cachedData.length > 0){
console.log('pulling from makes cache...');
const makes = cachedData.map((i) => {return {text:i.make.name}});
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(makes));
return;
}
//looking for query parameters
let result;
let uaList;
try{
await page.goto(`https://www.rockauto.com/en/catalog/`,{waitUntil : 'networkidle2',});
await page.waitForSelector(`a[href='/en/catalog/zundapp'].navlabellink`);
result = await page.$$eval('.ranavnode .ranavouter .inner .tbl', elements => {
return elements.map((el) => {
return { text:el.textContent};
});
});
}
catch(err) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({err:`ERROR: ${err} Refresh and Try Again, Or click kofi link to report bug.`}));
return;
}
console.log(result);
//process results into the schema for the db.
let dataToStore = result.map((i) => {
return {
make:{
name:i.text,
years:[]
}
}
});
let makesDataBag = {makes:true, dataToStore}
await updateData(makesDataBag);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(result));
}
//clean this elseif statement up
else if(req.url.startsWith('/years')) {
const cachedData = await checkIfCached({years:true, data:parameters.make});
console.log(cachedData.make.years);
if (cachedData.make.years.length > 0) {
console.log('pulling from makes cache...');
console.log(cachedData);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(cachedData.make.years[0].split(',')));
return;
}
console.log(parameters);
const evalMake = escapeSpaces(parameters.make.toLowerCase());
const likeMake = replaceSpacesWithPlus(parameters.make.toLowerCase());
console.log(likeMake,evalMake)
let result;
try {
await page.goto(`https://www.rockauto.com/en/catalog/${likeMake}`,{waitUntil : 'networkidle2',});
await page.waitForSelector(`a[href='/en/catalog/zundapp'].navlabellink`);
result = await page.$$eval(`[id*="catalog-${parameters.make.toLowerCase()}"] + div + .nchildren > .ranavnode:not(.ra-hide)`, nodes => nodes.map(n => n.textContent));
console.log(result);
}
catch(err) {
console.log(err);
const e = isErrorObject(err);
console.log(e.message);
res.setHeader('Content-Type','application/json');
res.end(JSON.stringify({err:`ERROR: ${err} Try refreshing the page and try again`}));
return;
}
console.log("result:",result);
await updateData({years:true, data:{ make:parameters.make.toUpperCase(), years:result}});
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(result));
}
else if(req.url.startsWith('/models')) {
const cachedData = await checkIfCached({models:true, data:{make:parameters.make,year:parameters.year}});
console.log(cachedData);
if(cachedData && cachedData.MakeYear.models.length > 0) {
console.log('pulling from makes cache...');
console.log(cachedData);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(cachedData.MakeYear.models.map((i)=> {return {model:i}})));
return;
}
//process the input
let make = parameters.make;
make = replaceSpacesWithPlus(parameters.make.toLowerCase());
try {
await page.goto(`https://www.rockauto.com/en/catalog/${make},${parameters.year}`,{waitUntil : 'networkidle2',});
await page.waitForSelector(`a[href='/en/catalog/${make},${parameters.year}'].navlabellink`);
}
catch(err) {
console.log(err);
res.setHeader('Content-Type','application/json');
res.end(JSON.stringify({err:`ERROR: ${err} Try refreshing the page and try again`}));
return;
}
let result = await page.$eval(`a[href='/en/catalog/${make},${parameters.year}'].navlabellink`, el => el.id);
console.log(result);
let id = result.substring(result.indexOf('[')+1,result.indexOf(']'));
console.log(id);
//use the id to select all possible models and return it to user.
let models = await page.$$eval(`#navchildren\\[${id}\\] .ranavnode > input:first-child`, models => {
return models.map(model => JSON.parse(model.value));
})
//save to db
await updateData({
models:true,
data:{
make:parameters.make.toUpperCase(),
year:parameters.year,
models:models.map((i)=> i.model)
}
});
models = models.map((model) => {return {model:model.model}});
console.log(models);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(models));
}
//====================engines==========================================================//
else if (req.url.startsWith('/engines')) {
console.log(parameters);
//process the input
let make = replaceSpacesWithPlus(parameters.make.toLowerCase());
let model = replaceSpacesWithPlus(parameters.model.toLowerCase());
//check cached result here
const cachedData = await checkIfCached({engines:true, data:{
make:parameters.make,
year:parameters.year,
model:parameters.model
}
});
console.log(cachedData);
if(cachedData && cachedData.MakeYearModel.engines.length > 0) {
console.log('pulling from engines cache...');
console.log(cachedData);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(cachedData.MakeYearModel.engines));
return;
}
let result;
try{
await page.goto(`https://www.rockauto.com/en/catalog/${make},${parameters.year},${model}`,{waitUntil : 'networkidle2',});
result = await page.$eval(`a[href='/en/catalog/${make},${parameters.year},${model}'].navlabellink`, el => el.id);
}
catch(err) {
console.log(err,'try again...');
console.log(parameters);
res.setHeader('Content-Type','application/json');
res.end(JSON.stringify({err:`ERROR: ${err} Try refreshing the page and try again`}));
return;
}
console.log(result);
let id = result.substring(result.indexOf('[')+1,result.indexOf(']'));
console.log(id);
//use the id to select all possible engines and return it to user.
let engines = await page.$$eval(`#navchildren\\[${id}\\] .ranavnode > input:first-child`, engines => {
return engines.map(engine => JSON.parse(engine.value));
});
//parse for engine and carcode since we'll need it later to identify rock auto parts.
engines = engines.map((engine) => {
return {carcode:engine.carcode, engine:engine.engine}
});
console.log(engines);
await updateData({
engines:true,
data:{
make:parameters.make.toUpperCase(),
year:parameters.year,
model:parameters.model,
engines:engines
}
});
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(engines));
}
else if (req.url.startsWith('/oilfilters')) {
console.log(parameters);
let make = replaceSpacesWithPlus(parameters.make.toLowerCase());
let model = replaceSpacesWithPlus(parameters.model.toLowerCase());
let engineData = JSON.parse(parameters.engine);
let engine = replaceSpacesWithPlus(engineData.engine.toLowerCase());
//check cached result here
const cachedData = await checkIfCached({oilFilters:true, data:{
make:parameters.make,
year:parameters.year,
model:parameters.model,
engine:engineData.engine
}
});
if(cachedData && cachedData.oilFilters.oilFilters.length > 0) {
console.log('pulling from oilFilters cache...');
console.log(cachedData);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(cachedData.oilFilters.oilFilters));
return;
}
//,2.0l+l4+sohc,1354092,engine,oil+filter,5340 we need this in order to go to the oil filter section in rock auto
await page.goto(`https://www.rockauto.com/en/catalog/${make},${parameters.year},${model},${engine},${engineData.carcode},engine,oil+filter,5340`,{waitUntil : 'networkidle2',});
try{
await page.waitForSelector(`form[action='/en/catalog/${make},${parameters.year},${model},${engine},${engineData.carcode},engine,oil+filter,5340'] > .listing-container-border > div > table.nobmp > tbody.listing-inner > tr > td > span`);
}
catch(err) {
res.setHeader('Content-Type','application/json');
res.end(JSON.stringify({err:`ERROR: ${err}`}));
return;
}
console.log(engineData,parameters);
let oilfilterResults = await page.$$eval(`form[action='/en/catalog/${make},${parameters.year},${model},${engine},${engineData.carcode},engine,oil+filter,5340'] > .listing-container-border > div > table.nobmp > tbody`, elements => {
return elements.map((el) => {
return { text:el.innerText};
});
});
console.log(oilfilterResults);
await updateData({
oilFilters:true,
data:{
make:parameters.make.toUpperCase(),
year:parameters.year,
model:parameters.model,
engine:engineData.engine,
oilFilters:oilfilterResults
}
});
res.setHeader('Content-Type','application/json');
res.end(JSON.stringify(oilfilterResults));
}
else {
serve(req, res, finalhandler(req, res));
}
}
}
const server = http.createServer(requestListener);
server.listen(3333);