Added logic to get correct count of stale media, based on cutoff.
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using MediaBrowser.Controller.Entities;
|
||||||
using MediaBrowser.Model.Plugins;
|
using MediaBrowser.Model.Plugins;
|
||||||
|
|
||||||
namespace Jellyfin.Plugin.MediaCleaner.Configuration;
|
namespace Jellyfin.Plugin.MediaCleaner.Configuration;
|
||||||
@@ -27,5 +30,5 @@ public class PluginConfiguration : BasePluginConfiguration
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the cut off days before deleting unwatched files.
|
/// Gets or sets the cut off days before deleting unwatched files.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int DaysUnwatched { get; set; } = 90;
|
public int StaleMediaCutoff { get; set; } = 90;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reflection.Metadata.Ecma335;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Jellyfin.Data.Enums;
|
using Jellyfin.Data.Enums;
|
||||||
using Jellyfin.Database.Implementations.Entities;
|
using Jellyfin.Database.Implementations.Entities;
|
||||||
|
using Jellyfin.Database.Implementations.Entities.Libraries;
|
||||||
|
using Jellyfin.Plugin.MediaCleaner.Configuration;
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
using MediaBrowser.Controller.Library;
|
using MediaBrowser.Controller.Library;
|
||||||
using MediaBrowser.Model.Tasks;
|
using MediaBrowser.Model.Tasks;
|
||||||
@@ -35,6 +39,9 @@ public sealed class StaleMediaTask : IScheduledTask
|
|||||||
_libraryManager = libraryManager;
|
_libraryManager = libraryManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static PluginConfiguration Configuration =>
|
||||||
|
Plugin.Instance!.Configuration;
|
||||||
|
|
||||||
string IScheduledTask.Name => "Scan Stale Media";
|
string IScheduledTask.Name => "Scan Stale Media";
|
||||||
|
|
||||||
string IScheduledTask.Key => "Stale Media";
|
string IScheduledTask.Key => "Stale Media";
|
||||||
@@ -47,26 +54,85 @@ public sealed class StaleMediaTask : IScheduledTask
|
|||||||
{
|
{
|
||||||
var query = new InternalItemsQuery
|
var query = new InternalItemsQuery
|
||||||
{
|
{
|
||||||
IncludeItemTypes = new[] { BaseItemKind.Movie, BaseItemKind.Series },
|
IncludeItemTypes = [BaseItemKind.Movie, BaseItemKind.Series],
|
||||||
Recursive = true
|
Recursive = true
|
||||||
};
|
};
|
||||||
var allItems = _libraryManager.GetItemsResult(query).Items;
|
List<BaseItem> allItems = [.. _libraryManager.GetItemsResult(query).Items];
|
||||||
|
|
||||||
_logger.LogInformation("Total items found: {AllItems}", allItems);
|
_logger.LogInformation("Total items found: {AllItems}", allItems);
|
||||||
|
|
||||||
foreach (BaseItem item in allItems)
|
List<BaseItem> shows = [.. allItems.Where(item => item.GetBaseItemKind() == BaseItemKind.Series)];
|
||||||
|
List<BaseItem> movies = [.. allItems.Where(item => item.GetBaseItemKind() == BaseItemKind.Movie && item.UserData.Count > 0)];
|
||||||
|
|
||||||
|
List<BaseItem> staleEpisodes = [.. shows.SelectMany(GetStaleEpisodes)];
|
||||||
|
List<BaseItem> staleMovies = [.. GetStaleMovies(movies)];
|
||||||
|
|
||||||
|
_logger.LogInformation("Stale Movies found: {StaleMovies}", staleMovies.Count);
|
||||||
|
_logger.LogInformation("Stale Series found: {StaleShows}", staleEpisodes.Count);
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<BaseItem> GetStaleMovies(List<BaseItem> movies)
|
||||||
|
{
|
||||||
|
List<BaseItem> staleMovies = [];
|
||||||
|
foreach (var movie in movies)
|
||||||
{
|
{
|
||||||
var userData = item.UserData.ToList();
|
var mostRecentUserData = movie.UserData.OrderByDescending(data => data.LastPlayedDate).First();
|
||||||
var mostRecentUserData = userData.OrderByDescending(data => data.LastPlayedDate).First();
|
if (mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff))
|
||||||
if (mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(1))
|
|
||||||
{
|
{
|
||||||
// Stale data
|
staleMovies.Add(movie);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Debugger.Break();
|
return staleMovies;
|
||||||
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
private List<BaseItem> GetStaleEpisodes(BaseItem item)
|
||||||
|
{
|
||||||
|
List<BaseItem> staleEpisodes = [];
|
||||||
|
|
||||||
|
// Gets each season in a show
|
||||||
|
var seasons = _libraryManager.GetItemList(new InternalItemsQuery
|
||||||
|
{
|
||||||
|
ParentId = item.Id,
|
||||||
|
Recursive = false
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach (var season in seasons)
|
||||||
|
{
|
||||||
|
// Gets each episode, to access user data.
|
||||||
|
var episodes = _libraryManager.GetItemList(new InternalItemsQuery
|
||||||
|
{
|
||||||
|
ParentId = season.Id,
|
||||||
|
Recursive = false
|
||||||
|
});
|
||||||
|
|
||||||
|
bool seasonHasUserData = episodes.Any(episode => episode.UserData.Count > 0);
|
||||||
|
|
||||||
|
if (seasonHasUserData)
|
||||||
|
{
|
||||||
|
var episodesWithUserData = episodes.Where(episode => episode.UserData.Count > 0).ToList();
|
||||||
|
foreach (var episode in episodesWithUserData)
|
||||||
|
{
|
||||||
|
bool stale = false;
|
||||||
|
var mostRecentUserData = episode.UserData.OrderByDescending(data => data.LastPlayedDate).First();
|
||||||
|
if (mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff))
|
||||||
|
{
|
||||||
|
staleEpisodes.AddRange(episodes);
|
||||||
|
stale = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stale)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return staleEpisodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerable<TaskTriggerInfo> IScheduledTask.GetDefaultTriggers()
|
IEnumerable<TaskTriggerInfo> IScheduledTask.GetDefaultTriggers()
|
||||||
|
|||||||
Reference in New Issue
Block a user