From ff73f5dcd4ce72c785bd8a47f741836b7313ffa1 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 8 Jan 2019 18:08:02 +0000 Subject: [PATCH] Queing working, discoverd bug on region locked videos --- src/server.js | 5 +- src/youtubehelper.js | 128 +++++++++++++++++++++++++++---------------- 2 files changed, 81 insertions(+), 52 deletions(-) diff --git a/src/server.js b/src/server.js index 1f0d296..0d241cc 100644 --- a/src/server.js +++ b/src/server.js @@ -39,10 +39,7 @@ module.exports.listen = async () => { socket.on('download', async (data) => { logger.log(`Socket id '${socket.id}' is requesting a download`); let toDownload = await youtube.resolveVideos(data.videos); - if (data.audioOnly) - youtube.downloadAudio(toDownload.data, socket, {path: main.config.downloadLocation}); - else - youtube.downloadVideos(toDownload.data, socket, {path: main.config.downloadLocation}); + youtube.setupDownloadQueue(toDownload.data, socket, {path: main.config.downloadLocation, audioOnly: !data.audioOnly}); }); }); } diff --git a/src/youtubehelper.js b/src/youtubehelper.js index 15100dd..25ea334 100644 --- a/src/youtubehelper.js +++ b/src/youtubehelper.js @@ -8,72 +8,104 @@ module.exports.resolveVideos = async (arr) => { for (let video of arr) { if (video == '' || video == ' ') continue; - if (await ytdl.validateURL(video)) { - const info = await ytdl.getBasicInfo(video); - if (!info) { + try { + if (await ytdl.validateURL(video)) { + const info = await ytdl.getBasicInfo(video); + if (!info) { + output.data[video] = {found: false}; + return; + } + output.data[video] = { + found: true, + title: info.title, + thumbnail: info.thumbnail_url.replace('default', 'maxresdefault'), + runtime: info.length_seconds + } + output.contents = true; + } else { output.data[video] = {found: false}; - return; + output.contents = true; } - output.data[video] = { - found: true, - title: info.title, - thumbnail: info.thumbnail_url.replace('default', 'maxresdefault'), - runtime: info.length_seconds - } - output.contents = true; - } else { + } catch (e) { output.data[video] = {found: false}; output.contents = true; + logger.log(`Error resolving video ${video}`); } } return output; } -module.exports.downloadVideos = async (arr, socket, options) => { - let path = options.path ? options.path : './' - let numOfDownloads = 0; +let downloadQueue; + +module.exports.setupDownloadQueue = async (arr, socket, options) => { + let numOfDownloads; + + downloadQueue[socket.id] = { + count: 0, + videos: { } + } for (const [key, value] of Object.entries(arr)) { if (ytdl.validateURL(key)) { - try { - const stream = await ytdl(key, {quality: 'highest', filter: (format) => format.container === 'mp4'}); - stream.pipe(fs.createWriteStream(`${path}/${(value.title).replace(/\//, '-')}.mp4`)); - stream.on('response', (res) => { - let totalSize = res.headers['content-length']; - let dataRead = 0; - let lastPercent = 0; - res.on('data', (data) => { - dataRead += data.length; - let percent = Math.floor((dataRead / totalSize) * 100) + '%'; - if (percent != lastPercent) { - socket.emit('download-progress', {video: key, percent: percent, title: value.title}); - } - lastPercent = percent; - }); - res.on('end', () => { - logger.log(`Socket id '${socket.id}' finished downloading ${value.title}`) - socket.emit('download-done', {video: key, title: value.title}); - }); - }); - - logger.log(`Socket id '${socket.id}' is downloading ${value.title}`); - } catch (e) { - logger.log(`Socket id '${socket.id}' failed to download ${value.title}`); - socket.emit('download-done', {video: key, title: value.title}); - } numOfDownloads++; + socket.emit('download-progress', {video: key, percent: "Queued", title: value.title}); } } + socket.emit('download-count', {num: numOfDownloads}); -} - -module.exports.downloadAudio = async (arr, socket, options) => { - let path = options.path ? options.path : './' - let numOfDownloads = 0; - + for (const [key, value] of Object.entries(arr)) { - + if (ytdl.validateURL(key)) { + await runQueueAsync(socket.id); + await download(key, value.title, socket, options.audioOnly); + } } } + +async function runQueueAsync(socketID) { + +} + +async function download(video, videoName ,socket, audioOnly, path = './') { + return new Promise(async (resolve, reject) => { + let stream; + + try { + if (audioOnly) { + stream = await ytdl(video, {quality: 'highest', filter: (format) => format.container === 'mp4'}); + stream.pipe(fs.createWriteStream(`${path}/${(videoName).replace(/\//, '-')}.mp4`)); + } else { + stream = await ytdl(video, {quality: 'highest', filter: "audioonly"}); + stream.pipe(fs.createWriteStream(`${path}/${(videoName).replace(/\//, '-')}.mp3`)); + } + + stream.on('response', (res) => { + let totalSize = res.headers['content-length']; + let dataRead = 0; + let lastPercent = 0; + res.on('data', (data) => { + dataRead += data.length; + let percent = Math.floor((dataRead / totalSize) * 100) + '%'; + if (percent != lastPercent) { + socket.emit('download-progress', {video: video, percent: percent, title: videoName}); + } + lastPercent = percent; + }); + + res.on('end', () => { + logger.log(`Socket id '${socket.id}' finished downloading ${videoName}`) + socket.emit('download-done', {video: video, title: videoName}); + resolve('complete'); + }); + + logger.log(`Socket id '${socket.id}' is downloading ${videoName}`); + }); + } catch (e) { + logger.log(`Socket id '${socket.id}' failed to download ${videoName}: ${e}`); + socket.emit('download-done', {video: video, title: videoName}); + resolve('error'); + } + }); +}