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;
|
2024-10-19 23:50:41 +02:00
|
|
|
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;
|
|
|
|
|
2024-10-19 23:50:41 +02:00
|
|
|
namespace IntroSkipper.ScheduledTasks;
|
2022-05-01 00:33:22 -05:00
|
|
|
|
|
|
|
/// <summary>
|
2024-11-21 15:42:55 +01:00
|
|
|
/// Analyze all television episodes for media segments.
|
2022-05-01 00:33:22 -05:00
|
|
|
/// </summary>
|
2024-10-19 22:49:47 +02:00
|
|
|
/// <remarks>
|
2024-11-21 15:42:55 +01:00
|
|
|
/// Initializes a new instance of the <see cref="DetectSegmentsTask"/> class.
|
2024-10-19 22:49:47 +02:00
|
|
|
/// </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>
|
2024-11-21 15:42:55 +01:00
|
|
|
public class DetectSegmentsTask(
|
|
|
|
ILogger<DetectSegmentsTask> logger,
|
2024-10-19 22:49:47 +02:00
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
MediaSegmentUpdateManager mediaSegmentUpdateManager) : IScheduledTask
|
2022-05-09 22:50:41 -05:00
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
private readonly ILogger<DetectSegmentsTask> _logger = logger;
|
2024-04-13 16:34:58 +02:00
|
|
|
|
2024-10-19 22:49:47 +02:00
|
|
|
private readonly ILoggerFactory _loggerFactory = loggerFactory;
|
2022-06-22 22:03:34 -05:00
|
|
|
|
2024-10-19 22:49:47 +02:00
|
|
|
private readonly ILibraryManager _libraryManager = libraryManager;
|
2024-10-16 16:05:59 +02:00
|
|
|
|
2024-10-19 22:49:47 +02:00
|
|
|
private readonly MediaSegmentUpdateManager _mediaSegmentUpdateManager = mediaSegmentUpdateManager;
|
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>
|
2024-11-21 15:42:55 +01:00
|
|
|
public string Name => "Detect and Analyze Media Segments";
|
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>
|
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>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// Gets the task key.
|
2022-05-01 00:33:22 -05:00
|
|
|
/// </summary>
|
2024-11-21 15:42:55 +01:00
|
|
|
public string Key => "IntroSkipperDetectSegmentsTask";
|
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>
|
2024-10-19 22:49:47 +02:00
|
|
|
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)
|
|
|
|
{
|
2023-02-02 01:20:11 -06:00
|
|
|
throw new InvalidOperationException("Library manager was null");
|
2022-06-22 22:03:34 -05:00
|
|
|
}
|
|
|
|
|
2024-04-16 18:19:05 +02:00
|
|
|
// abort automatic analyzer if running
|
|
|
|
if (Entrypoint.AutomaticTaskState == TaskState.Running || Entrypoint.AutomaticTaskState == TaskState.Cancelling)
|
2024-03-29 16:58:16 +01:00
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
_logger.LogInformation("Automatic Task is {TaskState} and will be canceled.", Entrypoint.AutomaticTaskState);
|
2024-10-19 22:49:47 +02:00
|
|
|
await Entrypoint.CancelAutomaticTaskAsync(cancellationToken).ConfigureAwait(false);
|
2024-03-30 15:36:15 +01:00
|
|
|
}
|
2024-04-16 18:19:05 +02:00
|
|
|
|
2024-10-19 22:49:47 +02: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");
|
2024-03-29 16:58:16 +01:00
|
|
|
|
2024-05-10 06:35:52 +02:00
|
|
|
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
|
2024-11-21 15:42:55 +01:00
|
|
|
_loggerFactory.CreateLogger<DetectSegmentsTask>(),
|
2024-05-10 06:35:52 +02:00
|
|
|
_loggerFactory,
|
2024-10-19 22:49:47 +02:00
|
|
|
_libraryManager,
|
|
|
|
_mediaSegmentUpdateManager);
|
2024-03-30 19:02:18 +01:00
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
await baseIntroAnalyzer.AnalyzeItemsAsync(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>
|
2022-05-09 22:50:41 -05:00
|
|
|
/// <returns>Task triggers.</returns>
|
2022-05-01 00:33:22 -05:00
|
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
|
|
|
{
|
2024-09-10 18:08:42 +02:00
|
|
|
return
|
|
|
|
[
|
2022-05-01 00:33:22 -05:00
|
|
|
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
|
|
|
}
|
2024-09-10 18:08:42 +02:00
|
|
|
];
|
2022-05-01 00:33:22 -05:00
|
|
|
}
|
|
|
|
}
|