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 introduction sequences. /// public class DetectIntroductionsTask : 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 DetectIntroductionsTask( ILogger logger, ILoggerFactory loggerFactory, ILibraryManager libraryManager) { _logger = logger; _loggerFactory = loggerFactory; _libraryManager = libraryManager; } /// /// Gets the task name. /// public string Name => "Detect Introductions"; /// /// Gets the task category. /// public string Category => "Intro Skipper"; /// /// Gets the task description. /// public string Description => "Analyzes the audio of all television episodes to find introduction sequences."; /// /// Gets the task key. /// public string Key => "CPBIntroSkipperDetectIntroductions"; /// /// 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.Introduction, _loggerFactory.CreateLogger(), _loggerFactory, _libraryManager); baseAnalyzer.AnalyzeItems(progress, cancellationToken); Plugin.Instance!.AnalyzerTaskIsRunning = false; return Task.CompletedTask; } /// /// Get task triggers. /// /// Task triggers. public IEnumerable GetDefaultTriggers() { return new[] { new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerDaily, TimeOfDayTicks = TimeSpan.FromHours(0).Ticks } }; } }