-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_class.js
164 lines (142 loc) · 5.02 KB
/
check_class.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
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
/*
* Dartmouth Course Checker
*
* Displays Dartmouth course data, including enrollment. Easily extendible to
* provide email notification on course availability detection.
*
* Return values:
* 0 if class has room
* 1 if class is full
* -1 if the program has an error
*
* Usage: phantomjs check_class.js [DEPARMENT] [COURSE NUMBER]
* Example: phantomjs check_class.js EARS 6
*
* Created by Daniel Chen '16
* Github: cheniel
*/
var verbose = false;
// called when the class has space. Fill this out if you like!
// send an email, modify a database, etc...
function classHasSpaceHandler(results, page) {
// // MANDRILL EMAIL TEMPLATE
// // If you would like to have an email sent when course availabilities is
// // detected, uncomment the code below, register for a free account, and
// // fill in YOUR_API_KEY, YOUR@EMAIL.HERE, and RECIPIENT@EMAIL.HERE
// page.evaluate(function(title, enrl, lim) {
// console.log("sending email");
// $.ajax({
// async: false,
// type: 'POST',
// url: 'https://mandrillapp.com/api/1.0/messages/send.json',
// data: {
// 'key': 'YOUR_API_KEY',
// 'message': {
// 'from_email': 'YOUR@EMAIL.HERE',
// 'to': [
// {
// 'email': 'RECIPIENT@EMAIL.HERE',
// 'type': 'to'
// }
// ],
// 'autotext': 'true',
// 'subject': 'Sign up for ' + title + ' now!!',
// 'html': 'The time has come! The capacity is ' + enrl + '/' + lim + ". <a href='http://www.dartmouth.edu/bannerstudent/'>Banner.</a>"
// }
// }
// }).done(function(response) {
// console.log("response: " + response);
// });
// }, results.title, results.enrl, results.lim);
// console.log("finished");
}
// constants
var NUM_DATA_POINTS = 17,
SERVER = 'https://banner.dartmouth.edu/dart/groucho/timetable.course_quicksearch',
JQUERY = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js';
// get command line args
var system = require('system');
var args = system.args;
// terminate if no arguments
if (args.length !== 3) {
console.log("Usage: phantomjs" + args[0] + " [DEPARMENT] [COURSE NUMBER]");
console.log("Example: phantomjs" + args[0] + " EARS 6");
phantom.exit(-1);
}
var page = require('webpage').create(),
data = 'classyear=2008&subj=' + args[1] + '&crsenum=' + args[2];
// ERROR HANDLERS
phantom.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
console.error(msgStack.join('\n'));
phantom.exit(-1);
};
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
};
// // Uncomment if you would like to see console.log() messages from inside
// // page.evaluate(function() {...})
// page.onConsoleMessage = function(msg) {
// console.log(msg);
// };
page.open(SERVER, 'post', data, function(status) {
if (status !== 'success') {
console.log('Unable to post!');
phantom.exit(-1);
}
// include jquery
page.includeJs(JQUERY, function() {
var results = [];
for (var i = 0; i < NUM_DATA_POINTS; i++) {
// get data type from header row
var key = page.evaluate(function(idx) {
var tableRows = $(".data-table > table > tbody > tr");
return tableRows.eq(0).children("th").eq(idx).text().toLowerCase();
}, i);
// get data from data row
var data = page.evaluate(function(idx) {
var tableRows = $(".data-table > table > tbody > tr");
return tableRows.eq(2).children("td").eq(idx).text();
}, i);
results[key] = data;
}
// print data
if (verbose) {
console.log("Course Information:");
for(var dataType in results) {
console.log(dataType + ": " + results[dataType]);
}
}
if (results.term === "") {
console.log("Could not find specified course for next term.");
phantom.exit(-1);
}
if (!results.lim) {
console.log("There is no limit for this class.");
phantom.exit(1);
} else if (!results.enrl) {
console.log("No enrollment information can be found for this class.");
phantom.exit(1);
} else if (parseInt(results.lim) > parseInt(results.enrl)) {
console.log((new Date()).toLocaleTimeString() + " | " + results.enrl + "/" + results.lim + " | " + "THERE IS ROOM IN THE CLASS!!! " + "\u0007");
classHasSpaceHandler(results, page);
phantom.exit(0);
} else {
console.log((new Date()).toLocaleTimeString() + " | " + results.enrl + "/" + results.lim + " | " + "The class is full. ");
phantom.exit(1);
}
});
});