forked from maggiewhite/ge-appointment-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathge-cancellation-checker.phantom.js
195 lines (181 loc) · 6.92 KB
/
ge-cancellation-checker.phantom.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
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
var system = require('system');
var fs = require('fs');
var schedule = false,
loadInProgress = false,
verbose = false,
curDate,
newDate,
python = false,
debug = false,
earlierApptAvail = false;
// XXX: send text/email notifications when rescheduling
// XXX: accept arguments to automatically recheck
// Calculate path of this file
system.args.forEach(function(val, i) {
if (val == '-s' || val == '--schedule') { schedule = true; }
if (val == '-v' || val == '--verbose') { verbose = true; }
if (val == '-p' || val == '--python') { python = true; }
});
// Read settings from JSON
try {
var settings = JSON.parse(fs.read(fs.absolute('config.json')));
if (!settings.logfile)
console.log('No logfile specified. Please specify logfile in' +
'config.json');
// Confirm JSON is loaded. Is this and python checks really necessary?
if (!settings.username || !settings.password || !settings.init_url || !settings.enrollment_location_id) {
console.log('Missing username, password, enrollment location ID, and/or initial URL. Exiting...');
phantom.exit();
}
}
catch(e) {
console.log(e + ' Could not find config.json');
phantom.exit();
}
// Set up log file
try {
var logfile = fs.open(fs.absolute(settings.logfile), 'a');
phantom.aboutToExit.connect(logfile.flush);
phantom.aboutToExit.connect(logfile.close);
logfile.writeLine(Date().toString() + ' GOES APPOINTMENT CHECKER');
logfile.flush();
}
catch(e) {
console.log(e + ' Issue opening log file');
}
// Write to log
function log(msg, pri) {
// XXX: pretty sure this will fail if log file doesn't open correctly
logfile.writeLine(Date().toString() + ' ' + msg);
if (verbose || pri) { console.log(msg); }
logfile.flush();
}
function pythonlog() {
if (python) {
console.log(curDate);
if (earlierApptAvail==true)
console.log("Earlier appt available");
else
console.log("Earlier appt unavailable");
console.log(newDate);
}
}
// Open and set up page
var page = require('webpage').create();
page.open(settings.init_url);
page.onConsoleMessage = function(msg) {
log(msg);
};
page.onError = function(msg, trace) {
return;
console.error('Error on page: ' + msg);
}
page.onCallback = function(query, msg) {
if (query == 'username') { return settings.username; }
if (query == 'password') { return settings.password; }
if (query == 'enrollment_location_id') {
return settings.enrollment_location_id.toString(); }
if (query == 'schedule') {
return schedule; }
if (query == 'curDate') {
if (msg) { curDate = msg; return; }
else { return curDate; }
}
if (query == 'newDate') { newDate = msg; }
if (query == 'earlierApptAvail') {
if (msg) {
earlierApptAvail = true;
return; }
else {
return earlierApptAvail; }
}
if (query == 'fatal-error') {
log('Fatal error: ' + msg);
phantom.exit();
}
return null;
}
page.onLoadStarted = function() { loadInProgress = true; };
page.onLoadFinished = function() { loadInProgress = false; };
var steps = [
function() { // Login
page.evaluate(function() {
console.log('Logging in...');
document.querySelector('input[name=username]').value =
window.callPhantom('username');
document.querySelector('input[name=password]').value =
window.callPhantom('password');
document.querySelector('input[name="Sign In"]').click();
});
},
function() { // Accept terms of agreement
page.evaluate(function() {
console.log('Accepting terms...');
document.querySelector('a[href="/main/goes/HomePagePreAction.do"]').click();
});
},
function() { // Appointment management button
page.evaluate(function() {
console.log('Entering appointment management...');
document.querySelector('.bluebutton[name=manageAptm]').click();
});
},
function() { // Collect current date
page.evaluate(function() {
// Current date XXX: clean up this search
date = document.querySelector(".maincontainer p:nth-child(7)").innerHTML.replace(/<strong>[\s\S]*?<\/strong>/, "");
date += " " + document.querySelector(".maincontainer p:nth-child(8)").innerHTML.replace(/<strong>[\s\S]*?<\/strong>/, "");
window.callPhantom('curDate', date);
console.log('Current date found: ' + date);
document.querySelector('input[name=reschedule]').click();
});
},
function() { // Select enrollment center
page.evaluate(function() {
console.log('Selecting enrollment center ' + window.callPhantom('enrollment_location_id'));
document.querySelector('select[name=selectedEnrollmentCenter]').value = window.callPhantom('enrollment_location_id');
document.querySelector('input[name=next]').click();
});
},
function() { // Check next available appointment
page.evaluate(function() {
console.log('Checking for earlier appointment...');
// We made it! Now we have to scrape the page for the earliest available date
var date = document.querySelector('.date table tr:first-child td:first-child').innerHTML;
var month_year = document.querySelector('.date table tr:last-child td:last-child div').innerHTML;
var newDate = month_year.replace(',', ' ' + date + ',');
newDate += " " + document.querySelector('a[href="#"].entry span').innerHTML;
console.log('Next date is ' + newDate);
window.callPhantom('newDate', newDate);
var curDate = window.callPhantom('curDate');
if(Date.parse(newDate).valueOf() < Date.parse(curDate).valueOf()) {
window.callPhantom('earlierApptAvail', true);
// XXX: format this for python script
console.log('Earlier appt available on ' + newDate);
var schedule = window.callPhantom('schedule');
if (schedule) {
document.querySelector('a[href="#"].entry').onmouseup();
}
}
});
},
function() { // Confirm scheduling appointment
pythonlog();
if( earlierApptAvail && schedule ) {
page.evaluate( function() {
console.log('Scheduling earlier appointment');
document.querySelector('input[name=comments]').value = "Earlier appointment";
document.querySelector('input[name=Confirm]').click();
});
}
} // XXX: confirm appointment scheduled. can't do this without losing my appointment
];
var i = 0;
interval = setInterval(function() {
if (loadInProgress) { return; } // not ready yet...
if (typeof steps[i] != "function") {
return phantom.exit();
}
steps[i]();
i++;
}, 100);