From 3446c180aaea13160a17d321e9c7c740b9dbd900 Mon Sep 17 00:00:00 2001 From: rlauu <46294892+rlauu@users.noreply.github.com> Date: Sat, 30 Mar 2024 15:36:15 +0100 Subject: [PATCH] Merge Scheduled Tasks --- .../ScheduledTasks/DetectCreditsTask.cs | 127 ------------------ ...sTask.cs => DetectIntrosAndCreditsTask.cs} | 72 ++++------ 2 files changed, 28 insertions(+), 171 deletions(-) delete mode 100644 ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs rename ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/{DetectIntroductionsTask.cs => DetectIntrosAndCreditsTask.cs} (55%) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs deleted file mode 100644 index bef0547..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Controller.Library; -using MediaBrowser.Model.Tasks; -using Microsoft.Extensions.Logging; - -namespace ConfusedPolarBear.Plugin.IntroSkipper; - -/// -/// Analyze all television episodes for credits. -/// TODO: analyze all media files. -/// -public class DetectCreditsTask : IScheduledTask -{ - private readonly ILogger _logger; - - private readonly ILoggerFactory _loggerFactory; - - private readonly ILibraryManager _libraryManager; - - /// - /// Initializes a new instance of the class. - /// - /// Logger factory. - /// Library manager. - /// Logger. - public DetectCreditsTask( - ILogger logger, - ILoggerFactory loggerFactory, - ILibraryManager libraryManager) - { - _logger = logger; - _loggerFactory = loggerFactory; - _libraryManager = libraryManager; - } - - /// - /// Gets the task name. - /// - public string Name => "Detect Credits"; - - /// - /// Gets the task category. - /// - public string Category => "Intro Skipper"; - - /// - /// Gets the task description. - /// - public string Description => "Analyzes the audio and video of all television episodes to find credits."; - - /// - /// Gets the task key. - /// - public string Key => "CPBIntroSkipperDetectCredits"; - - /// - /// Analyze all episodes in the queue. Only one instance of this task should be run at a time. - /// - /// Task progress. - /// Cancellation token. - /// Task. - public Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken) - { - if (_libraryManager is null) - { - throw new InvalidOperationException("Library manager was null"); - } - - // Wait for running analyzer - if (Plugin.Instance!.AnalyzerTaskIsRunning) - { - _logger.LogInformation("Other running Analyzer Task detected. Wait..."); - using (var timer = new Timer(_ => { }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan)) - { - try - { - void DisposeTimerOnCancellation() => timer.Dispose(); - cancellationToken.Register(DisposeTimerOnCancellation); - while (Plugin.Instance!.AnalyzerTaskIsRunning && !cancellationToken.IsCancellationRequested) - { - timer.Change(TimeSpan.FromMilliseconds(20000), Timeout.InfiniteTimeSpan); // Adjust delay - } - } - catch (OperationCanceledException) - { - return Task.CompletedTask; - } - } - - if (!cancellationToken.IsCancellationRequested) // Check cancellation again before logging - { - _logger.LogInformation("No other Task active. Run Analyzer Task"); - } - else - { - _logger.LogInformation("Task was canceled"); - return Task.CompletedTask; - } - } - - Plugin.Instance!.AnalyzerTaskIsRunning = true; - - var baseAnalyzer = new BaseItemAnalyzerTask( - AnalysisMode.Credits, - _loggerFactory.CreateLogger(), - _loggerFactory, - _libraryManager); - - baseAnalyzer.AnalyzeItems(progress, cancellationToken); - - Plugin.Instance!.AnalyzerTaskIsRunning = false; - - return Task.CompletedTask; - } - - /// - /// Get task triggers. - /// - /// Task triggers. - public IEnumerable GetDefaultTriggers() - { - return Array.Empty(); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntroductionsTask.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosAndCreditsTask.cs similarity index 55% rename from ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntroductionsTask.cs rename to ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosAndCreditsTask.cs index 3499017..d9f43ca 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntroductionsTask.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosAndCreditsTask.cs @@ -11,26 +11,21 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper; /// /// Analyze all television episodes for introduction sequences. /// -public class DetectIntroductionsTask : IScheduledTask +public class DetectIntrosAndCreditsTask : IScheduledTask { - private readonly ILogger _logger; - private readonly ILoggerFactory _loggerFactory; private readonly ILibraryManager _libraryManager; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// Logger factory. /// Library manager. - /// Logger. - public DetectIntroductionsTask( - ILogger logger, + public DetectIntrosAndCreditsTask( ILoggerFactory loggerFactory, ILibraryManager libraryManager) { - _logger = logger; _loggerFactory = loggerFactory; _libraryManager = libraryManager; } @@ -38,7 +33,7 @@ public class DetectIntroductionsTask : IScheduledTask /// /// Gets the task name. /// - public string Name => "Detect Introductions"; + public string Name => "Detect Introductions and Credits"; /// /// Gets the task category. @@ -48,12 +43,12 @@ public class DetectIntroductionsTask : IScheduledTask /// /// Gets the task description. /// - public string Description => "Analyzes the audio of all television episodes to find introduction sequences."; + public string Description => "Analyzes the audio of all television episodes to find introduction and credit sequences."; /// /// Gets the task key. /// - public string Key => "CPBIntroSkipperDetectIntroductions"; + public string Key => "CPBIntroSkipperDetectIntrosAndCredits"; /// /// Analyze all episodes in the queue. Only one instance of this task should be run at a time. @@ -68,47 +63,36 @@ public class DetectIntroductionsTask : IScheduledTask throw new InvalidOperationException("Library manager was null"); } - // Wait for running analyzer + // abort if analyzer is already running if (Plugin.Instance!.AnalyzerTaskIsRunning) { - _logger.LogInformation("Other running Analyzer Task detected. Wait..."); - using (var timer = new Timer(_ => { }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan)) - { - try - { - void DisposeTimerOnCancellation() => timer.Dispose(); - cancellationToken.Register(DisposeTimerOnCancellation); - while (Plugin.Instance!.AnalyzerTaskIsRunning && !cancellationToken.IsCancellationRequested) - { - timer.Change(TimeSpan.FromMilliseconds(20000), Timeout.InfiniteTimeSpan); // Adjust delay - } - } - catch (OperationCanceledException) - { - return Task.CompletedTask; - } - } - - if (!cancellationToken.IsCancellationRequested) // Check cancellation again before logging - { - _logger.LogInformation("No other Task active. Run Analyzer Task"); - } - else - { - _logger.LogInformation("Task was canceled"); - return Task.CompletedTask; - } + return Task.CompletedTask; + } + else + { + Plugin.Instance!.AnalyzerTaskIsRunning = true; } - Plugin.Instance!.AnalyzerTaskIsRunning = true; - - var baseAnalyzer = new BaseItemAnalyzerTask( + // intro + var baseIntroAnalyzer = new BaseItemAnalyzerTask( AnalysisMode.Introduction, - _loggerFactory.CreateLogger(), + _loggerFactory.CreateLogger(), _loggerFactory, _libraryManager); - baseAnalyzer.AnalyzeItems(progress, cancellationToken); + baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken); + + // reset progress + progress.Report(0); + + // outro + var baseCreditAnalyzer = new BaseItemAnalyzerTask( + AnalysisMode.Credits, + _loggerFactory.CreateLogger(), + _loggerFactory, + _libraryManager); + + baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken); Plugin.Instance!.AnalyzerTaskIsRunning = false;