-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (119 loc) Β· 4.27 KB
/
index.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
const request = require("request");
const cheerio = require("cheerio");
const csvWriter = require('csv-write-stream');
const fs = require("fs");
const readline = require('readline');
const defaultConfig = {
priceMax: 20000,
yearMin: 2015,
mileageMax: 75000,
energies: 'ess,hyb',
regions: "FR-PAC,FR-NAQ",
powerDINMin: 0
}
const carArray = [
{ ...defaultConfig, brand: "ALFA ROMEO", model: "GIULIETTA", powerDINMin: 200},
// {brand: "BMW", model: "SERIE 1"},
// {brand: "MAZDA", model: "3"},
// {brand: "MERCEDES", model: "CLASSE A"},
{ ...defaultConfig, brand: "VOLKSWAGEN", model: "GOLF", energies: 'hyb'},
// {brand: "AUDI", model: "A1"},
// {brand: "VOLVO", model: "V40"}
]
let page = 1;
doRequest = (url) => {
return new Promise(function (resolve, reject) {
request(url, function (error, res, body) {
if (!error && res.statusCode == 200) {
resolve(body);
} else {
reject(error);
}
});
});
}
buildURL = (car) => {
let res = encodeURI(`https://www.lacentrale.fr/listing?makesModelsCommercialNames=${car.brand}$$${car.model}&priceMax=${car.priceMax}&mileageMax=${car.mileageMax}&yearMin=${car.yearMin}&page=${car.page}&energies=${car.energies}®ions=${car.regions}&powerDINMin=${car.powerDINMin}`);
return res.replace('$$', encodeURIComponent(':'));
};
strToNumber = (str = '') => {
let result = "";
str.split('').forEach(value => {
if (Number(value) || value === '0') {
result += value;
}
}
)
return result;
}
parseNumPage = (html) => {
const $ = cheerio.load(html);
let res = $('.numAnn').get(0);
return Math.ceil(res.firstChild.data / 10);
}
parseHTML = (html, brand, carModel) => {
const $ = cheerio.load(html);
let res = $('.searchCard__rightContainer').toArray();
let resArray = [];
res.map(item => {
let model = item.children[0].children[1].lastChild.data;
let price = strToNumber(item.children[1].children[0].children[0].children[0].children[0].data);
let km = strToNumber(item.lastChild.lastChild.lastChild.lastChild.data);
let departement = item.children[2].children[1].children[0].children[0].data;
let year = item.lastChild.children[2].lastChild.lastChild.data;
let link = "https://www.lacentrale.fr" + item.parent.parent.attribs.href;
resArray.push({model: model, departement: departement, price: price, km: km, year: year, link: link})
})
return resArray;
};
writeDocument = async (dataToWrite, brand, model) => {
if (dataToWrite.length > 0) {
let fileName = brand + ' ' + model + '.csv';
var writer;
if (!fs.existsSync(fileName))
writer = csvWriter({headers: ["model", "departement", "price", "km", "year", "link"]});
else
writer = csvWriter({sendHeaders: false});
writer.pipe(fs.createWriteStream(fileName, {flags: 'a'}));
for (let elem of dataToWrite) {
writer.write(elem);
}
writer.end()
}
}
question = (question) => {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(question, (answer) => {
rl.close();
resolve(answer);
});
});
}
main = async () => {
let resArray = [];
console.log("Please wait parsing in progress...")
for (let elem of carArray) {
page = 1;
let maxPage = 1;
for (let i = 1; i <= maxPage; i++) {
let URL = buildURL(elem);
console.log('### SEARCHING ###')
console.log('URL searched : ' + URL);
let body = await doRequest(URL).catch(err => console.error("### Lacentrale website structure has changed ###"));
maxPage = parseNumPage(body);
resArray.push({brand: elem.brand, model: elem.model, cars: parseHTML(body, elem.brand, elem.model)});
page += 1;
}
;
console.log(maxPage + " page parsed for " + elem.brand + ' ' + elem.model);
}
//write in the excel file
for (let e of resArray) {
await writeDocument(e.cars, e.brand, e.model)
}
}
main();