-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (116 loc) · 3.56 KB
/
index.js
File metadata and controls
142 lines (116 loc) · 3.56 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
var express = require("express");
var cheerio = require('cheerio');
var request = require('request');
var app = express();
var path = require('path');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = 80;
//uses port 80 on the production server, use 3000 when on public wifi
app.set("port", port);
app.use(express.static('public'));
app.set('views', 'public');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.get("/", function (req, res) {
res.render("index.html");
});
app.get("/buses/:id", function (req, res) {
res.render("bus.html");
});
app.get("/api/buses/:id", function (req, res) {
busNumber = req.params["id"];
getBusData(busNumber, (data) => {
if (data["busStops"].length == 0 || data["busTimes"] == 0) {
res.send({
"message": "No more buses today or bus may not exist"
});
} else {
res.send(JSON.stringify(data));
}
})
});
app.get("/api/friendlybuses/:id", function (req, res) {
busNumber = req.params["id"];
getBusData(busNumber, (data) => {
toSend = {};
toSend["busStops"] = [];
if (data["busStops"].length == 0 || data["busTimes"] == 0) {
toSend = {
"message": "No more buses today or bus may not exist"
};
} else {
var currentDate = new Date;
for(var i = 0; i < data["busTimes"].length; i++){
if(data["busTimes"][i] === undefined || data["busTimes"][i]["datetime"] === undefined){
continue;
}
var dateOfBus = new Date(data["busTimes"][i]["datetime"]);
if(i != 0 && dateOfBus - currentDate >= 0){
toSend['busStops'].push({"name" : data["busStops"][i], "departure" : dateOfBus});
}
}
for(var i =0; i < toSend['busStops'].length; i++){
if(i != 0 && toSend['busStops'][i]["name"] === toSend['busStops'][0]["name"]){
toSend['busStops'].length = i + 1;
}
}
console.log("----------------------------------------------------------")
console.log(toSend);
for (var key in toSend['busStops']) {
var hours = toSend['busStops'][key]['departure'].getHours();
var minutes = toSend['busStops'][key]['departure'].getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
toSend['busStops'][key]['departure'] = strTime;
}
}
res.send(JSON.stringify(toSend));
})
});
io.on('connection', function (socket) {
// When a new user connects, it will serve the latest data from Tamu
// bus servers
socket.on('busData', function (busID) {
getBusData(busID, function (data) {
socket.emit('busData', data);
});
});
});
http.listen(app.get("port"), function () {
console.log("Server Running on " + port);
});
function getBusData(bus, callback) {
if (bus == null) {
return;
}
if (bus.toString().length == 1) {
bus = '0' + bus;
}
url = 'http://transport.tamu.edu/BusRoutes/Routes.aspx?r=' + bus;
request(url, function (error, response, html) {
var data = {
busStops: [],
busTimes: []
};
if (!error) {
var $ = cheerio.load(html);
// removes arrive/leave row
$("#TimeTableGridView > tr").first().remove();
$("#TimeTableGridView > tr > td").each(function (i, v) {
$this = $(this)
$time = $this.children();
var $th = $this.closest('table').find('th').eq($this.index());
data.busStops.push($th.html());
data.busTimes.push($time.attr());
});
$('.timetable').children('tbody').each(function (i, v) {
//console.log($(this).html());
});
}
callback(data);
});
};