74 lines
3.1 KiB
C#
74 lines
3.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Jellyfin.Database.Implementations.Entities;
|
|
using Jellyfin.Plugin.MediaCleaner.Configuration;
|
|
using MediaBrowser.Controller.Entities;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.MediaCleaner.Helpers;
|
|
|
|
public class MovieHelper(ILogger logger)
|
|
{
|
|
private readonly LoggingHelper _loggingHelper = new(logger);
|
|
|
|
private static PluginConfiguration Configuration =>
|
|
Plugin.Instance!.Configuration;
|
|
|
|
public bool IsMovieStale(BaseItem movie)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(movie, "IsMovieStale process recieved a null movie. Logic failure. Exception thrown.");
|
|
|
|
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
|
_loggingHelper.LogDebugInformation("Scanning movie: {Movie}", movie);
|
|
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
|
|
|
bool movieIsStale = false;
|
|
|
|
bool createdOutsideCutoff = movie.DateCreated < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff);
|
|
bool hasUserData = movie.UserData.Where(data => data.LastPlayedDate != null).ToList().Count > 0;
|
|
|
|
if (hasUserData)
|
|
{
|
|
var mostRecentUserData = movie.UserData.OrderByDescending(data => data.LastPlayedDate).First(data => data.LastPlayedDate != null);
|
|
|
|
_loggingHelper.LogDebugInformation("Most recent user data: {Movie}", movie);
|
|
|
|
foreach (var property in typeof(UserData).GetProperties())
|
|
{
|
|
_loggingHelper.LogDebugInformation("{PropertyName}: {PropertyValue}", property.Name, property.GetValue(mostRecentUserData));
|
|
}
|
|
|
|
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
|
|
|
if (mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff))
|
|
{
|
|
_loggingHelper.LogDebugInformation("Most recent user data has last played date that is outside of cutoff.");
|
|
_loggingHelper.LogDebugInformation("Adding {Movie} to stale movies.", movie);
|
|
_loggingHelper.LogDebugInformation("With Last Played Date: {LastPlayedDate}", mostRecentUserData.LastPlayedDate);
|
|
|
|
movieIsStale = true;
|
|
}
|
|
}
|
|
|
|
if (createdOutsideCutoff && !hasUserData)
|
|
{
|
|
_loggingHelper.LogDebugInformation("Movie has no user data and was created outside of cutoff: {DateCreated}.", movie.DateCreated);
|
|
_loggingHelper.LogDebugInformation("Adding {Movie} to stale movies.", movie);
|
|
movieIsStale = true;
|
|
}
|
|
|
|
if (!createdOutsideCutoff && !hasUserData)
|
|
{
|
|
_loggingHelper.LogDebugInformation("Movie has no user data and was not created outside of cutoff: {DateCreated}.", movie.DateCreated);
|
|
_loggingHelper.LogDebugInformation("Movie is not stale.");
|
|
}
|
|
|
|
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
|
_loggingHelper.LogDebugInformation("End of scanning for movie: {Movie}", movie);
|
|
|
|
return movieIsStale;
|
|
}
|
|
|
|
}
|