-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcoursera_downloader.js
82 lines (66 loc) · 3.16 KB
/
coursera_downloader.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
// insert the "Download All Selected" header and textarea
$("div#course-page-content div.course-item-list").before("<br><br><br><h3>Download All Selected</h3>Cut and paste this bash code into the command line. <a href='http://christiangenco.github.com/Coursera-Video-Downloader-Bookmarklet/#select_all' target='_blank'>How?</a><br><textarea id='cmd' rows='5'></textarea><br><br>")
// section selectors
$("h3").prepend("<a data-placement='left' rel='twipsy' data-original-title='Include chapter in multi-download' style='margin-right:10px'><input type='checkbox' class='multidownload_chapter'></input></a>")
// video selectors
$(".lecture-link").prepend("<a data-placement='left' rel='twipsy' data-original-title='Include in multi-download'><input type='checkbox' class='multidownload'></input></a>")
// check/uncheck individual videos based on viewed status
$(".multidownload").each(function(){
if($(this).parents("li").hasClass('unviewed'))
$(this).prop("checked", true);
});
// check/uncheck section based on its videos
function refreshSectionCheckmarks(){
$(".multidownload_chapter").each(function(){
var chapterCheckbox = $(this);
chapterCheckbox.prop("checked", true);
$(this).parents(".course-item-list-header").next(".course-item-list-section-list").find(".multidownload").each(function(){
if(!$(this).attr("checked")){
chapterCheckbox.prop("checked", false);
return;
}
});
});
}
refreshSectionCheckmarks();
// the actual work of writing the command to download the checked videos
function buildCommand(){
var cookieHeader = ' --header \"Cookie:'+ document.cookie.replace(/"/g,'\\"') + '\" ';
var command = "";
$("h3").each(function(sectionIndex){
var sectionName = $(this).text().replace(/Chapter .+ - /,"").replace(/\:/,'-').replace(/^(V|I|X)+\. /,'');
$(this).parent().next().find("a.lecture-link").each(function(videoIndex){
var $lectureLink = $(this);
var videoName = $.trim($lectureLink.text()).replace(/\/|:|"|!/g,"_");
var downloadLink = $lectureLink.attr('href').replace('/lecture/','/lecture/download.mp4?lecture_id=');
var directory = (sectionIndex+1) + '. ' + sectionName + '/';
var filename = directory + (videoIndex+1) + '. ' + videoName + '.mp4';
filename.replace(/\//g, '_');
var cmd = 'echo "' + filename + '" && ';
cmd += 'mkdir -p "' + directory + '" && ';
cmd += 'curl -L -C - ' + cookieHeader + downloadLink + ' -o "' + filename + '"';
if($(this).find(".multidownload").attr("checked"))
command += cmd + "; ";
});
});
$("#cmd").val(command);
}
buildCommand();
// clicking a section checkbox
$(".multidownload_chapter").unbind();
$(".multidownload_chapter").click(function(e) {
var check = $(this).attr("checked") ? true : false;
$(this).parents(".course-item-list-header").next(".course-item-list-section-list").find(".multidownload").each(function(){
$(this).prop("checked", check);
console.log(check);
});
buildCommand();
e.stopPropagation();
});
// clicking a video checkbox
$(".multidownload").unbind();
$(".multidownload").click(function(e) {
buildCommand();
refreshSectionCheckmarks();
e.stopPropagation();
});