-
Notifications
You must be signed in to change notification settings - Fork 1
/
getTrafficTime.js
124 lines (101 loc) · 2.95 KB
/
getTrafficTime.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
var select = require('soupselect').select,
htmlparser = require("htmlparser"),
http = require('http'),
sys = require('sys'),
gpio = require('pi-gpio');
// fetch some HTML...
var options90 = {
hostname: 'www.wsdot.com',
port: 80,
path: '/traffic/seattle/traveltimes/commutes/SeattleRedmond90.aspx'
};
var options520 = {
hostname: 'www.wsdot.com',
port: 80,
path: '/traffic/seattle/traveltimes/commutes/SeattleRedmond520.aspx'
};
var selectors = {
selector: '#TravelTimes',
structureFromSelector: 'elem[0].children[3].children[2].children[5].children[0].children[0].data'
}
function displayTrafficData(sourceOptions, selectorOptions){
getTrafficData(sourceOptions, selectorOptions, function (err, time) {
if(err) {
sys.debug("Error: " + err);
}
else{
outputTrafficData(time);
}
});
}
function getTrafficData(sourceOptions, selectorOptions, callback){
getHTMLBody(sourceOptions, function (err, body){
if(err){
sys.debug("Error: " + err);
}
else{
parseBodyForTime(body, selectorOptions, callback);
}
})
}
function getHTMLBody(sourceOptions, callback){
var html = http.get(sourceOptions, function(res) {
sys.puts("Got response: " + res.statusCode);
var body = "";
res.on("data", function(chunk) {
body += chunk;
});
res.on("end", function(err){
callback(err, body);
});
});
}
function parseBodyForTime(htmlText, options, callback){
var handler = new htmlparser.DefaultHandler(function(err, htmlText) {
if (err) {
sys.debug("Error: " + err);
} else {
var elem = select(htmlText, options.selector);
callback(err, elem[0].children[3].children[2].children[5].children[0].children[0].data);
}
});
var parser = new htmlparser.Parser(handler);
parser.parseComplete(htmlText);
}
function outputTrafficData(time){
sys.puts(time);
}
displayTrafficData(options90, selectors);
displayTrafficData(options520, selectors);
// function get(urlOptions){
// var html = http.get(urlOptions, function(res) {
// sys.puts("Got response: " + res.statusCode);
// var body = "";
// res.on("data", function(chunk) {
// body += chunk;
// });
// res.on("end", function(){
// return body;
// });
// });
// }
// sys.puts(getHTMLBody(options) + "1");
// sys.puts("test");
// var minutes = 1, the_interval = minutes * 60 * 1000;
// setInterval(function() {
// res.on("end", function(){
// var handler = new htmlparser.DefaultHandler(function(err, dom) {
// if (err) {
// sys.debug("Error: " + err);
// } else {
// var elem = select(dom, '#TravelTimes');
// console.log(elem[0].children[3].children[2].children[5].children[0].children[0].data);
// }
// });
// var parser = new htmlparser.Parser(handler);
// parser.parseComplete(body);
// });
// }).on('error', function(e) {
// console.log("Got error: " + e.message);
// });
// }, the_interval);