From 3a86d5c636c55a2c1b1f3ffc7f996ccb693dd3dd Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Mon, 11 Sep 2023 15:33:37 -0400 Subject: [PATCH] Removed node-youtube-dl dependency --- backend/app.js | 2 -- backend/package.json | 3 +-- backend/subscriptions.js | 33 +++++++++++++++------------------ backend/youtube-dl.js | 9 +++++++-- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/backend/app.js b/backend/app.js index a8c115c..eb8c886 100644 --- a/backend/app.js +++ b/backend/app.js @@ -23,8 +23,6 @@ const session = require('express-session'); // needed if bin/details somehow gets deleted if (!fs.existsSync(CONSTS.DETAILS_BIN_PATH)) fs.writeJSONSync(CONSTS.DETAILS_BIN_PATH, {"version":"2000.06.06","path":"node_modules\\youtube-dl\\bin\\youtube-dl.exe","exec":"youtube-dl.exe","downloader":"youtube-dl"}) -const youtubedl = require('youtube-dl'); - const logger = require('./logger'); const config_api = require('./config.js'); const downloader_api = require('./downloader'); diff --git a/backend/package.json b/backend/package.json index 3ca4270..70b5cb3 100644 --- a/backend/package.json +++ b/backend/package.json @@ -65,7 +65,6 @@ "unzipper": "^0.10.10", "uuidv4": "^6.2.13", "winston": "^3.7.2", - "xmlbuilder2": "^3.0.2", - "youtube-dl": "^3.0.2" + "xmlbuilder2": "^3.0.2" } } diff --git a/backend/subscriptions.js b/backend/subscriptions.js index 8085f26..58211b4 100644 --- a/backend/subscriptions.js +++ b/backend/subscriptions.js @@ -1,6 +1,5 @@ const fs = require('fs-extra'); const path = require('path'); -const youtubedl = require('youtube-dl'); const youtubedl_api = require('./youtube-dl'); const config_api = require('./config'); @@ -475,23 +474,21 @@ async function checkVideoIfBetterExists(file_obj, sub, user_uid) { const downloadConfig = await generateArgsForSubscription(sub, user_uid, true, new_path); logger.verbose(`Checking if a better version of the fresh upload ${file_obj['id']} exists.`); // simulate a download to verify that a better version exists - youtubedl.getInfo(file_obj['url'], downloadConfig, async (err, output) => { - if (err) { - // video is not available anymore for whatever reason - } else if (output) { - const metric_to_compare = sub.type === 'audio' ? 'abr' : 'height'; - if (output[metric_to_compare] > file_obj[metric_to_compare]) { - // download new video as the simulated one is better - const {parsed_output, err} = await youtubedl_api.runYoutubeDLMain(sub.url, downloadConfig); - if (err) { - logger.verbose(`Failed to download better version of video ${file_obj['id']}`); - } else if (parsed_output) { - logger.verbose(`Successfully upgraded video ${file_obj['id']}'s ${metric_to_compare} from ${file_obj[metric_to_compare]} to ${output[metric_to_compare]}`); - await db_api.setVideoProperty(file_obj['uid'], {[metric_to_compare]: output[metric_to_compare]}); - } - } - } - }); + + const info = await downloader_api.getVideoInfoByURL(file_obj['url'], downloadConfig); + if (info && info.length === 1) { + const metric_to_compare = sub.type === 'audio' ? 'abr' : 'height'; + if (info[metric_to_compare] > file_obj[metric_to_compare]) { + // download new video as the simulated one is better + const {parsed_output, err} = await youtubedl_api.runYoutubeDLMain(sub.url, downloadConfig); + if (err) { + logger.verbose(`Failed to download better version of video ${file_obj['id']}`); + } else if (parsed_output) { + logger.verbose(`Successfully upgraded video ${file_obj['id']}'s ${metric_to_compare} from ${file_obj[metric_to_compare]} to ${info[metric_to_compare]}`); + await db_api.setVideoProperty(file_obj['uid'], {[metric_to_compare]: info[metric_to_compare]}); + } + } + } await db_api.setVideoProperty(file_obj['uid'], {'fresh_upload': false}); } diff --git a/backend/youtube-dl.js b/backend/youtube-dl.js index 46177d8..5d55688 100644 --- a/backend/youtube-dl.js +++ b/backend/youtube-dl.js @@ -7,7 +7,6 @@ const logger = require('./logger'); const utils = require('./utils'); const CONSTS = require('./consts'); const config_api = require('./config.js'); -const youtubedl = require('youtube-dl'); const is_windows = process.platform === 'win32'; @@ -39,7 +38,7 @@ exports.runYoutubeDL = async (url, args, downloadMethod = null) => { } // Run youtube-dl in a main thread (with possible downloadMethod) -exports.runYoutubeDLMain = async (url, args, downloadMethod = youtubedl.exec) => { +exports.runYoutubeDLMain = async (url, args, downloadMethod = defaultDownloadMethod) => { return new Promise(resolve => { downloadMethod(url, args, {maxBuffer: Infinity}, async function(err, output) { const parsed_output = utils.parseOutputJSON(output, err); @@ -68,6 +67,12 @@ async function getYoutubeDLPath() { return guessed_base_path + 'youtube-dl' + (is_windows ? '.exe' : ''); } +async function defaultDownloadMethod(url, args, options, callback) { + const {child_process, _} = await runYoutubeDLProcess(url, args); + const {stdout, stderr} = await child_process; + return await callback(stderr, stdout.trim().split(/\r?\n/)); +} + exports.killYoutubeDLProcess = async (child_process) => { kill(child_process.pid, 'SIGKILL'); }