84 lines
3.2 KiB
C#
84 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Jellyfin.Database.Implementations.Entities;
|
|
using Jellyfin.Database.Implementations.Entities.Libraries;
|
|
using Jellyfin.Plugin.MediaCleaner.Configuration;
|
|
using MediaBrowser.Controller.Entities;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
namespace Jellyfin.Plugin.MediaCleaner.Helpers;
|
|
|
|
public class SeriesHelper
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly LoggingHelper _loggingHelper;
|
|
|
|
public SeriesHelper(ILogger logger)
|
|
{
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
_loggingHelper = new LoggingHelper(logger);
|
|
}
|
|
|
|
private static PluginConfiguration Configuration =>
|
|
Plugin.Instance!.Configuration;
|
|
|
|
public bool IsSeasonUserDataStale(IReadOnlyList<BaseItem> episodes)
|
|
{
|
|
bool seasonIsStale = false;
|
|
|
|
List<BaseItem> staleEpisodes = [];
|
|
|
|
var episodesWithUserData = episodes.Where(episode => episode.UserData.Where(data => data.LastPlayedDate != null).ToList().Count > 0).ToList();
|
|
|
|
_loggingHelper.PrintDebugEpisodesWithUserData(episodesWithUserData);
|
|
|
|
foreach (var episode in episodesWithUserData)
|
|
{
|
|
bool episodeIsStale = false;
|
|
|
|
var mostRecentUserData = episode.UserData.OrderByDescending(data => data.LastPlayedDate).First(data => data.LastPlayedDate != null);
|
|
var staleLastPlayedDate = mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff);
|
|
var staleCreationDate = episode.DateCreated < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff);
|
|
|
|
if(Configuration.DebugMode){
|
|
_logger.LogInformation("User data for episode: {Episode}", episode);
|
|
_logger.LogInformation("-------------------------------------------------");
|
|
foreach (var property in typeof(UserData).GetProperties())
|
|
{
|
|
_logger.LogInformation("{PropertyName}: {PropertyValue}", property.Name, property.GetValue(mostRecentUserData));
|
|
}
|
|
_logger.LogInformation("-------------------------------------------------");
|
|
}
|
|
|
|
if (staleLastPlayedDate && staleCreationDate)
|
|
{
|
|
episodeIsStale = true;
|
|
if(Configuration.DebugMode){
|
|
_logger.LogInformation("Most recent user data has a last played date of: {LastPlayedDate}.", [mostRecentUserData.LastPlayedDate]);
|
|
_logger.LogInformation("And episode created {DateCreated}.", episode.DateCreated);
|
|
}
|
|
}
|
|
|
|
if (episodeIsStale)
|
|
{
|
|
staleEpisodes.Add(episode);
|
|
if(Configuration.DebugMode){
|
|
_logger.LogInformation("Episode is stale.");
|
|
}
|
|
}
|
|
}
|
|
|
|
if(staleEpisodes.Count == episodes.Count)
|
|
{
|
|
seasonIsStale = true;
|
|
_logger.LogInformation("Stale episodes count matches all episode count. Season is stale.");
|
|
_logger.LogInformation("-------------------------------------------------");
|
|
}
|
|
|
|
return seasonIsStale;
|
|
}
|
|
}
|