-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (72 loc) · 1.97 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
'use strict';
const cheerio = require('cheerio');
const request = require('request-promise');
var options = {
url : 'http://www.chilexpress.cl/Views/ChilexpressCL/Resultado-busqueda.aspx',
method: 'GET',
headers: {'Connection':'Close'}
};
function parseBody(body){
if (body.indexOf('No hemos encontrado resultados') !== -1){
return -1;
}
var $ = cheerio.load(body);
var infoTable = $('.wigdet-content li').eq(0).html();
var entregaTable = $('.wigdet-content').eq(1).html();
var hitosTable = $('.addresses').html();
//Parsing infoTable
var infoResp = {};
try{
cheerio.load(infoTable)('li').each((i,elem)=> {
var key = elem.children[0].children[0].data.replace(':','').toLowerCase().replace(/ /g, '_');
infoResp[key] = elem.children[1].data.trim();
});
}catch(e){}
//Parsing entregaTable
var entregaResp = {};
try{
cheerio.load(entregaTable)('li').each((i,elem)=> {
if (elem.children[0].children[0].data){
var key = elem.children[0].children[0].data.replace(':','').toLowerCase().replace(/ /g, '_');
entregaResp[key] = elem.children[1].data.trim();
}
});
if (entregaResp['hora_entrega']){
entregaResp['fecha_entrega'] += ' ' + entregaResp['hora_entrega'];
delete entregaResp['hora_entrega'];
}
}catch(e){}
//Parsing hitosTable
var hitosResp = [];
try{
cheerio.load(hitosTable)('tr').each((i,elem)=> {
if (i !== 0){
var fecha = elem.children[0].children[0].data;
var hora = elem.children[1].children[0].data;
var actividad = elem.children[2].children[0].data;
hitosResp.push({
'fecha': fecha + ' ' + hora,
'actividad': actividad
});
}
});
}catch(e){}
return {
info: infoResp,
entrega: entregaResp,
hitos: hitosResp
};
}
function getTrackingInfo(idPedido){
options.qs = {
'DATA' : idPedido
};
return request(options).then(body => {
return parseBody(body);
});
}
module.exports = function (trackingIdArray){
return Promise.all(trackingIdArray.map(t=>{
return getTrackingInfo(t);
}));
};