-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
203 lines (184 loc) · 5.59 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
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
/**
* Module dependencies.
*/
var express = require('express'),
formidable = require('formidable'),
fs = require('fs'),
Path = require('path'),
cheerio = require('cheerio'),
jsonfile = require('jsonfile'),
colors = require('colors');
var app = express();
app.get('/', function(req, res) {
res.sendFile(__dirname + '/views/index.html');
});
/*
Upload file and store it in directory "uploaded".
Call the function that will make treatment of the uploded file.
render result.
*/
app.post('/convert', function(req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
if (Path.extname(files.filetoupload.name) == '.html'){
var oldpath = files.filetoupload.path;
var path = __dirname + '/uploaded/';
var newpath = path + files.filetoupload.name;
if (!fs.existsSync(path)){
fs.mkdirSync(path);
}
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
var html =fs.readFileSync(newpath, 'utf8');
html = html.replace(/\\r\\n|\\/g, '');
var $ = cheerio.load(html);
json = getTrips($);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(json, null, 2));
});
}
else
{
res.send('format inconnu !');
}
});
})
/*
transform data from htlm to json according to the required format.
*/
function getTrips($)
{
var code = $('.block-pnr').last().find('span').first().text().trim();
var name = $('.block-pnr').last().find('span').last().text().trim();
var totalPrice = $('.very-important').text().replace(',', '.').replace('€', '').trim();
var roundTrips = [];
var numItems = $('.product-details').length;
var detail ;
var train = [];
$('.product-details').each(function(index, element)
{
detail =
{
'type': $(this).find('.travel-way').text().trim(),
'date': getDate($, index),
'trains': getTrainDetail($, $(this), index, numItems)
}
roundTrips.push(detail);
});
// Get price.
var prices = [];
$('.cell').each(function(index, element)
{
if (index % 2 != 0)
{
price = {
'value': Number($(this).text().replace(',', '.').replace('€', '').trim()),
}
prices.push(price);
}
});
// Get amount.
var ammount = {
'value': Number($('.amount').text().replace(',', '.').replace('€', '').trim()),
}
prices.push(ammount);
// Prepare jsonfile.
var json = {
'status': 'ok',
'result':{
'trips':[
{
'code': code,
'name': name,
'details': {
'price': Number(totalPrice),
'roundTrips': roundTrips
},
},
],
'custom':{
'prices':prices,
}
}
};
jsonfile.writeFile('result.json',json ,{spaces: 2},function (err) {
if(err) console.error(err)
console.log('The file' + ' result.json ' .green + 'has been generated in:' .white + __dirname .green);
});
return json;
}
/*
get train detail.
*/
function getTrainDetail($, element, index, numItems)
{
trains =[];
var ride = {
'departureTime': element.find('.segment-departure').first().text().replace('h',':').trim(),
'departureStation': element.find('.origin-destination-station').first().text().trim(),
'arrivalTime': element.find('.origin-destination-hour').last().text().replace('h',':').trim(),
'arrivalStation': element.find('.origin-destination-station').last().text().trim(),
'type': element.children().children().children().eq(3).text().trim(),
'number': element.children().children().children().eq(4).text().trim()
};
if (index + 1 == numItems)
{
ride.passengers = getPassengers($, numItems);
}
trains.push(ride);
return trains;
}
/*
get passengers details.
*/
function getPassengers($, numItems)
{
var passengers = [];
for (var i = 1; i <= numItems; i++)
{
var passenger =
{
'type': 'échangeable',
'age': $('.typology').first().text().split('r ')[1].trim()
}
passengers.push(passenger);
}
return passengers;
}
/*
Get trips Dates.
*/
function getDate($, index)
{
switch (index)
{
case 0:
return getFormatedDate($('.pnr-summary').first().text().substring(25, 35).trim());
break;
case 1:
return getFormatedDate($('.pnr-summary').first().text().trim().substr(-10));
break;
case 2:
return getFormatedDate($('.pnr-summary').last().text().substring(28, 38).trim());
break;
case 3:
return getFormatedDate($('.pnr-summary').last().text().trim().substr(-10));
break;
}
}
/*
Reformat date.
*/
function getFormatedDate(date)
{
var splited = date.split('/');
var formattedDate = [splited[2],splited[1],splited[0]].join("-");
formattedDate = formattedDate + " 00:00:00.000Z";
//var date = new Date (formattedDate);
return formattedDate;
}
app.use(function(req, res, next){
res.setHeader('Content-Type', 'text/plain');
res.send('Page introuvable !');
});
app.listen(3000);