This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
forked from eliranmoyal/electron-youtube-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.js
161 lines (147 loc) · 5.12 KB
/
runner.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var spawn = require('child_process').spawn;
var dataOfDownload = {};
function Row(rowString){
if (rowString.indexOf("[") == -1) {
this.type = "Unknown";
return;
};
//splits '[a]b' to ['a','b']
var rowParts = rowString.split(/[\[+\]]/).filter(s=>s.length>1);
this.type = rowParts[0].trim();
this.value = rowParts[1].trim();
}
function splitAndDisplayLog (chunk) {
chunkString = chunk.toString();
var rowArray = splitLogToRows(chunkString);
rowArray.forEach(function(row) {
switch(row.type) {
case "youtube:playlist":
parseYouTubePlayList(row.value);
break;
case "download":
parseDownload(row.value);
break;
case "ffmpeg":
parseffmpeg(row.value);
break;
case "youtube":
break;
default:
console.log("Recieved Unknown Key:"+row.type);
}
});
}
function splitLogToRows(chunk){
console.log(chunk);
splittedChunk = chunkString.split("\n");
var rowArray = [];
splittedChunk.forEach(function(rowStr) {
if (rowStr.indexOf("Deleting") == 0) {
rowStr = "[ffmpeg]"+rowStr;
}
var splittedRow = rowStr.split("[").filter(s=>s.length>1);
if (splittedRow.length == 1) {
rowArray.push(new Row(rowStr));
}
else {
splittedRow.forEach(r=>rowArray.push(new Row("["+r)));
}
});
console.log(rowArray);
return rowArray;
}
function parseffmpeg (log) {
if(log.indexOf("Destination") == 0){
$("#converting").html("Converting to mp3...");
}
else {
$("#converting").html("Finished converting! saving the file..");
}
}
function parseDownload (log) {
if(log.indexOf("Downloading playlist:")==0){
playListName = log.substring("Downloading playlist:".length+1,log.length-1);
dataOfDownload["playlist"]["name"] = playListName;
$('#listStatus').html("Start download playlist " + playListName);
return;
}
if(log.indexOf("Downloading video ") == 0){
//[download] Downloading video 1 of 26
videoNumber = log.substr("Downloading video ".length);
videoNumber = videoNumber.substring(0,videoNumber.indexOf(" "));
$("#downloadProgress").hide();
dataOfDownload["current"] = {};
dataOfDownload["current"]["number"] = videoNumber;
$("#videoNumber").html("video number: " + videoNumber);
return;
}
if(log.indexOf("Destination: ") == 0){
//[download] Destination: Arctic Monkeys - Arabella (Official Audio)-Jn6-TItCazo.m4a
if (dataOfDownload["current"] == undefined) {
dataOfDownload["current"] = {};
$("#downloadProgress").hide();
}
videoName = log.substr("Destination: ".length);
dataOfDownload["current"]["name"] = videoName;
$("#videoName").html("video name:" + videoName);
}
if(log.indexOf("% of")!=-1 && log.indexOf("ETA")!=-1){
//[download] X% of YMiB at ZKiB/s ETA TIME .
//[download] 100% of 6.31MiB in 00:01 "
$("#downloadProgress").show();
$("#downloadProgress").css("display","block");
percentage = log.substring(0,log.indexOf("%"));
dataOfDownload["current"]["percentage"] = percentage;
timeLeft = log.substr(log.indexOf("ETA ") + "ETA ".length);
dataOfDownload["current"]["eta"] = timeLeft;
$("#percentage").html(percentage);
$("#percentage").width(percentage+"%");
$("#ETA").html(timeLeft);
return;
}
}
function parseYouTubePlayList(log){
//start message
if(log.indexOf("add --no-playlist to") != -1){
//take name from message
//Downloading playlist RDJn6-TItCazo - add --no-playlist to just download video
afterPlayList = log.substr(log.indexOf("playlist") + "playlist".length+1);
space = afterPlayList.indexOf(" ");
playListCode = afterPlayList.substring(0,space);
dataOfDownload["playlist"] = {"code": playListCode};
$('#listStatus').append("Analyzing playlist " + playListCode);
return;
}
//second message
if(log.indexOf(dataOfDownload["playlist"]["code"]) == 0){
playListType = log.substr(log.indexOf("Downloading") + "Downloading ".length);
dataOfDownload["playlist"]["type"] = playListType;
$('#listStatus').html("playlist:" + dataOfDownload["playlist"]["code"] + " is " +playListType);
return;
}
if (log.indexOf("playlist " + dataOfDownload["playlist"]["name"]) == 0){
//[youtube:playlist] playlist Mix - Arctic Monkeys - Arabella (Official Audio): Collected 26 video ids (downloading 26 of them)
numOfSongs = log.substr("playlist ".length + dataOfDownload["playlist"]["name"].length + ": Collected ".length);
numOfSongs = numOfSongs.substring(0,numOfSongs.indexOf(" video"));
dataOfDownload["playlist"]["numOfSongs"] = numOfSongs;
$('#listStatus').append(" (" + numOfSongs +" videos)");
}
}
// $('.openDownload').on('click', function (e) {
// var $file = $()shell.openItem(fullPath)
// })
$('#addDownload').on('click', function (e) {
var link = document.querySelectorAll('#downloadUrl')[0].value;
var options = ['--no-check-certificate', '--output=./videos/%(title)s-%(extractor_key)s%(id)s.%(ext)s', link];
var child = spawn('youtube-dl', options);
child.stdout.on('data', function(chunk) {
// reset form
$('#downloadUrl').val('')
//todo: process the logs and do stuff according to regex..
//$('#progress').append(chunk+"</br><br>");
splitAndDisplayLog(chunk);
});
child.stderr.on('data', function (data) {
console.log('ERROR: ' + data);
});
});