17 Commits

Author SHA1 Message Date
629d5d1f2e Merge pull request 'Media-Cleaner-Homepage-and-Api' (#7) from Media-Cleaner-Homepage-and-Api into main
Reviewed-on: #7
2026-01-27 08:16:01 -07:00
1bd4935232 Merge branch 'main' into Media-Cleaner-Homepage-and-Api 2026-01-27 08:15:49 -07:00
984db9e089 Bumped build version 2026-01-27 08:10:20 -07:00
e77f7dcd5f Cleaned up media_cleaner_table.js and renamed to home.js. Updated table with no data to have a message. 2026-01-27 08:09:03 -07:00
669f59db15 Managed to move js styling to css file and inject it during load. 2026-01-26 08:15:53 -07:00
a69f9df4e1 Disabled buttons with no click handlers and added comments. Also updated Settings button styling 2026-01-25 23:02:05 -07:00
e99ce96563 Added styling for tables and also converted settings anchor to a button to enable styling 2026-01-25 22:55:00 -07:00
56403b5722 Updated plugin to refresh page and state whenever the page is shown 2026-01-25 15:34:59 -07:00
ebe24e2630 Added barebones UI for Media Cleaner 2026-01-25 15:07:55 -07:00
04ef815a9b 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. 2026-01-25 14:52:47 -07:00
66716a9bc9 Updated logging to be more concise when debug is not active 2026-01-25 09:15:26 -07:00
9bddc59fe2 Further improvement to logging formatting and readability 2026-01-25 09:11:45 -07:00
82441d5247 Improved logging futher and updated some methods to use LogDebugInformation and to handle exceptions 2026-01-25 08:58:46 -07:00
5ba270c9a0 Merge pull request 'Media cleaner logging improvements and refactors' (#6) from Media-Cleaner-Homepage-and-Api into main
Reviewed-on: #6
2026-01-24 23:16:39 -07:00
4fc8b4799d Continued to improve logging and fixed a few bugs introduced by refactor 2026-01-24 23:14:16 -07:00
9e324f14a7 Significantly refactored code to help with readability of logging and episode processing 2026-01-24 08:34:42 -07:00
d024035d07 Managed to figure out how to use javascript on the plugin page by utilizing data-controller as found in other repos. Unsure how this is used, but appears to be how you can attach a js file to a div. Also implemented a basic state api to build off of in future. 2026-01-20 20:32:37 -07:00
17 changed files with 652 additions and 429 deletions

View File

@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<AssemblyVersion>0.0.0.9</AssemblyVersion>
<AssemblyVersion>0.0.0.11</AssemblyVersion>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,32 @@
using Jellyfin.Plugin.MediaCleaner.Data;
using Jellyfin.Plugin.MediaCleaner;
using Jellyfin.Plugin.MediaCleaner.Models;
using Microsoft.AspNetCore.Mvc;
using Jellyfin.Plugin.MediaCleaner.Configuration;
namespace Jellyfin.Plugin.MediaCleaner.Controllers;
[Route("mediacleaner/state")]
public class StateController(MediaCleanerState state) : Controller
{
private readonly MediaCleanerState _state = state;
private static PluginConfiguration Configuration =>
Plugin.Instance!.Configuration;
[HttpGet("getSeriesInfo")]
public IActionResult GetSeriesInfo() => Ok(_state.GetSeriesInfo());
[HttpGet("getMovieInfo")]
public IActionResult GetMovieInfo() => Ok(_state.GetMovieInfo());
[HttpGet("updateState")]
public IActionResult GetUpdateState() => Ok(_state.UpdateState());
[HttpGet("getMoviesTitle")]
public IActionResult GetMoviesTitle() =>
Ok($"Stale Movies (Unwatched for and created over {Configuration.StaleMediaCutoff} Days ago.)");
[HttpGet("getSeriesTitle")]
public IActionResult GetSeriesTitle() =>
Ok($"Stale Series (Unwatched for and created over {Configuration.StaleMediaCutoff} Days ago.)");
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Database.Implementations.ModelConfiguration;
using Jellyfin.Plugin.MediaCleaner;
using Jellyfin.Plugin.MediaCleaner.Models;
using Jellyfin.Plugin.MediaCleaner.ScheduledTasks;
using MediaBrowser.Controller.Library;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.MediaCleaner.Data;
public class MediaCleanerState(ILogger<StaleMediaScanner> logger, ILibraryManager libraryManager)
{
private readonly Lock _lock = new();
private IEnumerable<MediaInfo> _mediaInfo = [];
private readonly StaleMediaScanner _staleMediaScanner = new(logger, libraryManager);
public async Task UpdateState()
{
_mediaInfo = await _staleMediaScanner.ScanStaleMedia().ConfigureAwait(false);
}
public IEnumerable<SeriesInfo> GetSeriesInfo()
{
lock (_lock)
{
return _mediaInfo.OfType<SeriesInfo>();
}
}
public IEnumerable<MovieInfo> GetMovieInfo()
{
lock (_lock)
{
return _mediaInfo.OfType<MovieInfo>();
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Jellyfin.Plugin.MediaCleaner.Configuration;
using Jellyfin.Plugin.MediaCleaner.Models;
@@ -10,150 +11,25 @@ using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.MediaCleaner.Helpers;
public class LoggingHelper
public class LoggingHelper(ILogger logger)
{
private readonly ILogger _logger;
private readonly ILogger _logger = logger ?? throw new ArgumentNullException(nameof(logger));
public LoggingHelper(ILogger logger)
[SuppressMessage("Microsoft.Performance", "CA2254:TemplateShouldBeConstant", Justification = "Message parameter is intentionally variable for flexible debug logging")]
public void LogDebugInformation(string message, params object?[] args)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
if (Configuration.DebugMode)
{
_logger.LogInformation(message, args);
}
}
[SuppressMessage("Microsoft.Performance", "CA2254:TemplateShouldBeConstant", Justification = "Message parameter is intentionally variable for flexible logging")]
public void LogInformation(string message, params object?[] args)
{
_logger.LogInformation(message, args);
}
private static PluginConfiguration Configuration =>
Plugin.Instance!.Configuration;
public void StartLogging()
{
if (Configuration.DebugMode)
{
_logger.LogInformation("--DEBUG MODE ACTIVE--");
}
_logger.LogInformation("-------------------------------------------------");
_logger.LogInformation("Starting stale media scan...");
}
public void EndLogging()
{
_logger.LogInformation("Ending stale media scan...");
_logger.LogInformation("-------------------------------------------------");
}
public void PrintDebugEndOfScanningForSeries(BaseItem item)
{
if (Configuration.DebugMode)
{
_logger.LogInformation("End of scanning for series: {Series}", item);
_logger.LogInformation("-------------------------------------------------");
}
}
public void StartScanningSeriesItems()
{
_logger.LogInformation("-------------------------------------------------");
_logger.LogInformation("Starting scan of series items.");
_logger.LogInformation("-------------------------------------------------");
}
public void StartScanningMoviesItems()
{
_logger.LogInformation("-------------------------------------------------");
_logger.LogInformation("Starting scan of movies items.");
_logger.LogInformation("-------------------------------------------------");
}
public void PrintItemsInformation(IReadOnlyCollection<BaseItem> items)
{
ArgumentNullException.ThrowIfNull(items);
_logger.LogInformation("Total items: {ItemCount}", items.Count);
_logger.LogInformation("Stale items found: {AllItems}", items);
}
public void PrintStaleMoviesInformation(IReadOnlyCollection<BaseItem> staleMovies)
{
ArgumentNullException.ThrowIfNull(staleMovies);
_logger.LogInformation("-------------------------------------------------");
_logger.LogInformation("Stale Movies found: {StaleMovies}", staleMovies.Count);
if (staleMovies.Count > 0 && Configuration.DebugMode)
{
foreach (var movieInfo in staleMovies)
{
_logger.LogInformation("Movie Info: ID: {Id} | Movie Name: {MovieName}", [movieInfo.Id, movieInfo.Name]);
}
}
}
public void PrintStaleEpisodesInformation(Func<IReadOnlyCollection<BaseItem>, List<SeriesInfo>> findSeriesInfoFromEpisodes, IReadOnlyCollection<BaseItem> staleEpisodes)
{
ArgumentNullException.ThrowIfNull(staleEpisodes);
ArgumentNullException.ThrowIfNull(findSeriesInfoFromEpisodes);
_logger.LogInformation("-------------------------------------------------");
_logger.LogInformation("Stale Episodes found: {StaleEpisodes}", staleEpisodes.Count);
if (staleEpisodes.Count > 0 && Configuration.DebugMode)
{
if (findSeriesInfoFromEpisodes == null)
{
throw new ArgumentNullException(nameof(findSeriesInfoFromEpisodes), "The method to find series information cannot be null.");
}
List<SeriesInfo> seriesInfoList = findSeriesInfoFromEpisodes(staleEpisodes);
foreach (var seriesInfo in seriesInfoList)
{
_logger.LogInformation("Series Info: ID: {Id} | Series Name: {SeriesName} | Stale Seasons: {Seasons}", [seriesInfo.Id, seriesInfo.SeriesName, string.Join(", ", seriesInfo.Seasons)]);
}
}
_logger.LogInformation("-------------------------------------------------");
}
public void PrintDebugDataForSeries(BaseItem item)
{
ArgumentNullException.ThrowIfNull(item);
if (Configuration.DebugMode)
{
_logger.LogInformation("-------------------------------------------------");
_logger.LogInformation("Debug data for series: {SeriesName}", item.Name);
_logger.LogInformation("-------------------------------------------------");
}
}
public void PrintDebugSeasonInfo()
{
if (Configuration.DebugMode)
{
_logger.LogInformation("Season debug information:");
}
}
public void PrintDebugEpisodesWithUserData(IReadOnlyCollection<BaseItem> episodesWithUserData)
{
if(Configuration.DebugMode){
_logger.LogInformation("Episodes with user data: {EpisodesWithUserData}", episodesWithUserData);
_logger.LogInformation("-------------------------------------------------");
}
}
public void PrintDebugEpisodeCreationInfo(IReadOnlyCollection<BaseItem> episodes)
{
ArgumentNullException.ThrowIfNull(episodes);
if(Configuration.DebugMode){
_logger.LogInformation("-------------------------------------------------");
_logger.LogInformation("Episode creation dates:");
_logger.LogInformation("-------------------------------------------------");
foreach(BaseItem episode in episodes)
{
_logger.LogInformation("Episode: {EpisodeName} | Date Created: {EpisodeDateCreated}", [episode.Name, episode.DateCreated]);
}
_logger.LogInformation("-------------------------------------------------");
}
}
}

View File

@@ -8,26 +8,20 @@ using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.MediaCleaner.Helpers;
public class MovieHelper
public class MovieHelper(ILogger logger)
{
private readonly ILogger _logger;
public MovieHelper(ILogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
private readonly LoggingHelper _loggingHelper = new(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("-------------------------------------------------");
}
ArgumentNullException.ThrowIfNull(movie, "IsMovieStale process recieved a null movie. Logic failure. Exception thrown.");
_loggingHelper.LogDebugInformation("-------------------------------------------------");
_loggingHelper.LogDebugInformation("Scanning movie: {Movie}", movie);
_loggingHelper.LogDebugInformation("-------------------------------------------------");
bool movieIsStale = false;
@@ -38,41 +32,41 @@ public class MovieHelper
{
var mostRecentUserData = movie.UserData.OrderByDescending(data => data.LastPlayedDate).First(data => data.LastPlayedDate != null);
if (Configuration.DebugMode){
_logger.LogInformation("Most recent user data: {Movie}", movie);
_loggingHelper.LogDebugInformation("Most recent user data: {Movie}", movie);
foreach (var property in typeof(UserData).GetProperties())
{
_logger.LogInformation("{PropertyName}: {PropertyValue}", property.Name, property.GetValue(mostRecentUserData));
}
_logger.LogInformation("-------------------------------------------------");
foreach (var property in typeof(UserData).GetProperties())
{
_loggingHelper.LogDebugInformation("{PropertyName}: {PropertyValue}", property.Name, property.GetValue(mostRecentUserData));
}
_loggingHelper.LogDebugInformation("-------------------------------------------------");
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);
}
_loggingHelper.LogDebugInformation("Most recent user data has last played date that is outside of cutoff.");
_loggingHelper.LogDebugInformation("Adding {Movie} to stale movies.", movie);
_loggingHelper.LogDebugInformation("With Last Played Date: {LastPlayedDate}", mostRecentUserData.LastPlayedDate);
movieIsStale = true;
}
}
else if (createdOutsideCutoff)
if (createdOutsideCutoff && !hasUserData)
{
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]);
}
_loggingHelper.LogDebugInformation("Movie has no user data and was created outside of cutoff: {DateCreated}.", movie.DateCreated);
_loggingHelper.LogDebugInformation("Adding {Movie} to stale movies.", movie);
movieIsStale = true;
}
if (Configuration.DebugMode)
if (!createdOutsideCutoff && !hasUserData)
{
_logger.LogInformation("-------------------------------------------------");
_logger.LogInformation("End of scanning for movie: {Movie}", movie);
_logger.LogInformation("-------------------------------------------------");
_loggingHelper.LogDebugInformation("Movie has no user data and was not created outside of cutoff: {DateCreated}.", movie.DateCreated);
_loggingHelper.LogDebugInformation("Movie is not stale.");
}
_loggingHelper.LogDebugInformation("-------------------------------------------------");
_loggingHelper.LogDebugInformation("End of scanning for movie: {Movie}", movie);
return movieIsStale;
}

View File

@@ -11,71 +11,77 @@ using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.MediaCleaner.Helpers;
public class SeriesHelper
public class SeriesHelper(ILogger logger)
{
private readonly ILogger _logger;
private readonly LoggingHelper _loggingHelper;
public SeriesHelper(ILogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_loggingHelper = new LoggingHelper(logger);
}
private readonly LoggingHelper _loggingHelper = new(logger);
private static PluginConfiguration Configuration =>
Plugin.Instance!.Configuration;
public bool IsSeasonUserDataStale(IReadOnlyList<BaseItem> episodes)
private List<BaseItem> ProcessEpisodes(IReadOnlyCollection<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)
List<BaseItem> staleEpisodes = [.. episodes
.Where(episode =>
{
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);
var hasUserDataWithLastPlayedDate = episode.UserData.Any(data => data.LastPlayedDate != null);
_loggingHelper.LogDebugInformation("-------------------------------------------------");
_loggingHelper.LogDebugInformation("Debug data for episode: {Episode}", episode);
_loggingHelper.LogDebugInformation("-------------------------------------------------");
if (staleCreationDate && !hasUserDataWithLastPlayedDate){
_loggingHelper.LogDebugInformation("Creation date is stale, and no user data for episode {Episode}.", episode);
_loggingHelper.LogDebugInformation("Date created: {DateCreated}", episode.DateCreated);
episodeIsStale = true;
}
if (hasUserDataWithLastPlayedDate){
UserData mostRecentUserData = episode.UserData
.OrderByDescending(data => data.LastPlayedDate)
.First();
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));
_loggingHelper.LogDebugInformation("{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);
_loggingHelper.LogDebugInformation("-------------------------------------------------");
bool staleLastPlayedDate = mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff);
if (staleLastPlayedDate && staleCreationDate)
{
episodeIsStale = true;
_loggingHelper.LogDebugInformation("Most recent user data has a last played date of: {LastPlayedDate}.", [mostRecentUserData.LastPlayedDate]);
_loggingHelper.LogDebugInformation("Episode created {DateCreated}.", episode.DateCreated);
_loggingHelper.LogDebugInformation("Episode is marked as stale.");
}
}
if (episodeIsStale)
{
staleEpisodes.Add(episode);
if(Configuration.DebugMode){
_logger.LogInformation("Episode is stale.");
}
}
}
return episodeIsStale;
})];
return staleEpisodes;
}
public bool IsSeasonDataStale(IReadOnlyList<BaseItem> episodes)
{
ArgumentNullException.ThrowIfNull(episodes, "IsSeasonDataStale process recieved null episodes. Logic failure. Exception thrown.");
bool seasonIsStale = false;
List<BaseItem> staleEpisodes = ProcessEpisodes(episodes);
if(staleEpisodes.Count == episodes.Count)
{
seasonIsStale = true;
_logger.LogInformation("Stale episodes count matches all episode count. Season is stale.");
_logger.LogInformation("-------------------------------------------------");
_loggingHelper.LogDebugInformation("-------------------------------------------------");
_loggingHelper.LogDebugInformation("Stale episodes count matches season episode count. Season is stale.");
_loggingHelper.LogDebugInformation("-------------------------------------------------");
}
return seasonIsStale;

View File

@@ -22,8 +22,8 @@
<ItemGroup>
<None Remove="Configuration\settings.html" />
<EmbeddedResource Include="Configuration\settings.html" />
<None Remove="Pages\home.html" />
<EmbeddedResource Include="Pages\home.html" />
<None Remove="Pages\*" />
<EmbeddedResource Include="Pages\*" />
</ItemGroup>
<PropertyGroup>

View File

@@ -0,0 +1,9 @@
using System;
namespace Jellyfin.Plugin.MediaCleaner.Models;
public abstract class MediaInfo
{
public required Guid Id { get; set; }
public required string Name { get; set; }
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Jellyfin.Plugin.MediaCleaner;
namespace Jellyfin.Plugin.MediaCleaner.Models;
/// <summary>
/// Contains Movie information.
/// </summary>
public class MovieInfo : MediaInfo
{
}

View File

@@ -1,30 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Jellyfin.Plugin.MediaCleaner;
namespace Jellyfin.Plugin.MediaCleaner.Models;
/// <summary>
/// Contains series information.
/// </summary>
public class SeriesInfo
public class SeriesInfo : MediaInfo
{
/// <summary>
/// Gets or sets series identifier.
/// </summary>
public Guid Id { get; set; }
/// <summary>
/// Gets or sets series name.
/// </summary>
public string SeriesName { get; set; } = string.Empty;
/// <summary>
/// Gets or sets seasons.
/// </summary>
#pragma warning disable CA2227 // Collection properties should be read only
#pragma warning disable CA1002 // Do not expose generic lists
public List<string> Seasons { get; set; } = [];
#pragma warning restore CA1002 // Do not expose generic lists
#pragma warning restore CA2227 // Collection properties should be read only
public IEnumerable<string> Seasons { get; set; } = [];
}

View File

@@ -0,0 +1,27 @@
table {
border: 1px solid;
border-collapse: collapse;
}
td, th {
border: 1px solid;
padding: 0.5rem 0.75rem;
text-align: left;
}
.links {
background-color: #0f0f0f;
border: 1px solid;
padding: 0.8rem 1.8rem;
font-size: 1.2rem;
color: #ffffff;
text-decoration: none;
cursor: pointer;
line-height: inherit;
vertical-align: baseline;
transition: background-color 0.3s ease;
}
.links:hover {
background-color: #2a2a2a;
}

View File

@@ -1,11 +1,36 @@
<div data-role="page" class="page type-interior pluginConfigurationPage withTabs">
<div data-role="page" class="page type-interior pluginConfigurationPage"
data-controller="__plugin/home.js">
<div data-role="content">
<div class="content-primary">
<div>
<a href="#configurationpage?name=Home">Home</a>
<a href="#configurationpage?name=Settings">Settings</a>
<div id="loading">Loading...</div>
<div id="homepage" style="visibility: hidden;">
<button class="links" data-target="configurationpage?name=Settings">Settings</button>
<h2>Media Cleaner</h2>
<h3 id="moviesTitle"></h3>
<table id="moviesTable">
<thead>
<tr>
<th>Name</th>
<th class="actions-cell">Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
<br>
<h3 id="seriesTitle"></h3>
<table id="seriesTable">
<thead>
<tr>
<th>Name</th>
<th>Seasons</th>
<th class="actions-cell">Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<h2>Media Cleaner</h2>
</div>
</div>
</div>

View File

@@ -0,0 +1,151 @@
document.addEventListener('pageshow', async () => {
await fetchHomepageCSS();
await updateMediaCleanerState();
var moviesTitle = document.getElementById("moviesTitle");
var seriesTitle = document.getElementById("seriesTitle");
moviesTitle.innerHTML = await getMediaCleanerMoviesTitle();
seriesTitle.innerHTML = await getMediaCleanerSeriesTitle();
await populateTables();
addClickHandlersToLinks();
finishLoading();
});
const getMediaCleanerSeriesInfo = async () => {
const response = await fetch("/mediacleaner/state/getSeriesInfo");
if(!response.ok){
throw new Error(`Response status: ${response.status}`)
}
return response.json();
};
const getMediaCleanerMovieInfo = async () => {
const response = await fetch("/mediacleaner/state/getMovieInfo");
if(!response.ok){
throw new Error(`Response status: ${response.status}`)
}
return response.json();
};
const updateMediaCleanerState = async () => {
const response = await fetch("/mediacleaner/state/updateState");
if(!response.ok){
throw new Error(`Response status: ${response.status}`)
}
return response.json();
};
const getMediaCleanerSeriesTitle = async () => {
const response = await fetch("/mediacleaner/state/getSeriesTitle");
if(!response.ok){
throw new Error(`Response status: ${response.status}`);
}
return response.json();
};
const getMediaCleanerMoviesTitle = async () => {
const response = await fetch("/mediacleaner/state/getMoviesTitle");
if(!response.ok){
throw new Error(`Response status: ${response.status}`);
}
return response.json();
};
const populateTables = async () => {
var moviesInfo = await getMediaCleanerMovieInfo();
var seriesInfo = await getMediaCleanerSeriesInfo();
var seriesTableBody = seriesTable.getElementsByTagName('tbody')[0];
seriesTableBody.replaceChildren();
var moviesTableBody = moviesTable.getElementsByTagName('tbody')[0];
moviesTableBody.replaceChildren();
if (moviesInfo.length > 0){
for(let i = 0; i < moviesInfo.length; i++){
var row = moviesTableBody.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = moviesInfo[i].Name;
// Will need to be enabled once radarr and sonarr integration is enabled.
// Maybe change this to an element to remove hard coding.
cell2.innerHTML = "<button type=\"button\" disabled>Delete</button>";
}
}
else{
var columnCount = moviesTable.tHead.rows[0].cells.length;
var row = moviesTableBody.insertRow(-1);
var cell1 = row.insertCell(0);
cell1.colSpan = columnCount;
cell1.innerHTML = "No stale movies found.";
}
if(seriesInfo.length > 0){
for(let i = 0; i < seriesInfo.length; i++){
var row = seriesTableBody.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = seriesInfo[i].Name;
cell2.innerHTML = seriesInfo[i].Seasons.map(season => season.replace("Season ", "")).join(", ");
// Will need to be enabled once radarr and sonarr integration is enabled.
// Maybe change this to an element to remove hard coding.
cell3.innerHTML = "<button type=\"button\" disabled>Delete</button>";
cell3.className = "actions-cell";
}
}
else{
var columnCount = seriesTable.tHead.rows[0].cells.length;
var row = seriesTableBody.insertRow(-1);
var cell1 = row.insertCell(0);
cell1.colSpan = columnCount;
cell1.innerHTML = "No stale series found.";
}
};
const addClickHandlersToLinks = () => {
const linkbtns = document.querySelectorAll("button.links")
linkbtns.forEach(btn => {
btn.addEventListener("click", () => {
const target = btn.dataset.target;
if (!target) return;
window.location.hash = target;
})
})
}
const finishLoading = () => {
const loadingElement = document.getElementById("loading");
const homepage = document.getElementById("homepage");
loadingElement.style.visibility = "hidden";
homepage.style.visibility = "visible";
console.log("Loading element: ", loadingElement);
console.log("Homepage element: ", homepage);
}
const fetchHomepageCSS = async () => {
const response = await fetch('/web/configurationpage?name=home.css')
if(!response.ok){
throw new Error(`Response status: ${response.status}`);
}
const css = await response.text();
const styles = document.createElement('style');
styles.textContent = css;
document.head.appendChild(styles);
}

View File

@@ -2,11 +2,13 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Jellyfin.Plugin.MediaCleaner.Configuration;
using Jellyfin.Plugin.MediaCleaner.Data;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.DependencyInjection;
namespace Jellyfin.Plugin.MediaCleaner;
@@ -53,6 +55,16 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
EmbeddedResourcePath = string.Format(CultureInfo.InvariantCulture, "{0}.Pages.home.html", GetType().Namespace),
EnableInMainMenu = true,
},
new PluginPageInfo
{
Name = "home.js",
EmbeddedResourcePath = string.Format(CultureInfo.InvariantCulture, "{0}.Pages.home.js", GetType().Namespace),
},
new PluginPageInfo
{
Name = "home.css",
EmbeddedResourcePath = string.Format(CultureInfo.InvariantCulture, "{0}.Pages.home.css", GetType().Namespace),
}
];
}
}

View File

@@ -0,0 +1,13 @@
using Jellyfin.Plugin.MediaCleaner.Data;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Plugins;
using Microsoft.Extensions.DependencyInjection;
namespace Jellyfin.Plugin.MediaCleaner;
public class PluginServiceRegistrator : IPluginServiceRegistrator
{
public void RegisterServices(IServiceCollection serviceCollection, IServerApplicationHost applicationHost)
{
serviceCollection.AddSingleton<MediaCleanerState>();
}
}

View File

@@ -1,187 +0,0 @@
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.Models;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.MediaCleaner.ScheduledTasks;
/// <summary>
/// A task to scan media for stale files.
/// </summary>
public sealed class StaleMediaTask : IScheduledTask
{
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 StaleMediaTask(ILogger<StaleMediaTask> logger, ILibraryManager libraryManager)
{
_logger = logger;
_libraryManager = libraryManager;
_loggingHelper = new LoggingHelper(_logger);
_movieHelper = new MovieHelper(_logger);
_seriesHelper = new SeriesHelper(_logger);
}
private static PluginConfiguration Configuration =>
Plugin.Instance!.Configuration;
string IScheduledTask.Name => "Scan Stale Media";
string IScheduledTask.Key => "Stale Media";
string IScheduledTask.Description => "Scan Stale Media";
string IScheduledTask.Category => "Media";
Task IScheduledTask.ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
_loggingHelper.StartLogging();
var query = new InternalItemsQuery
{
IncludeItemTypes = [BaseItemKind.Movie, BaseItemKind.Series],
Recursive = true
};
List<BaseItem> allItems = [.. _libraryManager.GetItemsResult(query).Items];
_loggingHelper.PrintItemsInformation(allItems);
List<BaseItem> series = [.. allItems.Where(item => item.GetBaseItemKind() == BaseItemKind.Series)];
List<BaseItem> movies = [.. allItems.Where(item => item.GetBaseItemKind() == BaseItemKind.Movie)];
_loggingHelper.StartScanningSeriesItems();
List<BaseItem> staleEpisodes = [.. series.SelectMany(GetStaleEpisodes)];
_loggingHelper.StartScanningMoviesItems();
List<BaseItem> staleMovies = [.. GetStaleMovies(movies)];
_loggingHelper.PrintStaleMoviesInformation(staleMovies);
_loggingHelper.PrintStaleEpisodesInformation(FindSeriesInfoFromEpisodes, staleEpisodes);
_loggingHelper.EndLogging();
return Task.CompletedTask;
}
private List<BaseItem> GetStaleMovies(List<BaseItem> movies)
{
List<BaseItem> staleMovies = [];
staleMovies.AddRange(movies.Where(_movieHelper.IsMovieStale));
return staleMovies;
}
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
});
_loggingHelper.PrintDebugDataForSeries(item);
foreach (var season in seasons)
{
// Gets each episode, to access user data.
var episodes = _libraryManager.GetItemList(new InternalItemsQuery
{
ParentId = season.Id,
Recursive = false
});
_loggingHelper.PrintDebugSeasonInfo();
bool seasonHasUserData = episodes.Any(episode => episode.UserData.Count > 0);
bool seasonIsStale = seasonHasUserData && _seriesHelper.IsSeasonUserDataStale(episodes);
if (seasonIsStale)
{
if (!seasonHasUserData)
{
_loggingHelper.PrintDebugEpisodeCreationInfo(episodes);
}
staleEpisodes.AddRange(episodes);
}
}
_loggingHelper.PrintDebugEndOfScanningForSeries(item);
return staleEpisodes;
}
private List<SeriesInfo> FindSeriesInfoFromEpisodes(IReadOnlyCollection<BaseItem> episodes)
{
Guid[] seasonIds = [.. episodes.Select(episode => episode.ParentId).Distinct()];
var seasons = _libraryManager.GetItemList(new InternalItemsQuery
{
ItemIds = seasonIds
});
Guid[] seriesIds = [.. seasons.Select(season => season.ParentId).Distinct()];
var series = _libraryManager.GetItemList(new InternalItemsQuery
{
ItemIds = seriesIds
}).ToList();
List<string> seriesNames = [.. series.Select(series => series.Name).Distinct()];
List<SeriesInfo> seriesInfoList = [];
series.ForEach(series =>
{
seriesInfoList.Add(new SeriesInfo
{
Id = series.Id,
SeriesName = series.Name,
Seasons = [.. seasons.Where(season => season.ParentId == series.Id).Select(season => season.Name)]
});
});
return seriesInfoList;
}
IEnumerable<TaskTriggerInfo> IScheduledTask.GetDefaultTriggers()
{
// Run this task every 24 hours
yield return new TaskTriggerInfo
{
Type = TaskTriggerInfoType.IntervalTrigger,
IntervalTicks = TimeSpan.FromHours(24).Ticks
};
}
}

View 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;
}
}