Skip to content

Instantly share code, notes, and snippets.

@Luavis
Last active October 3, 2015 05:49
Show Gist options
  • Save Luavis/59ac6fc001cefa86417f to your computer and use it in GitHub Desktop.
Save Luavis/59ac6fc001cefa86417f to your computer and use it in GitHub Desktop.
ssu timetable crawler
var saintPage = require('webpage').create();
var loadInProgress = false;
var stepIndex = 0;
var id = '';
var passwd = '';
var endProcess = true;
saintPage.onConsoleMessage = (function(msg) {console.log(msg);});
saintPage.onAlert = (function(msg) {console.log('ALERT' + msg);});
saintPage.onError = function (msg) {console.log('ERR ' + msg);};
saintPage.onResourceReceived = function(response) {
console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
};
saintPage.onResourceRequested = function(requestData, networkRequest) {
console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};
saintPage.onLoadStarted = function() {
console.debug('open url ' + saintPage.url);
loadInProgress = true;
};
saintPage.onLoadFinished = function (status) {
if(endProcess) {
console.log('load finished ' + saintPage.url);
loadInProgress = false;
}
else {
loadInProgress = true;
saintPage.render('img22.png');
console.log('you can not be here ' + saintPage.url);
}
};
var steps = [
function() {
saintPage.open('http://saint.ssu.ac.kr');
console.debug('open saint!');
endProcess = true;
},
function() {
saintPage.evaluate(function(id, passwd) {
document.getElementById('logonuidfield').value = atob(id);
document.getElementById('logonpassfield').value = atob(passwd);
$('input.btn[name="Login"]').click();
}, id, passwd);
endProcess = true;
},
function() {
saintPage.render('img.png');
saintPage.evaluate(function() {
document.getElementById('navNode_1_2').click();
});
setTimeout(function() {
endProcess = true;
}, 4000);
},
function() {
saintPage.render('img1.png');
saintPage.evaluate(function() {
document.getElementById('navNode_2_1').click();
});
setTimeout(function() {
endProcess = true;
}, 4000);
},
function() {
saintPage.evaluate(function() {
var collegeIndex = 1;
var majorIndex = 1;
var subMajorIndex = 1;
var loopFlag = setInterval(function() {
try {
var content = document.getElementById('ivuFrm_page0ivu2').contentDocument
.querySelectorAll('iframe#isolatedWorkArea')[0].contentDocument
.querySelectorAll('frameset > frame')[0].contentDocument;
content.querySelectorAll('#WDDD')[0].click(); // click college button first
content.querySelectorAll('#sapPopupMainId_X0')[0].contentDocument
.querySelectorAll('.urCob2Box')[0]
.querySelectorAll('tr:nth-child(' + collegeIndex + ') >td')[0]
.click(); // select college
content.querySelectorAll('#WDDD')[0].click(); // click major
content.querySelectorAll('#sapPopupMainId_X0')[0].contentDocument
.querySelectorAll('.urCob2Box')[0]
.querySelectorAll('tr:nth-child(' + majorIndex + ') >td')[0]
.click(); // select major
content.querySelectorAll('#WDDD')[0].click(); // click subMajor
content.querySelectorAll('#sapPopupMainId_X0')[0].contentDocument
.querySelectorAll('.urCob2Box')[0]
.querySelectorAll('tr:nth-child(' + subMajorIndex + ') >td')[0]
.click(); // select sub major
content.querySelectorAll('#WDF1')[0].click(); // click search
content.getElementById('WD016E').click(); // click export
content.getElementById('sapPopupMainId_X0').contentDocument
.querySelectorAll('td.urMnuTxt > span')[0].click(); // export!!
clearInterval(loopFlag);
}
catch(err) {
// redo inifinite
console.log('Click Redo!');
}
}, 1000);
});
setInterval(function() {
saintPage.render('click-1.png');
}, 1000);
},
function() {
setTimeout(function() {
saintPage.evaluate(function() {
});
endProcess = true;
}, 7000);
},
function() {
phantom.exit();
}
];
var interval = setInterval(function() {
if(!loadInProgress && typeof steps[stepIndex] == 'function' && endProcess) {
console.log('call ' + stepIndex);
endProcess = false;
steps[stepIndex]();
stepIndex++;
}
if(typeof steps[stepIndex] != 'function') {
phantom.exit();
}
}, 50);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment