Skip to content
This repository was archived by the owner on Aug 28, 2021. It is now read-only.

Commit db06303

Browse files
authored
Merge pull request #76 from CT1994/echo360-download-fix
fixes after echo360 made changes to syllabus json
2 parents d3270dd + ae35b5b commit db06303

2 files changed

Lines changed: 188 additions & 219 deletions

File tree

app/src/background.js

Lines changed: 55 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,64 @@
1-
function getVideoFileName({lesson}, downloadHD) {
2-
// ES6 allows you to do this.
3-
// Old: const updatedAt = lesson.lesson.updatedAt;
4-
// Old: const age, name = person.age, person.name
5-
// New: const {age, name} = person;
6-
const {updatedAt} = lesson.video.media;
7-
const quality = (downloadHD) ? "_HD" : "_SD";
8-
return updatedAt.slice(0, updatedAt.indexOf("T")) + quality + ".mp4";
9-
}
1+
async function getDownloadLink({lessonID, lessonName}, echo360Domain, downloadHD) {
2+
const regex = /(?:\(\")(?:.*)(?:\"\))/;
3+
const lessonHTMLPageRequest = new Request(`${echo360Domain}/lesson/${lessonID}/classroom`, { method: 'GET', credentials: 'include' });
4+
const lessonHTMLPageResponse = await fetch(lessonHTMLPageRequest)
5+
const lessonHTMLPageText = await lessonHTMLPageResponse.text();
6+
const dummyEl = document.createElement('html')
7+
dummyEl.innerHTML = lessonHTMLPageText;
108

11-
// Returns only unit code.
12-
function getUnitCode({lesson}) {
13-
const lectureName = lesson.lesson.name;
14-
var unitCodeTrailing = lectureName.slice(0, lectureName.indexOf("/"));
15-
var splitChar = "_";
16-
if (unitCodeTrailing.indexOf("_") == -1) {
17-
// If there aren't underscores, split by space instead (for e.g. UNSW).
18-
if (unitCodeTrailing.indexOf(" ") != -1) {
19-
splitChar = " ";
20-
}
21-
}
22-
try {
23-
return unitCodeTrailing.split(splitChar)[0];
24-
} catch (err) {
25-
// Some Universities may have weird formats.
26-
return unitCodeTrailing;
9+
const videoDataString = dummyEl.getElementsByTagName('script')[11].innerText.match(regex)[0]
10+
const cleanString = videoDataString.substring(1, videoDataString.length - 1);
11+
const videoDataObject = JSON.parse(JSON.parse(cleanString));
12+
13+
let totalSoruces = 0;
14+
15+
if (!videoDataObject.video) {
16+
return null;
2717
}
28-
}
2918

30-
function getDownloadLink({lesson}, downloadHD) {
31-
// Expected case: lesson.video.media.media.current gives array of downloadable links.
32-
// Unexpected case: no attribute current (see unkown issues).
33-
// TODO: Handle this.
34-
const {primaryFiles} = lesson.video.media.media.current;
19+
videoDataObject.video.playableMedias.forEach((media) => {
20+
if (media.sourceIndex > totalSoruces) {
21+
totalSoruces = media.sourceIndex;
22+
}
23+
})
24+
25+
const downloadArray = [];
26+
for (let i = 1; i <= totalSoruces; i++) {
27+
const quality = downloadHD ? `hd${i}.mp4` : `sd${i}.mp4`;
28+
const videoName = `video_source_${i}_${quality}`
29+
const templateUrl = new URL(videoDataObject.video.playableMedias[0].uri);
30+
templateUrl.search = '';
31+
templateUrl.pathname = templateUrl.pathname.replace(/\/[^\/]*$/, `/${quality}`)
3532

36-
if (downloadHD) {
37-
const {s3Url, width, height} = primaryFiles[1];
38-
// TODO: URL for access outside of Australia.
39-
// URL Access has been enabled, we might need a global variable instead or 2 versions (for multi-region support)
40-
return s3Url;
41-
} else {
42-
const {s3Url, width, height} = primaryFiles[0];
43-
return s3Url;
33+
downloadArray.push({
34+
url: templateUrl.href,
35+
lessonName,
36+
videoName,
37+
});
4438
}
39+
40+
return downloadArray;
4541
}
4642

47-
chrome.extension.onConnect.addListener(function(port) {
48-
console.log("Connected .....");
49-
port.onMessage.addListener(function(toDownload, downloadHD) {
50-
let unitCode = getUnitCode(toDownload[0]);
51-
unitCode = unitCode.replace(/[/\\?%*:|"<>)]/g, '');
43+
chrome.extension.onConnect.addListener(function (port) {
44+
console.log("Connected .....");
45+
port.onMessage.addListener(function ({toDownload, echo360Domain, downloadHD, courseName}) {
5246

53-
toDownload.forEach((downloadable) => {
54-
console.log('downloadable information');
55-
console.log(getDownloadLink(downloadable, downloadHD));
56-
console.log(getVideoFileName(downloadable, downloadHD));
57-
let saveFileAs = unitCode + "_" + getVideoFileName(downloadable, downloadHD);
58-
saveFileAs = saveFileAs.replace(/[/\\?%*:|"<>)]/g, '');
59-
console.log("Downloading " + saveFileAs);
60-
chrome.downloads.download({
61-
url: getDownloadLink(downloadable, downloadHD),
62-
filename: "Echo360_Lectures/" + unitCode + "/" + saveFileAs
63-
}, function callback(downloadId){
64-
console.log(downloadId);
65-
var currentDownload = {
66-
id: downloadId
67-
}
68-
chrome.downloads.search(currentDownload, function test(result){
69-
console.log(result[0]);
70-
})
71-
}
72-
);
73-
});
74-
});
47+
toDownload.forEach((downloadable) => {
48+
getDownloadLink(downloadable, echo360Domain, downloadHD)
49+
.then((downloadArray) => {
50+
if (!downloadArray)
51+
{
52+
return;
53+
}
54+
55+
downloadArray.forEach((downloadData) => {
56+
chrome.downloads.download({
57+
url: downloadData.url,
58+
filename: `Echo360_Lectures/${courseName}/${downloadData.lessonName}/${downloadData.videoName}`
59+
})
60+
})
61+
});
62+
});
63+
});
7564
})

0 commit comments

Comments
 (0)