-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
111 lines (93 loc) · 3.26 KB
/
background.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const UniLink = ["https://opencast-present.tu-braunschweig.de/search/episode.json?id=",
"https://opencast03.zmml.uni-bremen.de/search/episode.json?id="];
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.vidId != null){
if(request.download != null){
getLink(request.UniId, request.vidId, request.vidForm, sendResponse, true);
}
else{
getLink(request.UniId, request.vidId, request.vidForm, sendResponse, false);
}
}
return true;
}
);
function parseJson(res, vidForm, sendResponse, download){
var resp = sendResponse;
var HQvideo = 0;
var HQvideoTwo = null;
var maxResolution = 0;
var videoList = res["search-results"]["result"]["mediapackage"]["media"]["track"];
for(i =0; i < videoList.length; i++){
if(videoList[i]["video"] == null){
console.log("no resulution");
break;
}
var resolution = parseInt(videoList[i]["video"]["resolution"].split("x")[0]);
if (resolution > maxResolution){
maxResolution = resolution;
HQvideo = i;
HQvideoTwo = null;
}
else{
if(resolution == maxResolution){
HQvideoTwo = i;
}
}
}
bitrateVid: if(HQvideoTwo){
var bitrate = res["search-results"]["result"]["mediapackage"]["media"]["track"][HQvideo]["video"]["bitrate"];
var bitrateTwo = res["search-results"]["result"]["mediapackage"]["media"]["track"][HQvideoTwo]["video"]["bitrate"];
let blackBitrate = 35000; //if video is blackscrean bitrate < blackBitrate => just download other video
if(bitrate < blackBitrate){
HQvideo = HQvideoTwo;
break bitrateVid;
}
if(bitrateTwo < blackBitrate){
break bitrateVid;
}
if(bitrate > bitrateTwo){ //standart video is video with lower bitrate
let store = HQvideo;
HQvideo = HQvideoTwo;
HQvideoTwo = store;
}
if(vidForm == "webcam"){ //if webcam download other video
HQvideo = HQvideoTwo;
}
}
if(res["search-results"]["result"]["mediapackage"]["title"] != null){
var name = res["search-results"]["result"]["mediapackage"]["title"];
}else{
var name = generateFileName();
console.log("did not get fileName");
}
var url = res["search-results"]["result"]["mediapackage"]["media"]["track"][HQvideo]["url"];
if (name.length <=1){
name = generateFileName();
}
var fileName = name.replaceAll(" - ","-").replaceAll("/","-").replaceAll(/[?%*:;,|"]/g, "").replaceAll(/[\\.<> ]/g, "_").replaceAll("__","_")+ ".mp4";
if(download){
console.log("started download "+fileName );
chrome.downloads.download({
url: url,
filename: fileName
});
}
resp({videoUrl: url, videoName: fileName});
}
async function getLink(UniId, vidId, vidForm, sendResponse, download){
jsonUrl = UniLink[UniId] + vidId;
fetch(jsonUrl).then(res => res.json()).then(data => parseJson(data, vidForm, sendResponse, download));
}
function generateFileName(){
var date = new Date();
var year = String(date.getFullYear());
var month = String(date.getMonth()+1);
if (month.length != 2){month = "0"+month;}
var day = String(date.getDate());
var hour = String(date.getHours());
var min = String(date.getMinutes());
var sec = String(date.getSeconds());
return "UniVideo_"+year+"_"+month+"-"+day+"_"+hour+"-"+min+"-"+sec;
}