using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using ConfusedPolarBear.Plugin.IntroSkipper.Data;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace ConfusedPolarBear.Plugin.IntroSkipper.ScheduledTasks;
///
/// 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 media to determine the timestamp and length of 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");
}
// 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);
}
using (ScheduledTaskSemaphore.Acquire(cancellationToken))
{
_logger.LogInformation("Scheduled Task is starting");
var modes = new List { AnalysisMode.Credits };
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
modes,
_loggerFactory.CreateLogger(),
_loggerFactory,
_libraryManager);
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
return Task.CompletedTask;
}
}
///
/// Get task triggers.
///
/// Task triggers.
public IEnumerable GetDefaultTriggers()
{
return [];
}
}