-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (48 loc) · 1.78 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
var cheerio = require('cheerio'),
request = require('request');
exports = module.exports = function (code) {
return new Promise(function(resolve, reject) {
request
.post('http://seguimientoweb.correos.cl/ConEnvCorreos.aspx', function(error, response, body) {
if (error) {
reject(error);
return;
}
var $ = cheerio.load(body),
histories = [],
history = {},
keys = [],
values = [],
information = {};
$('.datosgenerales td').each(function(i) {
if (i % 2 == 0) {
keys.push($(this).text().toLowerCase().trim().replace(' ', '_'));
}
else {
values.push($(this).text().trim());
}
});
keys.map(function(key, index){
information[key] = values[index];
});
$('.tracking tr').each(function() {
history = {
'state': $(this).children('td').eq(0).text().trim(),
'datetime': $(this).children('td').eq(1).text().trim(),
'place': $(this).children('td').eq(2).text().trim()
};
if (history.state) {
histories.push(history);
}
});
resolve({
'information': information,
'history': histories
});
})
.form({
obj_key: 'Cor398-cc',
obj_env: code
});
});
};