80 lines
2.9 KiB
C#
80 lines
2.9 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
|
|
{
|
|
private readonly ILogger _logger;
|
|
|
|
public MovieHelper(ILogger logger)
|
|
{
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
private static PluginConfiguration Configuration =>
|
|
Plugin.Instance!.Configuration;
|
|
|
|
public bool IsMovieStale(BaseItem movie)
|
|
{
|
|
if (Configuration.DebugMode)
|
|
{
|
|
_logger.LogInformation("-------------------------------------------------");
|
|
_logger.LogInformation("Start of scanning for movie: {Movie}", movie);
|
|
_logger.LogInformation("-------------------------------------------------");
|
|
}
|
|
|
|
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);
|
|
|
|
if (Configuration.DebugMode){
|
|
_logger.LogInformation("Most recent user data: {Movie}", movie);
|
|
|
|
foreach (var property in typeof(UserData).GetProperties())
|
|
{
|
|
_logger.LogInformation("{PropertyName}: {PropertyValue}", property.Name, property.GetValue(mostRecentUserData));
|
|
}
|
|
_logger.LogInformation("-------------------------------------------------");
|
|
}
|
|
|
|
if (mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff))
|
|
{
|
|
if (Configuration.DebugMode)
|
|
{
|
|
_logger.LogInformation("Most recent user data last played date is outside of cutoff. Adding {Movie} to stale movies.", movie);
|
|
}
|
|
movieIsStale = true;
|
|
}
|
|
}
|
|
else if (createdOutsideCutoff)
|
|
{
|
|
if (Configuration.DebugMode)
|
|
{
|
|
_logger.LogInformation("Movie has no user data and was created outside of cutoff: {DateCreated}. Adding {Movie} to stale movies.", [movie.DateCreated, movie]);
|
|
}
|
|
movieIsStale = true;
|
|
}
|
|
|
|
if (Configuration.DebugMode)
|
|
{
|
|
_logger.LogInformation("-------------------------------------------------");
|
|
_logger.LogInformation("End of scanning for movie: {Movie}", movie);
|
|
_logger.LogInformation("-------------------------------------------------");
|
|
}
|
|
|
|
return movieIsStale;
|
|
}
|
|
|
|
}
|