diff --git a/backend/db.js b/backend/db.js index 16a8d0c..c8ef8fb 100644 --- a/backend/db.js +++ b/backend/db.js @@ -213,6 +213,29 @@ async function importUnregisteredFiles() { } +async function preimportUnregisteredSubscriptionFile(sub, appendedBasePath) { + const preimported_file_paths = []; + + let dbPath = null; + if (sub.user_uid) + dbPath = users_db.get('users').find({uid: sub.user_uid}).get('subscriptions').find({id: sub.id}).get('videos'); + else + dbPath = db.get('subscriptions').find({id: sub.id}).get('videos'); + + const files = await utils.getDownloadedFilesByType(appendedBasePath, sub.type); + files.forEach(file => { + // check if file exists in db, if not add it + const file_is_registered = !!(dbPath.find({id: file.id}).value()) + if (!file_is_registered) { + // add additional info + registerFileDBManual(dbPath, file); + preimported_file_paths.push(file['path']); + logger.verbose(`Preemptively added subscription file to the database: ${file.id}`); + } + }); + return preimported_file_paths; +} + async function getVideo(file_uid, uuid, sub_id) { const base_db_path = uuid ? users_db.get('users').find({uid: uuid}) : db; const sub_db_path = sub_id ? base_db_path.get('subscriptions').find({id: sub_id}).get('videos') : base_db_path.get('files'); @@ -235,6 +258,7 @@ module.exports = { updatePlaylist: updatePlaylist, getFileDirectoriesAndDBs: getFileDirectoriesAndDBs, importUnregisteredFiles: importUnregisteredFiles, + preimportUnregisteredSubscriptionFile: preimportUnregisteredSubscriptionFile, getVideo: getVideo, setVideoProperty: setVideoProperty } diff --git a/backend/subscriptions.js b/backend/subscriptions.js index 3a6e0a3..7ff1d06 100644 --- a/backend/subscriptions.js +++ b/backend/subscriptions.js @@ -277,6 +277,7 @@ async function getVideosForSub(sub, user_uid = null) { basePath = config_api.getConfigItem('ytdl_subscriptions_base_path'); let appendedBasePath = getAppendedBasePath(sub, basePath); + fs.ensureDirSync(appendedBasePath); let multiUserMode = null; if (user_uid) { @@ -292,8 +293,17 @@ async function getVideosForSub(sub, user_uid = null) { logger.verbose('Subscription: getting videos for subscription ' + sub.name); return new Promise(resolve => { + const preimported_file_paths = []; + const PREIMPORT_INTERVAL = 5000; + const preregister_check = setInterval(() => { + if (sub.streamingOnly) return; + db_api.preimportUnregisteredSubscriptionFile(sub, appendedBasePath); + }, PREIMPORT_INTERVAL); youtubedl.exec(sub.url, downloadConfig, {maxBuffer: Infinity}, async function(err, output) { + // cleanup updateSubscriptionProperty(sub, {downloading: false}, user_uid); + clearInterval(preregister_check); + logger.verbose('Subscription: finished check for ' + sub.name); if (err && !output) { logger.error(err.stderr ? err.stderr : err.message); @@ -337,7 +347,7 @@ async function getVideosForSub(sub, user_uid = null) { } const reset_videos = i === 0; - handleOutputJSON(sub, sub_db, output_json, multiUserMode, reset_videos); + handleOutputJSON(sub, sub_db, output_json, multiUserMode, preimported_file_paths, reset_videos); } if (config_api.getConfigItem('ytdl_subscriptions_redownload_fresh_uploads')) { @@ -351,6 +361,7 @@ async function getVideosForSub(sub, user_uid = null) { }, err => { logger.error(err); updateSubscriptionProperty(sub, {downloading: false}, user_uid); + clearInterval(preregister_check); }); }