forked from ZimbiX/echo360-download-tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
echo360-download-tool.js
95 lines (85 loc) · 2.42 KB
/
echo360-download-tool.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
// Paste jQuery source first
var max_delay = 1500;
var min_delay = 1000;
var course = $('#course-info').text();
var downloads = [];
var echoes = $('#echoes-list > li');
scheduleGetNextVideoUrl(0, echoes.length - 1);
// This function from: http://stackoverflow.com/a/10073788
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
// Function from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function numericReplaceMonth(rawDate) {
var months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
var numericReplacedMonth;
$(months).each(function(i, month) {
if (new RegExp(month + ' ').test(rawDate)) {
numericMonth = pad(i+1, 2);
numericReplacedMonth = rawDate.replace(month + ' ', numericMonth + '-');
}
});
return numericReplacedMonth;
}
function getNextVideoUrl(i, max) {
var download = {}
var currentYear = new Date().getFullYear();
var rawDate = $('.date-value').text();
var date = currentYear + '-' + numericReplaceMonth(rawDate);
var title = course + ' ' + date;
var url = $('.info-value a[title~=Video]').attr('href');
url = url.replace('media.m4v', 'mediacontent.m4v');
title = title.replace(':', '.');
console.log(title);
console.log(url);
download['url'] = url;
download['title'] = title;
downloads.push(download);
scheduleGetNextVideoUrl(i+1, max);
}
function scheduleGetNextVideoUrl(i, max) {
if (i <= max) {
echoes[i].click();
setTimeout(function() {
getNextVideoUrl(i, max);
}, getRandomArbitrary(min_delay, max_delay));
} else {
done();
}
}
function done() {
console.log('Done');
$('body').children().remove();
var list = document.createElement('ul');
$(downloads).each(function() {
var li = document.createElement('li');
var a = document.createElement('a');
href = document.createAttribute('href');
href.value = this['url'];
a.setAttributeNode(href);
var title = document.createTextNode(this['title']);
a.appendChild(title);
li.appendChild(a);
list.appendChild(li);
})
document.body.appendChild(list);
alert('Ready to download! Right click and choose "DownThemAll!..."');
}