From 1d5490c0ff9195bfcb8acf778cee6e17e3509e38 Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Tue, 12 Jan 2021 22:08:42 -0500 Subject: [PATCH 1/3] Allows playlists to be categorized based on the first video that matches --- backend/app.js | 2 +- backend/appdata/default.json | 3 ++- backend/categories.js | 30 ++++++++++++++---------- backend/config.js | 3 ++- backend/consts.js | 4 ++++ src/app/settings/settings.component.html | 5 +++- src/assets/default.json | 3 ++- 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/backend/app.js b/backend/app.js index 04edd6a..1a65d47 100644 --- a/backend/app.js +++ b/backend/app.js @@ -1133,7 +1133,7 @@ async function downloadFileByURL_exec(url, type, options, sessionID = null) { return; } else if (info) { // check if it fits into a category. If so, then get info again using new downloadConfig - if (!Array.isArray(info)) category = await categories_api.categorize(info); + if (!Array.isArray(info) || config_api.getConfigItem('ytdl_allow_playlist_categorization')) category = await categories_api.categorize(info); // set custom output if the category has one and re-retrieve info so the download manager has the right file name if (category && category['custom_output']) { diff --git a/backend/appdata/default.json b/backend/appdata/default.json index dca9eee..5bd5c54 100644 --- a/backend/appdata/default.json +++ b/backend/appdata/default.json @@ -20,7 +20,8 @@ "allow_quality_select": true, "download_only_mode": false, "allow_multi_download_mode": true, - "enable_downloads_manager": true + "enable_downloads_manager": true, + "allow_playlist_categorization": true }, "API": { "use_API_key": false, diff --git a/backend/categories.js b/backend/categories.js index d0b249a..d2af431 100644 --- a/backend/categories.js +++ b/backend/categories.js @@ -33,27 +33,31 @@ Rules: */ -async function categorize(file_json) { +async function categorize(file_jsons) { + // to make the logic easier, let's assume the file metadata is an array + if (!Array.isArray(file_jsons)) file_jsons = [file_jsons]; + let selected_category = null; const categories = getCategories(); if (!categories) { logger.warn('Categories could not be found. Initializing categories...'); db.assign({categories: []}).write(); return null; - return; } - for (let i = 0; i < categories.length; i++) { - const category = categories[i]; - const rules = category['rules']; - - // if rules for current category apply, then that is the selected category - if (applyCategoryRules(file_json, rules, category['name'])) { - selected_category = category; - logger.verbose(`Selected category ${category['name']} for ${file_json['webpage_url']}`); - return selected_category; - } - } + file_jsons.forEach(file_json => { + categories.forEach(category => { + const rules = category['rules']; + + // if rules for current category apply, then that is the selected category + if (applyCategoryRules(file_json, rules, category['name'])) { + selected_category = category; + logger.verbose(`Selected category ${category['name']} for ${file_json['webpage_url']}`); + return selected_category; + } + }); + }); + return selected_category; } diff --git a/backend/config.js b/backend/config.js index 4790e34..cb3e8b3 100644 --- a/backend/config.js +++ b/backend/config.js @@ -197,7 +197,8 @@ DEFAULT_CONFIG = { "allow_quality_select": true, "download_only_mode": false, "allow_multi_download_mode": true, - "enable_downloads_manager": true + "enable_downloads_manager": true, + "allow_playlist_categorization": true }, "API": { "use_API_key": false, diff --git a/backend/consts.js b/backend/consts.js index fa14171..fc29fcf 100644 --- a/backend/consts.js +++ b/backend/consts.js @@ -68,6 +68,10 @@ let CONFIG_ITEMS = { 'key': 'ytdl_enable_downloads_manager', 'path': 'YoutubeDLMaterial.Extra.enable_downloads_manager' }, + 'ytdl_allow_playlist_categorization': { + 'key': 'ytdl_allow_playlist_categorization', + 'path': 'YoutubeDLMaterial.Extra.allow_playlist_categorization' + }, // API 'ytdl_use_api_key': { diff --git a/src/app/settings/settings.component.html b/src/app/settings/settings.component.html index bd085b1..a7d69d6 100644 --- a/src/app/settings/settings.component.html +++ b/src/app/settings/settings.component.html @@ -140,7 +140,7 @@
-
+
Categories
@@ -154,6 +154,9 @@
+
+ Allow playlist categorization +
diff --git a/src/assets/default.json b/src/assets/default.json index 532b32e..ce2cdb3 100644 --- a/src/assets/default.json +++ b/src/assets/default.json @@ -20,7 +20,8 @@ "download_only_mode": false, "allow_multi_download_mode": true, "settings_pin_required": false, - "enable_downloads_manager": true + "enable_downloads_manager": true, + "allow_playlist_categorization": true }, "API": { "use_API_key": false, From d7d861ef0e504f6dc100fe2bd85f1fd5d1691d3b Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Tue, 12 Jan 2021 22:32:27 -0500 Subject: [PATCH 2/3] Fixed typo in default custom output key for categories --- backend/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app.js b/backend/app.js index 1a65d47..9dbad30 100644 --- a/backend/app.js +++ b/backend/app.js @@ -2244,7 +2244,7 @@ app.post('/api/createCategory', optionalJwt, async (req, res) => { name: name, uid: uuid(), rules: [], - custom_putput: '' + custom_output: '' }; db.get('categories').push(new_category).write(); From 6481102e01ab96e1326e70a0e9adb77632e74123 Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Wed, 13 Jan 2021 16:12:11 -0500 Subject: [PATCH 3/3] Changes forEach loops in categorize() to regular for loops to facilitate early breaking --- backend/categories.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/categories.js b/backend/categories.js index d2af431..d4b19f5 100644 --- a/backend/categories.js +++ b/backend/categories.js @@ -45,8 +45,10 @@ async function categorize(file_jsons) { return null; } - file_jsons.forEach(file_json => { - categories.forEach(category => { + for (let i = 0; i < file_jsons.length; i++) { + const file_json = file_jsons[i]; + for (let j = 0; j < categories.length; j++) { + const category = categories[i]; const rules = category['rules']; // if rules for current category apply, then that is the selected category @@ -55,8 +57,8 @@ async function categorize(file_jsons) { logger.verbose(`Selected category ${category['name']} for ${file_json['webpage_url']}`); return selected_category; } - }); - }); + } + } return selected_category; }