108 lines
3.6 KiB
C#
Raw Normal View History

2024-10-25 14:31:50 -04:00
// Copyright (C) 2024 Intro-Skipper contributors <intro-skipper.org>
// SPDX-License-Identifier: GPL-3.0-only.
2024-10-25 14:15:12 -04:00
2022-05-01 00:33:22 -05:00
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using IntroSkipper.Data;
using IntroSkipper.Manager;
using IntroSkipper.Services;
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 IntroSkipper.ScheduledTasks;
2022-05-01 00:33:22 -05:00
/// <summary>
2024-03-30 18:22:54 -04:00
/// Analyze all television episodes for credits.
/// TODO: analyze all media files.
2022-05-01 00:33:22 -05:00
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="DetectCreditsTask"/> class.
/// </remarks>
/// <param name="loggerFactory">Logger factory.</param>
/// <param name="libraryManager">Library manager.</param>
/// <param name="logger">Logger.</param>
/// <param name="mediaSegmentUpdateManager">MediaSegment Update Manager.</param>
public class DetectCreditsTask(
ILogger<DetectCreditsTask> logger,
ILoggerFactory loggerFactory,
ILibraryManager libraryManager,
MediaSegmentUpdateManager mediaSegmentUpdateManager) : IScheduledTask
{
private readonly ILogger<DetectCreditsTask> _logger = logger;
2024-03-30 18:22:54 -04:00
private readonly ILoggerFactory _loggerFactory = loggerFactory;
2022-06-22 22:03:34 -05:00
private readonly ILibraryManager _libraryManager = libraryManager;
private readonly MediaSegmentUpdateManager _mediaSegmentUpdateManager = mediaSegmentUpdateManager;
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 18:22:54 -04:00
public string Name => "Detect 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 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 18:22:54 -04:00
public string Key => "CPBIntroSkipperDetectCredits";
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>
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
2022-05-01 00:33:22 -05:00
{
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);
await Entrypoint.CancelAutomaticTaskAsync(cancellationToken).ConfigureAwait(false);
2024-03-30 15:36:15 +01:00
}
using (await ScheduledTaskSemaphore.AcquireAsync(cancellationToken).ConfigureAwait(false))
2024-03-30 15:36:15 +01:00
{
2024-05-10 06:35:52 +02:00
_logger.LogInformation("Scheduled Task is starting");
var modes = new List<AnalysisMode> { AnalysisMode.Credits };
2024-05-10 06:35:52 +02:00
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
modes,
2024-05-10 06:35:52 +02:00
_loggerFactory.CreateLogger<DetectCreditsTask>(),
_loggerFactory,
_libraryManager,
_mediaSegmentUpdateManager);
await baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken).ConfigureAwait(false);
2024-05-10 06:35:52 +02:00
}
2022-05-13 01:28:06 -05:00
}
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 [];
2022-05-01 00:33:22 -05:00
}
}