added a fully functional playlist function
This commit is contained in:
Theenoro 2017-08-07 23:27:50 +02:00
parent 7474e78e92
commit 2ee2072dca
4 changed files with 121 additions and 8 deletions

2
.gitignore vendored
View File

@ -61,4 +61,4 @@ typings/
ffmpeg*
ffplay*
ffprobe*
youtube-dl*
lib/youtube-dl*

View File

@ -35,12 +35,31 @@ $(function(){
if(ampersandPosition != -1) {
video_id = video_id.substring(0, ampersandPosition);
}
request
.get('https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v='+ video_id+'&format=json', function(err,httpResponse,body){
var YT = JSON.parse(body);
$( '<div class="media" ><div class="media-left"><a href="#"><img width="128" class="media-object" src="'+YT.thumbnail_url +'" alt="..."></a></div><div class="media-body" id="body-'+video_id+'"><h4 class="media-heading">'+YT.title+'</h4> Author: '+YT.author_name+'<div class="progress"><div class="progress-bar" id="download-progress-'+video_id+'" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 00%;">0%</div></div></div></div>').prependTo('#output');
cache[video_id] = YT.title;
})
ipcRenderer.send('start-download', {url:$('#url').val(),id:video_id});
var playlist_id = $('#url').val().split('list=')[1];
if(typeof playlist_id !== 'undefined'){
var playlist_idPOS = playlist_id.indexOf('&');
if(playlist_idPOS != -1) {
playlist_id = playlist_id.substring(0, playlist_idPOS);
}
request
.get('https://www.youtube.com/oembed?url=https://www.youtube.com/playlist?list='+ playlist_id+'&format=json', function(err,httpResponse,body){
var YT = JSON.parse(body);
$( '<div class="media" ><div class="media-left"><a href="#"><img id="img-'+video_id+'" width="128" class="media-object" src="'+YT.thumbnail_url +'" alt="..."></a></div><div class="media-body" id="body-'+video_id+'"><h4 class="media-heading">'+YT.title+'</h4> Playlistauthor: '+YT.author_name+'<div class="progress"><div class="progress-bar" id="download-progress-'+video_id+'" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 00%;">0%</div></div></div></div>').prependTo('#output');
cache[video_id] = YT.title;
ipcRenderer.send('start-download-pl', {url:$('#url').val(),id:video_id,folder:YT.author_name+'-'+YT.title});
})
}else{
request
.get('https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v='+ video_id+'&format=json', function(err,httpResponse,body){
var YT = JSON.parse(body);
$( '<div class="media" ><div class="media-left"><a href="#"><img id="img-'+video_id+'" width="128" class="media-object" src="'+YT.thumbnail_url +'" alt="..."></a></div><div class="media-body" id="body-'+video_id+'"><h4 class="media-heading">'+YT.title+'</h4> Author: '+YT.author_name+'<div class="progress"><div class="progress-bar" id="download-progress-'+video_id+'" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 00%;">0%</div></div></div></div>').prependTo('#output');
cache[video_id] = YT.title;
ipcRenderer.send('start-download', {url:$('#url').val(),id:video_id});
})
}
console.log(playlist_id+' id:'+video_id)
})
})

93
controller/youtube-dl.js Normal file
View File

@ -0,0 +1,93 @@
const { spawn } = require('child_process');
const ipcMain = require('electron').ipcMain;
console.log(global.dir);
var orig_path = "";
var path = "";
ipcMain
.on('start-download', (event, arg)=>{
path = orig_path
var download = new yt_dl(arg.url,arg.id);
download.addListen(event)
download.download();
})
.on('start-download-pl', (event, arg)=>{
var mkdirp = require('mkdirp');
if(orig_path === ""){
path = '.'
}else{
path = orig_path
}
mkdirp(path+'/'+arg.folder, function(err) {
path = path+'/'+arg.folder;
// path exists unless there was an error
var download = new yt_dl(arg.url,arg.id);
download.addListen(event)
download.download();
});
})
ipcMain.on('setPath', function(event, arg) {
path = arg.path;
orig_path = arg.path;
console.log('SET Path: '+path)
});
var yt_dl = class{
constructor(url,id){
this.url = url ;
this.percent = 0;
this.lwrite = null;
this.id = id;
}
addListen(e){
this.lwrite = e
}
status(stat){
this.lwrite('download-progress', stat);
}
download(){
var log = [];
var me = this;
var ls;
if(path == ''){
ls = spawn(global.dir+'/lib/youtube-dl', ['-x','--audio-format','mp3','-i',this.url]);
}else{
ls = spawn(global.dir+'/lib/youtube-dl', ['-x','--audio-format','mp3','-i',this.url,'-o',path+'/%(title)s.%(ext)s']);
}
ls.stdout.on('data', (data) => {
data = data.toString('utf8');
log.push(data);
//console.log(data);
try{
var z = data.replace(/(\r\n|\n|\r)/gm,"").split(' ');
console.log(z)
switch (z[0]) {
case '[download]':
var percent = data.split(']')[1].split('of')[0];
//console.log(z)
me.percent = percent.trim();
me.lwrite.sender.send('download-progress',{percent :me.percent,id:me.id});
break;
default:
}
}catch(e){
console.log(e);
}finally{
}
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
me.lwrite.sender.send('file',{id:me.id,file:log[log.length-2].split('[ffmpeg] Destination: ')[1]});
me.lwrite.sender.send('process-fin',{percent :me.percent,id:me.id});
});
}
}
module.exports = yt_dl;

View File

@ -5,6 +5,7 @@
"main": "index.js",
"dependencies": {
"electron": "^1.6.11",
"mkdirp": "^0.5.1",
"request": "^2.81.0",
"youtube-dl": "^1.11.1"
},