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 DetectIntrosTask : 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 DetectIntrosTask( 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"); } // abort if analyzer is already running if (Plugin.Instance!.AnalyzerTaskIsRunning) { return Task.CompletedTask; } else { Plugin.Instance!.AnalyzerTaskIsRunning = true; } var baseIntroAnalyzer = new BaseItemAnalyzerTask( AnalysisMode.Introduction, _loggerFactory.CreateLogger(), _loggerFactory, _libraryManager); baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken); Plugin.Instance!.AnalyzerTaskIsRunning = false; return Task.CompletedTask; } /// /// Get task triggers. /// /// Task triggers. public IEnumerable GetDefaultTriggers() { return Array.Empty(); } }