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 DetectIntrosCreditsTask : 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 DetectIntrosCreditsTask( ILogger logger, ILoggerFactory loggerFactory, ILibraryManager libraryManager) { _logger = logger; _loggerFactory = loggerFactory; _libraryManager = libraryManager; } /// /// Gets the task name. /// public string Name => "Detect Intros and Credits"; /// /// Gets the task category. /// public string Category => "Intro Skipper"; /// /// Gets the task description. /// public string Description => "Analyzes media to determine the timestamp and length of intros and credits."; /// /// Gets the task key. /// public string Key => "CPBIntroSkipperDetectIntrosCredits"; /// /// 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"); } // abort automatic analyzer if running if (Entrypoint.AutomaticTaskState == TaskState.Running || Entrypoint.AutomaticTaskState == TaskState.Cancelling) { _logger.LogInformation("Automatic Task is {0} and will be canceled.", Entrypoint.AutomaticTaskState); Entrypoint.CancelAutomaticTask(cancellationToken); } ScheduledTaskSemaphore.Wait(-1, cancellationToken); if (cancellationToken.IsCancellationRequested) { ScheduledTaskSemaphore.Release(); return Task.CompletedTask; } _logger.LogInformation("Scheduled Task is starting"); var baseIntroAnalyzer = new BaseItemAnalyzerTask( AnalysisMode.Introduction, _loggerFactory.CreateLogger(), _loggerFactory, _libraryManager); baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken); var baseCreditAnalyzer = new BaseItemAnalyzerTask( AnalysisMode.Credits, _loggerFactory.CreateLogger(), _loggerFactory, _libraryManager); baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken); ScheduledTaskSemaphore.Release(); return Task.CompletedTask; } /// /// Get task triggers. /// /// Task triggers. public IEnumerable GetDefaultTriggers() { return new[] { new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerDaily, TimeOfDayTicks = TimeSpan.FromHours(0).Ticks } }; } }