-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
133 lines (123 loc) · 3.14 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
#!/usr/bin/env node
/**
* Cek Resi CLI
* (c) 2016 Ipan Ardian
*
* A command line app to tracking AWB number
* For details, see the web site: https://github.com/ipanardian/cekresi-cli
* The MIT License
*/
'use strict'
const VERSION = 'v1.0.9'
if (process.argv.length <= 2) {
console.log("Usage: cekresi-cli AWB_NUMBER")
process.exit(-1)
}
var param = process.argv[2]
if (param == '--version' || param == '-v') {
console.log(VERSION)
process.exit(-1)
}
else if (param.length < 5 || !param.match(/^[a-z0-9]+$/i)) {
console.log('Invalid AWB Number')
process.exit(-1)
}
var Horseman = require('node-horseman')
var cheerio = require('cheerio')
var tabletojson = require('tabletojson')
var Table = require('cli-table')
var colors = require('colors')
var Spinner = require('cli-spinner').Spinner
var spinner = new Spinner('Checking... %s ')
const url = "http://cekresi.com/?v=759802634&e=jne&noresi="
spinner.setSpinnerString(7)
spinner.start()
var horseman = new Horseman()
horseman
.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.3")
.on('error', function(message, trace) {
console.log(message)
})
.on('timeout', function(message) {
console.log('Timeout\n')
})
.open(url+param)
.waitFor(function(selector) {
return $(selector).is(':visible')
}, '#results', true)
.evaluate(function() {
return document.querySelector('#results').innerHTML
})
.then(function(result) {
cekresi.load(result).parse()
})
.finally(function() {
horseman.close()
spinner.stop()
return
})
var cekresi = {
result: null,
tablesAsJson: null,
infoExpedisi: null,
el: null,
checkResult() {
return (this.result != '' && this.infoExpedisi != '' ? true:false)
},
load(res) {
this.parseHTML(res)
this.result = res
this.infoExpedisi = this.el('h3.top_title').text()
return this
},
parseHTML(html) {
this.el = cheerio.load(html)
},
parseDeliveryInfo() {
var deliveryTable = new Table()
for (let tr in this.tablesAsJson[0]){
let row = []
for (let td in this.tablesAsJson[0][tr]) {
row.push(this.tablesAsJson[0][tr][td])
}
deliveryTable.push(row)
}
print(deliveryTable.toString())
},
parseActivityInfo() {
let header = []
let body = []
for (let i = 1; i <= this.tablesAsJson.length - 1; i++) {
for (let tr in this.tablesAsJson[i]) {
let row = []
for (let td in this.tablesAsJson[i][tr]) {
if (body.length < 1)
header.push(td)
row.push(this.tablesAsJson[i][tr][td])
}
body.push(row)
}
}
var activityTable = new Table({head: header})
for (let item in body) {
activityTable.push(body[item])
}
print(activityTable.toString())
},
parse() {
if (this.checkResult()) {
this.tablesAsJson = tabletojson.convert(this.result)
print('\n==================== '+ this.infoExpedisi.yellow +' =========================\n')
this.parseDeliveryInfo()
print('')
this.parseActivityInfo()
}
else {
print('\nMaaf no resi yang anda masukkan sementara tidak dapat kami proses'.yellow)
}
print('\nFor details: https://github.com/ipanardian/cekresi-cli')
}
}
function print(msg) {
console.log(msg)
}