From 2ee2072dca1b96b309f984658b9efad12d23b5f1 Mon Sep 17 00:00:00 2001 From: Theenoro Date: Mon, 7 Aug 2017 23:27:50 +0200 Subject: [PATCH] 1.1.0 added a fully functional playlist function --- .gitignore | 2 +- app/js/app.js | 33 +++++++++++--- controller/youtube-dl.js | 93 ++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 controller/youtube-dl.js 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); - $( '
...

'+YT.title+'

Author: '+YT.author_name+'
0%
').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); + $( '
...

'+YT.title+'

Playlistauthor: '+YT.author_name+'
0%
').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); + $( '
...

'+YT.title+'

Author: '+YT.author_name+'
0%
').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" },