116 lines
3.4 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>
2024-03-30 18:22:54 -04:00
public class DetectIntrosCreditsTask : IScheduledTask
{
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>
2024-03-30 18:22:54 -04:00
public DetectIntrosCreditsTask(
2022-09-25 17:07:11 -05:00
ILoggerFactory loggerFactory,
ILibraryManager libraryManager)
2022-05-01 00:33:22 -05:00
{
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
}
2024-03-30 15:36:15 +01:00
// abort if analyzer is already running
if (Plugin.Instance!.AnalyzerTaskIsRunning)
{
2024-03-30 15:36:15 +01:00
return Task.CompletedTask;
}
else
{
Plugin.Instance!.AnalyzerTaskIsRunning = true;
}
2024-03-30 18:22:54 -04:00
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
AnalysisMode.Introduction,
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),
_loggerFactory,
_libraryManager);
2024-03-30 18:22:54 -04:00
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
// reset progress
progress.Report(0);
2024-03-30 18:22:54 -04:00
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
AnalysisMode.Credits,
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),
_loggerFactory,
_libraryManager);
2024-03-30 18:22:54 -04:00
baseCreditAnalyzer.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
}
};
}
}