150 lines
5.7 KiB
C#
150 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using MediaBrowser.Controller.Entities;
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
using MediaBrowser.Controller.Library;
|
|
using MediaBrowser.Model.Logging;
|
|
using MediaBrowser.Model.Tasks;
|
|
|
|
namespace NFOGuard.Emby.Plugin
|
|
{
|
|
public class NFOGuardScheduledTask : IScheduledTask
|
|
{
|
|
private readonly ILibraryManager _libraryManager;
|
|
private readonly ILogger _logger;
|
|
|
|
public string Name => "NFOGuard Date Sync";
|
|
|
|
public string Key => "NFOGuardSync";
|
|
|
|
public string Description => "Synchronizes PremiereDate to DateCreated for all TV episodes and movies";
|
|
|
|
public string Category => "Library";
|
|
|
|
public NFOGuardScheduledTask(ILibraryManager libraryManager, ILogManager logManager)
|
|
{
|
|
_libraryManager = libraryManager;
|
|
_logger = logManager.GetLogger("NFOGuard");
|
|
}
|
|
|
|
public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
|
|
{
|
|
if (!Plugin.Instance.Configuration.EnableScheduledTask)
|
|
{
|
|
_logger.Info("NFOGuard :: Scheduled task is disabled in configuration");
|
|
return;
|
|
}
|
|
|
|
_logger.Info("NFOGuard :: Starting scheduled task execution");
|
|
|
|
try
|
|
{
|
|
// Get all TV episodes and movies
|
|
var episodes = _libraryManager.GetItemList(new InternalItemsQuery
|
|
{
|
|
IncludeItemTypes = new[] { "Episode" },
|
|
Recursive = true
|
|
}).OfType<Episode>().ToList();
|
|
|
|
var movies = _libraryManager.GetItemList(new InternalItemsQuery
|
|
{
|
|
IncludeItemTypes = new[] { "Movie" },
|
|
Recursive = true
|
|
}).OfType<Movie>().ToList();
|
|
|
|
var totalItems = episodes.Count + movies.Count;
|
|
_logger.Info($"NFOGuard :: Found {episodes.Count} episodes and {movies.Count} movies to process ({totalItems} total)");
|
|
|
|
var processedCount = 0;
|
|
var updatedCount = 0;
|
|
|
|
// Process episodes
|
|
for (int i = 0; i < episodes.Count; i++)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
var episode = episodes[i];
|
|
try
|
|
{
|
|
if (episode.PremiereDate.HasValue &&
|
|
episode.DateCreated != episode.PremiereDate.Value)
|
|
{
|
|
if (Plugin.Instance.Configuration.LogVerbose)
|
|
_logger.Info($"NFOGuard :: Syncing episode: {episode.Name} :: " +
|
|
$"PremiereDate: {episode.PremiereDate.Value}, DateCreated: {episode.DateCreated}");
|
|
|
|
episode.DateCreated = episode.PremiereDate.Value;
|
|
episode.UpdateToRepository(ItemUpdateType.MetadataEdit);
|
|
updatedCount++;
|
|
}
|
|
|
|
processedCount++;
|
|
var progressPercentage = (double)processedCount / totalItems * 100;
|
|
progress?.Report(progressPercentage);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.ErrorException($"NFOGuard :: Error processing episode {episode.Name}", ex);
|
|
}
|
|
}
|
|
|
|
// Process movies
|
|
for (int i = 0; i < movies.Count; i++)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
var movie = movies[i];
|
|
try
|
|
{
|
|
if (movie.PremiereDate.HasValue &&
|
|
movie.DateCreated != movie.PremiereDate.Value)
|
|
{
|
|
if (Plugin.Instance.Configuration.LogVerbose)
|
|
_logger.Info($"NFOGuard :: Syncing movie: {movie.Name} :: " +
|
|
$"PremiereDate: {movie.PremiereDate.Value}, DateCreated: {movie.DateCreated}");
|
|
|
|
movie.DateCreated = movie.PremiereDate.Value;
|
|
movie.UpdateToRepository(ItemUpdateType.MetadataEdit);
|
|
updatedCount++;
|
|
}
|
|
|
|
processedCount++;
|
|
var progressPercentage = (double)processedCount / totalItems * 100;
|
|
progress?.Report(progressPercentage);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.ErrorException($"NFOGuard :: Error processing movie {movie.Name}", ex);
|
|
}
|
|
}
|
|
|
|
_logger.Info($"NFOGuard :: Scheduled task completed. Processed: {processedCount} items ({episodes.Count} episodes, {movies.Count} movies), Updated: {updatedCount}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.ErrorException("NFOGuard :: Scheduled task failed", ex);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
|
{
|
|
// Default to run weekly at 3 AM on Sunday
|
|
return new[]
|
|
{
|
|
new TaskTriggerInfo
|
|
{
|
|
Type = TaskTriggerInfo.TriggerWeekly,
|
|
DayOfWeek = DayOfWeek.Sunday,
|
|
TimeOfDayTicks = TimeSpan.FromHours(3).Ticks
|
|
}
|
|
};
|
|
}
|
|
}
|
|
} |