-
Notifications
You must be signed in to change notification settings - Fork 2
/
echo360-download-tool.js
88 lines (79 loc) · 2.1 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
// Paste jQuery source first
var 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 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);
}, 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);
}