Further enhanced logging and refactored StaleMediaTask to aid in maintainability
This commit is contained in:
74
Jellyfin.Plugin.MediaCleaner/Helpers/MovieHelper.cs
Normal file
74
Jellyfin.Plugin.MediaCleaner/Helpers/MovieHelper.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
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)
|
||||
{
|
||||
_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).Where(data => data.LastPlayedDate != null).First();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
_logger.LogInformation("-------------------------------------------------");
|
||||
_logger.LogInformation("End of scanning for movie: {Movie}", movie);
|
||||
_logger.LogInformation("-------------------------------------------------");
|
||||
|
||||
return movieIsStale;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user