From bcb5e5ea300c9e4a3efe3c9517d937ab6667e065 Mon Sep 17 00:00:00 2001 From: ConfusedPolarBear <33811686+ConfusedPolarBear@users.noreply.github.com> Date: Wed, 23 Nov 2022 02:34:28 -0600 Subject: [PATCH] Remove dependency on shared media item queue --- .../Controllers/TroubleshootingController.cs | 5 +-- .../Controllers/VisualizationController.cs | 8 ++--- .../Entrypoint.cs | 7 +--- .../Plugin.cs | 4 +-- .../QueueManager.cs | 34 +++++++++++-------- .../ScheduledTasks/DetectCreditsTask.cs | 10 +++--- .../ScheduledTasks/DetectIntroductionsTask.cs | 10 +++--- 7 files changed, 38 insertions(+), 40 deletions(-) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/TroubleshootingController.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/TroubleshootingController.cs index 8923daf..1b1b901 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/TroubleshootingController.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/TroubleshootingController.cs @@ -53,10 +53,7 @@ public ActionResult GetSupportBundle() bundle.Append("* Queue contents: "); bundle.Append(Plugin.Instance!.TotalQueued); - bundle.Append(" episodes, "); - bundle.Append(Plugin.Instance!.AnalysisQueue.Count); - bundle.Append(" seasons"); - bundle.Append('\n'); + bundle.Append(" episodes\n"); bundle.Append(FFmpegWrapper.GetChromaprintLogs()); diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs index f2331ab..daef672 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs @@ -40,7 +40,7 @@ public ActionResult>> GetShowSeasons() var showSeasons = new Dictionary>(); // Loop through all seasons in the analysis queue - foreach (var kvp in Plugin.Instance!.AnalysisQueue) + foreach (var kvp in Plugin.Instance!.QueuedMediaItems) { // Check that this season contains at least one episode. var episodes = kvp.Value; @@ -104,10 +104,8 @@ public ActionResult> GetSeasonEpisodes( [HttpGet("Episode/{Id}/Chromaprint")] public ActionResult GetEpisodeFingerprint([FromRoute] Guid id) { - var queue = Plugin.Instance!.AnalysisQueue; - // Search through all queued episodes to find the requested id - foreach (var season in queue) + foreach (var season in Plugin.Instance!.QueuedMediaItems) { foreach (var needle in season.Value) { @@ -180,7 +178,7 @@ private string GetSeasonName(QueuedEpisode episode) /// Boolean indicating if the requested season was found. private bool LookupSeasonByName(string series, string season, out List episodes) { - foreach (var queuedEpisodes in Plugin.Instance!.AnalysisQueue) + foreach (var queuedEpisodes in Plugin.Instance!.QueuedMediaItems) { var first = queuedEpisodes.Value[0]; var firstSeasonName = GetSeasonName(first); diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Entrypoint.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Entrypoint.cs index 6a5dff2..a7a176a 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Entrypoint.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Entrypoint.cs @@ -57,7 +57,7 @@ public Task RunAsync() try { - // Enqueue all episodes at startup so the fingerprint visualizer works before the task is started. + // Enqueue all episodes at startup to ensure any FFmpeg errors appear as early as possible _logger.LogInformation("Running startup enqueue"); var queueManager = new QueueManager(_loggerFactory.CreateLogger(), _libraryManager); queueManager.EnqueueAllEpisodes(); @@ -67,11 +67,6 @@ public Task RunAsync() _logger.LogError("Unable to run startup enqueue: {Exception}", ex); } - _logger.LogDebug( - "Total enqueued seasons: {Count} ({Episodes} episodes)", - Plugin.Instance!.AnalysisQueue.Count, - Plugin.Instance!.TotalQueued); - return Task.CompletedTask; } diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Plugin.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Plugin.cs index bf5d241..84ea81c 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Plugin.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Plugin.cs @@ -92,9 +92,9 @@ public Plugin( public Dictionary Intros { get; } = new(); /// - /// Gets the mapping of season ids to episodes that have been queued for fingerprinting. + /// Gets the most recent media item queue. /// - public Dictionary> AnalysisQueue { get; } = new(); + public Dictionary> QueuedMediaItems { get; } = new(); /// /// Gets or sets the total number of episodes in the queue. diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs b/ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs index be905e6..4f3308f 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs @@ -2,12 +2,12 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using Jellyfin.Data.Enums; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; -using MediaBrowser.Model.Entities; using Microsoft.Extensions.Logging; /// @@ -19,7 +19,8 @@ public class QueueManager private ILogger _logger; private double analysisPercent; - private IList selectedLibraries; + private List selectedLibraries; + private Dictionary> _queuedEpisodes; /// /// Initializes a new instance of the class. @@ -31,13 +32,15 @@ public QueueManager(ILogger logger, ILibraryManager libraryManager _logger = logger; _libraryManager = libraryManager; - selectedLibraries = new List(); + selectedLibraries = new(); + _queuedEpisodes = new(); } /// /// Iterates through all libraries on the server and queues all episodes for analysis. /// - public void EnqueueAllEpisodes() + /// Queued media items. + public ReadOnlyDictionary> EnqueueAllEpisodes() { // Assert that ffmpeg with chromaprint is installed if (!FFmpegWrapper.CheckFFmpegVersion()) @@ -46,20 +49,13 @@ public void EnqueueAllEpisodes() "ffmpeg with chromaprint is not installed on this system - episodes will not be analyzed. If Jellyfin is running natively, install jellyfin-ffmpeg5. If Jellyfin is running in a container, upgrade it to the latest version of 10.8.0."); } - Plugin.Instance!.AnalysisQueue.Clear(); Plugin.Instance!.TotalQueued = 0; LoadAnalysisSettings(); - // For all selected TV show libraries, enqueue all contained items. + // For all selected libraries, enqueue all contained episodes. foreach (var folder in _libraryManager.GetVirtualFolders()) { - if (folder.CollectionType != CollectionTypeOptions.TvShows) - { - _logger.LogDebug("Not analyzing library \"{Name}\": not a TV show library", folder.Name); - continue; - } - // If libraries have been selected for analysis, ensure this library was selected. if (selectedLibraries.Count > 0 && !selectedLibraries.Contains(folder.Name)) { @@ -81,6 +77,14 @@ public void EnqueueAllEpisodes() _logger.LogError("Failed to enqueue items from library {Name}: {Exception}", folder.Name, ex); } } + + Plugin.Instance!.QueuedMediaItems.Clear(); + foreach (var kvp in _queuedEpisodes) + { + Plugin.Instance!.QueuedMediaItems[kvp.Key] = kvp.Value; + } + + return new(_queuedEpisodes); } /// @@ -156,7 +160,7 @@ private void QueueLibraryContents(string rawId) { if (item is not Episode episode) { - _logger.LogError("Item {Name} is not an episode", item.Name); + _logger.LogDebug("Item {Name} is not an episode", item.Name); continue; } @@ -198,11 +202,11 @@ private void QueueEpisode(Episode episode) 60 * Plugin.Instance!.Configuration.AnalysisLengthLimit); // Allocate a new list for each new season - Plugin.Instance!.AnalysisQueue.TryAdd(episode.SeasonId, new List()); + _queuedEpisodes.TryAdd(episode.SeasonId, new List()); // Queue the episode for analysis var maxCreditsDuration = Plugin.Instance!.Configuration.MaximumEpisodeCreditsDuration * 60; - Plugin.Instance.AnalysisQueue[episode.SeasonId].Add(new QueuedEpisode() + _queuedEpisodes[episode.SeasonId].Add(new QueuedEpisode() { SeriesName = episode.SeriesName, SeasonNumber = episode.AiredSeasonNumber ?? 0, diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs index d4da24e..92f9bbc 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs @@ -84,9 +84,7 @@ public Task ExecuteAsync(IProgress progress, CancellationToken cancellat _loggerFactory.CreateLogger(), _libraryManager); - queueManager.EnqueueAllEpisodes(); - - var queue = Plugin.Instance!.AnalysisQueue; + var queue = queueManager.EnqueueAllEpisodes(); if (queue.Count == 0) { @@ -142,7 +140,11 @@ public Task ExecuteAsync(IProgress progress, CancellationToken cancellat ex); } - progress.Report((totalProcessed * 100) / Plugin.Instance!.TotalQueued); + var total = Plugin.Instance!.TotalQueued; + if (total > 0) + { + progress.Report((totalProcessed * 100) / total); + } }); return Task.CompletedTask; diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntroductionsTask.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntroductionsTask.cs index 416cd80..5b232b8 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntroductionsTask.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntroductionsTask.cs @@ -83,9 +83,7 @@ public Task ExecuteAsync(IProgress progress, CancellationToken cancellat _loggerFactory.CreateLogger(), _libraryManager); - queueManager.EnqueueAllEpisodes(); - - var queue = Plugin.Instance!.AnalysisQueue; + var queue = queueManager.EnqueueAllEpisodes(); if (queue.Count == 0) { @@ -162,7 +160,11 @@ public Task ExecuteAsync(IProgress progress, CancellationToken cancellat EdlManager.UpdateEDLFiles(episodes); } - progress.Report((totalProcessed * 100) / Plugin.Instance!.TotalQueued); + var total = Plugin.Instance!.TotalQueued; + if (total > 0) + { + progress.Report((totalProcessed * 100) / total); + } }); // Turn the regenerate EDL flag off after the scan completes.