Last active
December 25, 2015 10:38
-
-
Save Frulko/6962566 to your computer and use it in GitHub Desktop.
CasperJS script to download all documents of courses on Claroline. - Gobelins 2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var clarolineURL = 'http://xxxxxxxxxxxx'; //URL of your Claroline site | |
var links = []; | |
var courses = []; | |
var downloadFolder = './documents/'; | |
var i = -1; | |
function getDocumentsLink() { | |
var links = document.querySelectorAll("table.claroTable tbody tr a.item"); | |
return Array.prototype.map.call(links, function(e) { | |
var link = []; | |
link[0] = e.innerText; //Document Name | |
link[1] = e.getAttribute("href"); //Document download URL | |
return link; | |
}); | |
} | |
function getCoursesList(){ | |
var courses = document.querySelectorAll("div.userCourseList dl.courseList dt span a"); | |
return Array.prototype.map.call(courses, function(e) { | |
var link = []; | |
link[0] = e.innerText; //Course Name | |
link[1] = e.getAttribute("href"); //Course URL | |
return link; | |
}); | |
} | |
var casper = require("casper").create(); | |
casper.start(clarolineURL, function() { | |
this.fill('form.claroLoginForm', { | |
'login': 'xxxxxxx', //Your login | |
'password': 'xxxxxxx' //Your password | |
}, true); | |
}); | |
//casper.thenClick('form.claroLoginForm button[type="submit"]'); //Desactivate to submit form | |
casper.then(function() { | |
courses = this.evaluate(getCoursesList); | |
}); | |
function getAllDoc(t_url){ | |
this.thenClick('a[href="'+t_url[1]+'"]'); | |
this.thenClick('a#CLDOC'); | |
this.then(function() { | |
var courseName = this.getHTML('div.courseInfos h2 a') + ' - ' + this.getHTML('div.courseInfos p b'); | |
this.echo(courseName); | |
documents = links.concat(this.evaluate(getDocumentsLink)); | |
for(var i=0; i<documents.length; i++){ | |
this.download(documents[i][1],downloadFolder+courseName+'/'+documents[i][0]); | |
} | |
this.wait(1000,function(){ | |
this.echo(documents.length + ' files downloaded , next course !'); | |
}); | |
this.back(); | |
this.back(); | |
}); | |
} | |
var i = 0; | |
var check = function() { | |
if (courses[i]) { | |
getAllDoc.call(this,courses[i]); | |
i++; | |
casper.run(check); | |
} else { | |
this.echo("All done."); | |
this.exit(); | |
} | |
}; | |
casper.run(check); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment