-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreservoir.js
155 lines (140 loc) · 4.31 KB
/
reservoir.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
var casper = require('casper').create({
viewportSize: {
width: 1280,
height: 800
}
});
// Geisel & BLB
var roomNum_DibsRoomID = {
'1': 1,'1040': 2,'1041': 3,'1042': 4,'1045': 5,'2': 51,'2070': 22,'2071':23,'2072':24,
'518':35, '519': 36, '521':37,'522':38,'618':39,'619':40, '620':41, '622':42,'623':43,'624':44,'625':45,'626':46,
'627':47, '629': 48, '630':49,'631':50,'718':52,'719':53, '720':54, '722':55,'723':56,'724':57,
'106':7, '107':8, '108':9, '109':10, '110':11, '123':12, '131':13,
'134':14, '204':17, '205':18, '206':19, '223':27, '224':28,
'225':29, '226':30, '227':31, '228':32, '229':33, '230':34
}
var libIDNum = casper.cli.get(0);
var argRmNum = casper.cli.get(1);
var argDate = casper.cli.get(2).toLowerCase();
var argTime = casper.cli.get(3);
var argEmail = casper.cli.get(4);
// Check command line args?
// casper.echo(casper.cli.has(0));
// if (casper.cli.args.length < 5){
// casper.echo('not enough args').exit();
// }
/**
* Currently parses the keywords: 'tomorrow', 'tmr', 'today'
* and returns DIBS formatted date as YYYY/MM/DD
*/
function parseDate(wantedDate){
// Parse date and return formatted date
var year, month, day;
var currDate = new Date(Date.now());
//var dateArray = currDate.toLocaleDateString();
year = currDate.getFullYear();
month = currDate.getMonth()+1;
day = currDate.getDate();
if (wantedDate === 'today'){
var dibsFormattedDate = year+'/'+month+'/'+day;
return dibsFormattedDate;
} else if (wantedDate === 'tomorrow' || wantedDate === 'tmr'){
var tomorrow = new Date();
tomorrow.setDate(currDate.getDate() + 1)
year = tomorrow.getFullYear();
month = tomorrow.getMonth()+1;
day = tomorrow.getDate();
var dibsFormattedDate = year+'/'+month+'/'+day;
return dibsFormattedDate
}
var dibsFormattedDate = year+'/'+month+'/'+day;
return dibsFormattedDate;
}
/**
* Takes in user input for time and returns military time
* Input:
* 6pm, 10:00am, 9:30pm
*/
function parseTime(str){
var hr, min;
var meridiem = str.split(/\d/g).pop().toLowerCase();
var nums = str.match(/\d+/);
if (nums.length == 1){
hr = nums.pop();
min = '00';
} else {
min = nums.pop();
hr = nums.pop();
}
if (meridiem == 'pm'){
var hr_int = parseInt(hr, 10);
hr_int += 12;
hr = hr_int;
}
var formattedTime = hr+':'+min+':00';
return formattedTime;
}
casper.start("http://ucsd.evanced.info/dibs/Login/", function(){
this.echo('Opening ' + this.getTitle());
this.sendKeys('input#tbxPatronLibCard', libIDNum.toString());
this.click('input#btnLoginSubmit');
this.echo('Submit Button Clicked');
});
casper.then(function(){
this.waitForSelector('select#SelectedRoomSize', function(){
this.echo(this.getCurrentUrl());
}, 5000);
});
casper.then(function(){
this.evaluate(function(){
document.querySelector('select#SelectedSearchDate').selectedIndex = 2;
})
this.click('input[value="Search"]');
this.echo('Search Button Clicked');
this.waitForSelector('form#frmBuildings', function(){
this.echo(this.getCurrentUrl());
}, 1000);
});
casper.then(function(){
// Doesn't matter which LIbrary is clicked because all it does is hide the rooms
// that belong to the other library
this.click('div.item-link');
this.echo('Clicked');
this.echo(this.getCurrentUrl());
});
casper.then(function(){
var formattedDate = parseDate(argDate);
var formattedTime = parseTime(argTime);
var startTime = formattedDate + ' ' + formattedTime;
this.fill('form#frmTimes', {
'SelectedStartTime' : startTime
}, true);
this.waitForSelector('form#frmRooms', function(){
this.echo(this.getCurrentUrl());
}, 5000);
});
casper.then(function(){
// select room
var roomNum = roomNum_DibsRoomID[argRmNum].toString();
this.echo(roomNum)
this.fill('form#frmRooms',{
'SelectedRoomID': roomNum
},true);
this.waitForSelector('input#EmailAddress', function(){
this.echo(this.getCurrentUrl());
}, 5000);
});
casper.then(function(){
this.sendKeys('input#EmailAddress', argEmail);
this.click('button#btnCallDibs');
this.echo('Clicked submit!!');
this.waitTimeout(1000);
this.waitForSelector('div.confirm', function(){
this.echo(this.getCurrentUrl());
}, 5000);
});
casper.run(function(){
this.captureSelector('dibs.jpg', 'html');
this.echo('Screenshot Taken');
this.exit();
});