-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js.bak
215 lines (198 loc) · 5.2 KB
/
index.js.bak
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require('newrelic');
var Horseman = require('node-horseman');
var horseman = new Horseman();
var himalaya = require('himalaya');
var util = require('util');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var async = require('async');
var timetableURL = null;
var date = null;
var timetableHTML = null;
var count = 8;
var completeJSON = [];
var day = null;
var postResponse = null;
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({
extended: true
})); // support encoded bodies
app.get('/', function(req, res) {
res.send('');
});
app.get('/privacy', function(req, res) {
res.sendFile(__dirname + '/privacy.html');
});
//Cannot deal with multiple post requests at one time
app.post('/', function(req, res) {
postResponse = res;
var username = req.body.username;
var password = req.body.password;
console.log(username);
console.log(password);
loadSpaces(username, password);
async.series([
]);
});
app.listen(process.env.PORT || 3000, function() {
console.log('Running Timetable Server');
});
function loadSpaces(username, password) {
completeJSON = [];
count = 8;
var timetableURLPromise =
horseman
.userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0')
.cookies([])
.open('https://spaces.newington.nsw.edu.au/signin')
.waitForNextPage({
timeout: 15000
})
.click("body > div.container > div.row.button > div > section:nth-child(1) > a")
.waitForNextPage({
timeout: 15000
})
.type("#user_email", username)
.type("#user_password", password)
.click("#user_submit")
.waitForNextPage({
timeout: 15000
})
.url()
.log()
.waitForNextPage({
timeout: 15000
})
.waitForSelector('#diary_timetable_1')
.attribute('#diary_timetable_1', "data-view-loadable-view");
timetableURLPromise.then(attribute => {
console.log("URL = " + attribute);
timetableURL = attribute;
var URLBeforeDate = timetableURL.search('=');
timetableURL = timetableURL.slice(8, URLBeforeDate + 1);
// console.log(timetableURL);
getTimetableData();
});
}
function getTimetableData() {
if (count <= 19) {
if (count < 10) {
date = "2017-05-0" + count;
} else if (count == 13 || count == 14) {
} else {
date = "2017-05-" + count;
}
// // console.log("MADE IT");
var timetableHTMLPromise =
horseman
.open("https://spaces.newington.nsw.edu.au" + timetableURL + date)
.catch(function(err) {
console.log(err);
})
.html("body > table > tbody");
timetableHTMLPromise.then(data => {
dataToJSON(data);
count = count + 1;
getTimetableData();
});
} else {
console.log("DONE");
// console.log("!!!!!!!!!!!!!!!!!!!!!!!!!" + completeJSON);
}
}
function dataToJSON(html) {
var json = himalaya.parse(html);
// // // console.log(util.inspect(json, false, null));
organiseJSON(json);
}
function organiseJSON(json) {
switch (count) {
case 8:
day = "Monday";
break;
case 15:
day = "Monday";
break;
case 9:
day = "Tuesday";
break;
case 16:
day = "Tuesday";
break;
case 10:
day = "Wednesday";
break;
case 17:
day = "Wednesday";
break;
case 11:
day = "Thursday";
break;
case 18:
day = "Thursday";
break;
case 12:
day = "Friday";
break;
case 19:
day = "Friday";
break;
}
// // // console.log(util.inspect(json, false, null));
// console.log(json);
if (count < 15) {
var week = "A";
} else {
var week = "B";
}
var timetableArray = [];
for (var i = 0; i < Math.floor(json.length / 2); i++) {
period = i + 1;
var index = (2 * i);
if (json[index].children[3].children[0].content == "Assembly") {
var dayTimetable = {
"time": "",
"subject": "",
"class": "",
"teacher_code": "",
"period": "Assembly"
};
timetableArray.push(dayTimetable);
} else if (json[index].children[3].children[0].content.indexOf("Mentor") != -1) {
var dayTimetable = {
"time": json[index].children[1].children[0].content,
"subject": json[index].children[3].children[0].content,
"class": json[index].children[5].children[0].content,
"teacher_code": json[index].children[7].children[1].children[0].content,
"period": "Mentor"
};
timetableArray.push(dayTimetable);
} else {
var dayTimetable = {
"time": json[index].children[1].children[0].content,
"subject": json[index].children[3].children[0].content,
"class": json[index].children[5].children[0].content,
"teacher_code": json[index].children[7].children[1].children[0].content,
"period": "Period " + period
};
timetableArray.push(dayTimetable);
}
}
var dayJSON = {
"week": week,
"day": day,
"timetable": timetableArray
};
completeJSON.push(dayJSON);
if (count == 19) {
response();
}
}
function response() {
var returnJSON = completeJSON;
if (returnJSON != null) {
postResponse.send(JSON.stringify(returnJSON));
postResponse.end();
}
}