120 lines
3.8 KiB
C#
Raw Normal View History

2022-05-01 00:33:22 -05:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
2022-05-01 00:33:22 -05:00
using System.Threading;
using System.Threading.Tasks;
2022-06-22 22:03:34 -05:00
using MediaBrowser.Controller.Library;
2022-05-01 00:33:22 -05:00
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace ConfusedPolarBear.Plugin.IntroSkipper;
/// <summary>
/// Analyze all television episodes for introduction sequences.
2022-05-01 00:33:22 -05:00
/// </summary>
2024-03-30 18:22:54 -04:00
public class DetectIntrosCreditsTask : IScheduledTask
{
private readonly ILogger<DetectIntrosCreditsTask> _logger;
2022-10-28 02:25:57 -05:00
private readonly ILoggerFactory _loggerFactory;
2022-06-22 22:03:34 -05:00
private readonly ILibraryManager _libraryManager;
2022-06-22 22:03:34 -05:00
2022-05-01 00:33:22 -05:00
/// <summary>
2024-03-30 18:22:54 -04:00
/// Initializes a new instance of the <see cref="DetectIntrosCreditsTask"/> class.
2022-05-01 00:33:22 -05:00
/// </summary>
2022-06-22 22:03:34 -05:00
/// <param name="loggerFactory">Logger factory.</param>
/// <param name="libraryManager">Library manager.</param>
/// <param name="logger">Logger.</param>
2024-03-30 18:22:54 -04:00
public DetectIntrosCreditsTask(
ILogger<DetectIntrosCreditsTask> logger,
2022-09-25 17:07:11 -05:00
ILoggerFactory loggerFactory,
ILibraryManager libraryManager)
2022-05-01 00:33:22 -05:00
{
_logger = logger;
2022-10-28 02:25:57 -05:00
_loggerFactory = loggerFactory;
_libraryManager = libraryManager;
2022-05-01 00:33:22 -05:00
}
/// <summary>
/// Gets the task name.
2022-05-01 00:33:22 -05:00
/// </summary>
2024-03-30 20:16:03 -04:00
public string Name => "Detect Intros and Credits";
2022-05-01 00:33:22 -05:00
/// <summary>
/// Gets the task category.
2022-05-01 00:33:22 -05:00
/// </summary>
public string Category => "Intro Skipper";
/// <summary>
/// Gets the task description.
2022-05-01 00:33:22 -05:00
/// </summary>
2024-03-30 20:16:03 -04:00
public string Description => "Analyzes media to determine the timestamp and length of intros and credits.";
2022-05-01 00:33:22 -05:00
/// <summary>
/// Gets the task key.
2022-05-01 00:33:22 -05:00
/// </summary>
2024-03-30 20:16:03 -04:00
public string Key => "CPBIntroSkipperDetectIntrosCredits";
2022-05-01 00:33:22 -05:00
/// <summary>
2022-06-22 22:03:34 -05:00
/// Analyze all episodes in the queue. Only one instance of this task should be run at a time.
2022-05-01 00:33:22 -05:00
/// </summary>
/// <param name="progress">Task progress.</param>
2022-05-01 00:33:22 -05:00
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Task.</returns>
2022-05-01 00:33:22 -05:00
public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
2022-06-22 22:03:34 -05:00
if (_libraryManager is null)
{
throw new InvalidOperationException("Library manager was null");
2022-06-22 22:03:34 -05:00
}
// 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);
2024-03-30 15:36:15 +01:00
}
ScheduledTaskSemaphore.Wait(-1, cancellationToken);
if (cancellationToken.IsCancellationRequested)
2024-03-30 15:36:15 +01:00
{
ScheduledTaskSemaphore.Release();
return Task.CompletedTask;
}
_logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.Configuration.PathRestrictions.Clear();
var modes = new List<AnalysisMode> { AnalysisMode.Introduction, AnalysisMode.Credits };
2024-03-30 18:22:54 -04:00
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
modes.AsReadOnly(),
2024-03-30 18:22:54 -04:00
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),
_loggerFactory,
_libraryManager);
2024-03-30 18:22:54 -04:00
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
ScheduledTaskSemaphore.Release();
2022-05-13 01:28:06 -05:00
return Task.CompletedTask;
}
2022-05-01 00:33:22 -05:00
/// <summary>
/// Get task triggers.
/// </summary>
/// <returns>Task triggers.</returns>
2022-05-01 00:33:22 -05:00
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
return new[]
{
new TaskTriggerInfo
{
Type = TaskTriggerInfo.TriggerDaily,
TimeOfDayTicks = TimeSpan.FromHours(0).Ticks
2022-05-01 00:33:22 -05:00
}
};
}
}