Converted StaleMediaTask to StaleMediaScanner for use in plugin home page. Also added a generic Media Info class that can be filtered to return the data you desire.
This commit is contained in:
225
Jellyfin.Plugin.MediaCleaner/StaleMediaScanner.cs
Normal file
225
Jellyfin.Plugin.MediaCleaner/StaleMediaScanner.cs
Normal file
@@ -0,0 +1,225 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Data.Common;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Data.Enums;
|
||||
using Jellyfin.Database.Implementations.Entities;
|
||||
using Jellyfin.Database.Implementations.Entities.Libraries;
|
||||
using Jellyfin.Plugin.MediaCleaner.Configuration;
|
||||
using Jellyfin.Plugin.MediaCleaner.Helpers;
|
||||
using Jellyfin.Plugin.MediaCleaner;
|
||||
using Jellyfin.Plugin.MediaCleaner.Models;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.MediaCleaner;
|
||||
|
||||
/// <summary>
|
||||
/// A task to scan media for stale files.
|
||||
/// </summary>
|
||||
public sealed class StaleMediaScanner
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly LoggingHelper _loggingHelper;
|
||||
private readonly MovieHelper _movieHelper;
|
||||
private readonly SeriesHelper _seriesHelper;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StaleMediaTask"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger for StaleMediaTask.</param>
|
||||
/// <param name="libraryManager">Accesses jellyfin's library manager for media.</param>
|
||||
public StaleMediaScanner(ILogger<StaleMediaScanner> logger, ILibraryManager libraryManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_libraryManager = libraryManager;
|
||||
_loggingHelper = new LoggingHelper(_logger);
|
||||
_movieHelper = new MovieHelper(_logger);
|
||||
_seriesHelper = new SeriesHelper(_logger);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<MediaInfo>> ScanStaleMedia()
|
||||
{
|
||||
_loggingHelper.LogDebugInformation("--DEBUG MODE ACTIVE--");
|
||||
_loggingHelper.LogInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogInformation("Starting stale media scan...");
|
||||
_loggingHelper.LogInformation("-------------------------------------------------");
|
||||
|
||||
var query = new InternalItemsQuery
|
||||
{
|
||||
IncludeItemTypes = [BaseItemKind.Movie, BaseItemKind.Series],
|
||||
Recursive = true
|
||||
};
|
||||
|
||||
List<BaseItem> allItems = [.. _libraryManager.GetItemsResult(query).Items];
|
||||
|
||||
_loggingHelper.LogInformation("Total items to scan: {ItemCount}", allItems.Count);
|
||||
_loggingHelper.LogDebugInformation("Items found: {AllItems}", allItems);
|
||||
|
||||
List<BaseItem> series = [.. allItems.Where(item => item.GetBaseItemKind() == BaseItemKind.Series)];
|
||||
List<BaseItem> movies = [.. allItems.Where(item => item.GetBaseItemKind() == BaseItemKind.Movie)];
|
||||
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogDebugInformation("Starting scan of series items.");
|
||||
|
||||
List<BaseItem> staleSeasons = [.. series.SelectMany(GetStaleSeasons)];
|
||||
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogDebugInformation("End of scan for series items.");
|
||||
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogDebugInformation("Starting scan of movie items.");
|
||||
|
||||
List<BaseItem> staleMovies = [.. GetStaleMovies(movies)];
|
||||
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogDebugInformation("End of scan for movie items.");
|
||||
|
||||
_loggingHelper.LogInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogInformation("Stale seasons found: {StaleSeasons}", staleSeasons.Count);
|
||||
|
||||
IEnumerable<MediaInfo> staleSeriesInfo = [];
|
||||
|
||||
if (staleSeasons.Count > 0)
|
||||
{
|
||||
staleSeriesInfo = FindSeriesInfo(staleSeasons);
|
||||
|
||||
foreach (SeriesInfo seriesInfo in staleSeriesInfo.Cast<SeriesInfo>())
|
||||
{
|
||||
_loggingHelper.LogInformation("Series Info: ID: {Id} | Series Name: {SeriesName} | Stale Seasons: {Seasons}", [seriesInfo.Id, seriesInfo.Name, string.Join(", ", seriesInfo.Seasons)]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_loggingHelper.LogInformation("No stale seasons found!");
|
||||
}
|
||||
|
||||
_loggingHelper.LogInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogInformation("Stale Movies found: {StaleMovies}", staleMovies.Count);
|
||||
|
||||
IEnumerable<MediaInfo> staleMoviesInfo = [];
|
||||
|
||||
if (staleMovies.Count > 0)
|
||||
{
|
||||
staleMoviesInfo = staleMovies.Select(movie => new MovieInfo
|
||||
{
|
||||
Id = movie.Id,
|
||||
Name = movie.Name
|
||||
});
|
||||
|
||||
foreach (MovieInfo movieInfo in staleMoviesInfo.Cast<MovieInfo>())
|
||||
{
|
||||
_loggingHelper.LogInformation("Movie Info: ID: {Id} | Movie Name: {MovieName}", [movieInfo.Id, movieInfo.Name]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_loggingHelper.LogInformation("No stale movies found!");
|
||||
}
|
||||
|
||||
_loggingHelper.LogInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogInformation("Ending stale media scan...");
|
||||
_loggingHelper.LogInformation("-------------------------------------------------");
|
||||
|
||||
IEnumerable<MediaInfo> mediaInfo = staleSeriesInfo.Concat(staleMoviesInfo);
|
||||
|
||||
return mediaInfo;
|
||||
}
|
||||
|
||||
private List<BaseItem> GetStaleMovies(List<BaseItem> movies)
|
||||
{
|
||||
List<BaseItem> staleMovies = [];
|
||||
|
||||
try
|
||||
{
|
||||
staleMovies.AddRange(movies.Where(_movieHelper.IsMovieStale));
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_loggingHelper.LogInformation("Arguement Null Exception in GetStaleMovies!");
|
||||
_loggingHelper.LogInformation(ex.Message);
|
||||
}
|
||||
|
||||
return staleMovies;
|
||||
}
|
||||
|
||||
|
||||
private List<BaseItem> GetStaleSeasons(BaseItem item)
|
||||
{
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogDebugInformation("Debug data for series: {SeriesName}", item.Name);
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
|
||||
var seasons = _libraryManager.GetItemList(new InternalItemsQuery
|
||||
{
|
||||
ParentId = item.Id,
|
||||
Recursive = false
|
||||
});
|
||||
|
||||
List<BaseItem> staleSeasons = [ ..seasons
|
||||
.Where(season => {
|
||||
var episodes = _libraryManager.GetItemList(new InternalItemsQuery
|
||||
{
|
||||
ParentId = season.Id,
|
||||
Recursive = false
|
||||
});
|
||||
|
||||
_loggingHelper.LogDebugInformation("Season debug information for {SeasonNumber}:", season);
|
||||
|
||||
bool isSeasonDataStale = false;
|
||||
|
||||
try
|
||||
{
|
||||
isSeasonDataStale = _seriesHelper.IsSeasonDataStale(episodes);
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_loggingHelper.LogInformation("Arguement Null Exception in GetStaleSeasons!");
|
||||
_loggingHelper.LogInformation(ex.Message);
|
||||
}
|
||||
|
||||
_loggingHelper.LogDebugInformation("End of season debug information for {SeasonNumber}.", season);
|
||||
|
||||
return isSeasonDataStale;
|
||||
})];
|
||||
|
||||
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogDebugInformation("End of scanning for series: {Series}", item);
|
||||
|
||||
return staleSeasons;
|
||||
}
|
||||
|
||||
private IEnumerable<MediaInfo> FindSeriesInfo(IReadOnlyCollection<BaseItem> seasons)
|
||||
{
|
||||
Guid[] seriesIds = [.. seasons.Select(season => season.ParentId).Distinct()];
|
||||
|
||||
IReadOnlyCollection<BaseItem> series = _libraryManager.GetItemList(new InternalItemsQuery
|
||||
{
|
||||
ItemIds = seriesIds
|
||||
});
|
||||
|
||||
IEnumerable<SeriesInfo> seriesInfoList = series.Select(series =>
|
||||
{
|
||||
return new SeriesInfo
|
||||
{
|
||||
Id = series.Id,
|
||||
Name = series.Name,
|
||||
Seasons = [.. seasons.Where(season => season.ParentId == series.Id).Select(season => season.Name)]
|
||||
};
|
||||
});
|
||||
|
||||
return seriesInfoList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user