diff --git a/.gitignore b/.gitignore index 2648389..12b972d 100644 --- a/.gitignore +++ b/.gitignore @@ -61,4 +61,4 @@ typings/ ffmpeg* ffplay* ffprobe* -youtube-dl* +lib/youtube-dl* diff --git a/app/js/app.js b/app/js/app.js index 4988959..b17f313 100644 --- a/app/js/app.js +++ b/app/js/app.js @@ -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); - $( '
').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); + $( '').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); + $( '').prependTo('#output'); + cache[video_id] = YT.title; + ipcRenderer.send('start-download', {url:$('#url').val(),id:video_id}); + }) + } + console.log(playlist_id+' id:'+video_id) + + }) }) diff --git a/controller/youtube-dl.js b/controller/youtube-dl.js new file mode 100644 index 0000000..1e4e55d --- /dev/null +++ b/controller/youtube-dl.js @@ -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; diff --git a/package.json b/package.json index e19b96a..11357df 100644 --- a/package.json +++ b/package.json @@ -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" },