-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
149 lines (114 loc) · 3.79 KB
/
parse.js
File metadata and controls
149 lines (114 loc) · 3.79 KB
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
var http = require('http');
var cheerio = require("cheerio");
var _ = require("lodash");
var exports = module.exports = {};
//
var post_options = {
host: 'passcomm.njtransit.com',
port: 80,
path: '/webdisplay/WebDisplay.asmx/Display2',
method: 'POST',
withCredentials: false,
headers: {
'Host': 'passcomm.njtransit.com',
'Origin': 'http://passcomm.njtransit.com',
'Referer': 'http://passcomm.njtransit.com/webdisplay/tid.aspx?SID=US',
'Content-Type': 'application/json; charset=UTF-8',
'Access-Control-Allow-Origin': 'passcomm.njtransit.com',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type'
}
};
var post_data = JSON.stringify({
'station_id' : 'US',
'platform': '',
'status': '',
'rfont' : '',
'order' : '',
'allowblanking' : '',
'agency' : '',
'browser' : 'Chrome',
'useragent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/47.0.2526.73 Chrome/47.0.2526.73 Safari/537.36"'
});
var data='';
function parseHtmlResponse(response) {
var $ = cheerio.load(response);
var result = [];
var count = 0;
var train = {};
$('p').each(function () {
if (count == 6) {
count = 0;
train = {};
}
switch (count) {
case 0:
train.time = $(this).text().trim();
break;
case 1:
train.to = $(this).text().trim();
break;
case 2:
train.track = $(this).text().trim();
break;
case 3:
train.line = $(this).text().trim();
break;
case 4:
train.train = $(this).text().trim();
break;
case 5:
train.status = $(this).text();
train.minutesLeft = getMinutesLeftFromStatus($(this).text());
if (train.minutesLeft && train.minutesLeft <= 5) {
train.class = 'go-line';
} else if (train.minutesLeft && train.minutesLeft <= 20) {
train.class = 'ready-line';
}
result.push(_.clone(train));
}
count = count + 1;
});
//console.log(result);
return result;
}
exports.getTrainData = function getTrainData(callback) {
var result = null;
// Set up the request
var post_req = http.request(post_options, function (res) {
//console.log('STATUS: ' + res.statusCode);
//console.log('Response HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
//console.log(chunk);
data += chunk;
});
res.on('end', function () {
var s1 = data.toString().replace(/\\r\\n/g, "");
var s2 = s1.replace(/\\u003c/g, "<");
var s3 = s2.replace(/\\u003e/g, ">");
var s4 = s3.replace(/\\">/g, "\">");
var s5 = s4.replace(/\"}/g, "\"");
//extract string that starts right after *SPLIT*
response = s5.substr(s5.lastIndexOf("*SPLIT*") + 7, s4.length);
//console.log(response);
result = parseHtmlResponse(response);
callback(result);
});
});
post_req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
// post the data
post_req.write(post_data);
post_req.end();
return 1;
}
function getMinutesLeftFromStatus(statusParam) {
var status = statusParam.toLowerCase().trim();
if (status.length == 0) {
return null;
} else {
return parseInt(status.substring(3,status.indexOf("min")).trim());
}
}