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>
|
2022-08-22 23:18:13 -05:00
|
|
|
/// 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
|
2022-05-09 22:50:41 -05:00
|
|
|
{
|
2024-03-29 16:58:16 +01:00
|
|
|
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
|
|
|
|
2023-02-02 01:20:11 -06: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>
|
2024-03-29 16:58:16 +01:00
|
|
|
/// <param name="logger">Logger.</param>
|
2022-11-02 17:07:45 -05:00
|
|
|
public DetectIntroductionsTask(
|
2024-03-29 16:58:16 +01:00
|
|
|
ILogger<DetectIntroductionsTask> logger,
|
2022-09-25 17:07:11 -05:00
|
|
|
ILoggerFactory loggerFactory,
|
2023-02-02 01:20:11 -06:00
|
|
|
ILibraryManager libraryManager)
|
2022-05-01 00:33:22 -05:00
|
|
|
{
|
2024-03-29 16:58:16 +01:00
|
|
|
_logger = logger;
|
2022-10-28 02:25:57 -05:00
|
|
|
_loggerFactory = loggerFactory;
|
2023-02-02 01:20:11 -06:00
|
|
|
_libraryManager = libraryManager;
|
2022-05-01 00:33:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// 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>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// Gets the task category.
|
2022-05-01 00:33:22 -05:00
|
|
|
/// </summary>
|
|
|
|
public string Category => "Intro Skipper";
|
|
|
|
|
|
|
|
/// <summary>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// 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>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// 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>
|
2022-05-10 02:10:39 -05:00
|
|
|
/// <param name="progress">Task progress.</param>
|
2022-05-01 00:33:22 -05:00
|
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// <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)
|
|
|
|
{
|
2023-02-02 01:20:11 -06:00
|
|
|
throw new InvalidOperationException("Library manager was null");
|
2022-06-22 22:03:34 -05:00
|
|
|
}
|
|
|
|
|
2024-03-29 16:58:16 +01: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;
|
|
|
|
|
2023-02-02 01:20:11 -06:00
|
|
|
var baseAnalyzer = new BaseItemAnalyzerTask(
|
|
|
|
AnalysisMode.Introduction,
|
|
|
|
_loggerFactory.CreateLogger<DetectIntroductionsTask>(),
|
|
|
|
_loggerFactory,
|
2022-10-28 02:25:57 -05:00
|
|
|
_libraryManager);
|
|
|
|
|
2023-02-02 01:20:11 -06:00
|
|
|
baseAnalyzer.AnalyzeItems(progress, cancellationToken);
|
2022-06-24 00:02:08 -05:00
|
|
|
|
2024-03-29 16:58:16 +01: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>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// <returns>Task triggers.</returns>
|
2022-05-01 00:33:22 -05:00
|
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
|
|
|
{
|
|
|
|
return new[]
|
|
|
|
{
|
|
|
|
new TaskTriggerInfo
|
|
|
|
{
|
|
|
|
Type = TaskTriggerInfo.TriggerDaily,
|
2022-08-22 23:18:13 -05:00
|
|
|
TimeOfDayTicks = TimeSpan.FromHours(0).Ticks
|
2022-05-01 00:33:22 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|