134 lines
4.3 KiB
C#
Raw Normal View History

2022-05-01 00:33:22 -05:00
using System;
using System.Collections.Generic;
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>
2022-11-02 17:07:45 -05:00
public class DetectIntroductionsTask : IScheduledTask
{
private readonly ILogger<DetectIntroductionsTask> _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>
2022-11-02 17:07:45 -05:00
/// Initializes a new instance of the <see cref="DetectIntroductionsTask"/> 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>
2022-11-02 17:07:45 -05:00
public DetectIntroductionsTask(
ILogger<DetectIntroductionsTask> 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>
2022-08-25 01:00:02 -05:00
public string Name => "Detect Introductions";
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>
public string Description => "Analyzes the audio of all television episodes to find introduction sequences.";
/// <summary>
/// Gets the task key.
2022-05-01 00:33:22 -05:00
/// </summary>
2022-08-25 01:00:02 -05:00
public string Key => "CPBIntroSkipperDetectIntroductions";
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
}
// 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<DetectIntroductionsTask>(),
_loggerFactory,
2022-10-28 02:25:57 -05:00
_libraryManager);
baseAnalyzer.AnalyzeItems(progress, cancellationToken);
2022-06-24 00:02:08 -05:00
Plugin.Instance!.AnalyzerTaskIsRunning = false;
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
}
};
}
}