Compare commits
44 Commits
v0.0.1-alp
...
629d5d1f2e
| Author | SHA1 | Date | |
|---|---|---|---|
| 629d5d1f2e | |||
| 1bd4935232 | |||
| 984db9e089 | |||
| e77f7dcd5f | |||
| 669f59db15 | |||
| a69f9df4e1 | |||
| e99ce96563 | |||
| 56403b5722 | |||
| ebe24e2630 | |||
| 04ef815a9b | |||
| 66716a9bc9 | |||
| 9bddc59fe2 | |||
| 82441d5247 | |||
| 5ba270c9a0 | |||
| 4fc8b4799d | |||
| 9e324f14a7 | |||
| d024035d07 | |||
| 8d85194df5 | |||
| d78d1069b1 | |||
| f7c463aba4 | |||
| 77f2873180 | |||
| b2da7beb00 | |||
| 9696826406 | |||
| 873b29985c | |||
| 30107010b1 | |||
| 5af49eb390 | |||
| ee44dd9ee6 | |||
| 9604289684 | |||
| ddf84b5cb6 | |||
| ca1d1fcab1 | |||
| 3b43d6807a | |||
| 2c91846d81 | |||
| c533eb4f90 | |||
| 2fdbae74e8 | |||
| 08e26943c9 | |||
| 65dd638b09 | |||
| 6b20bc0828 | |||
| db3a06cc67 | |||
| 1a4cefba40 | |||
| 2387fecfb6 | |||
| 1ddb840f4a | |||
| c001a4f2ca | |||
| 08747baf51 | |||
| 41c28f095f |
@@ -1,7 +1,5 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<Version>0.0.0.1</Version>
|
||||
<AssemblyVersion>0.0.0.1</AssemblyVersion>
|
||||
<FileVersion>0.0.0.1</FileVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<AssemblyVersion>0.0.0.11</AssemblyVersion>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
||||
@@ -31,4 +31,9 @@ public class PluginConfiguration : BasePluginConfiguration
|
||||
/// Gets or sets the cut off days before deleting unwatched files.
|
||||
/// </summary>
|
||||
public int StaleMediaCutoff { get; set; } = 90;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets debug mode.
|
||||
/// </summary>
|
||||
public bool DebugMode { get; set; }
|
||||
}
|
||||
|
||||
@@ -24,6 +24,12 @@
|
||||
<input id="StaleMediaCutoff" name="StaleMediaCutoff" type="number" is="emby-input" style="width: 20%;"/>
|
||||
<div class="fieldDescription">How many days to wait before marking files as stale</div>
|
||||
</div>
|
||||
<div class="checkboxContainer checkboxContainer-withDescription">
|
||||
<label class="emby-checkbox-label">
|
||||
<input id="DebugMode" name="DebugMode" type="checkbox" is="emby-checkbox" />
|
||||
<span>Debug Mode</span>
|
||||
</label>
|
||||
</div>
|
||||
<!-- <div class="selectContainer">
|
||||
<label class="selectLabel" for="Options">Several Options</label>
|
||||
<select is="emby-select" id="Options" name="Options" class="emby-select-withcolor emby-select">
|
||||
@@ -71,6 +77,7 @@
|
||||
document.querySelector('#RadarrAPIKey').value = config.RadarrAPIKey;
|
||||
document.querySelector('#SonarrAPIKey').value = config.SonarrAPIKey;
|
||||
document.querySelector('#StaleMediaCutoff').value = config.StaleMediaCutoff;
|
||||
document.querySelector('#DebugMode').checked = config.DebugMode;
|
||||
Dashboard.hideLoadingMsg();
|
||||
});
|
||||
});
|
||||
@@ -86,6 +93,7 @@
|
||||
config.RadarrAPIKey = document.querySelector('#RadarrAPIKey').value;
|
||||
config.SonarrAPIKey = document.querySelector('#SonarrAPIKey').value;
|
||||
config.StaleMediaCutoff = document.querySelector('#StaleMediaCutoff').value;
|
||||
config.DebugMode = document.querySelector('#DebugMode').checked;
|
||||
ApiClient.updatePluginConfiguration(MediaCleanerConfig.pluginUniqueId, config).then(function (result) {
|
||||
Dashboard.processPluginConfigurationUpdateResult(result);
|
||||
});
|
||||
32
Jellyfin.Plugin.MediaCleaner/Controllers/StateController.cs
Normal file
32
Jellyfin.Plugin.MediaCleaner/Controllers/StateController.cs
Normal 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.)");
|
||||
}
|
||||
42
Jellyfin.Plugin.MediaCleaner/Data/MediaCleanerState.cs
Normal file
42
Jellyfin.Plugin.MediaCleaner/Data/MediaCleanerState.cs
Normal 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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Jellyfin.Plugin.MediaCleaner/Helpers/LoggingHelper.cs
Normal file
35
Jellyfin.Plugin.MediaCleaner/Helpers/LoggingHelper.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
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;
|
||||
using Jellyfin.Plugin.MediaCleaner.ScheduledTasks;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.MediaCleaner.Helpers;
|
||||
|
||||
public class LoggingHelper(ILogger logger)
|
||||
{
|
||||
private readonly ILogger _logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA2254:TemplateShouldBeConstant", Justification = "Message parameter is intentionally variable for flexible debug logging")]
|
||||
public void LogDebugInformation(string message, params object?[] args)
|
||||
{
|
||||
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;
|
||||
}
|
||||
73
Jellyfin.Plugin.MediaCleaner/Helpers/MovieHelper.cs
Normal file
73
Jellyfin.Plugin.MediaCleaner/Helpers/MovieHelper.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
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(ILogger logger)
|
||||
{
|
||||
private readonly LoggingHelper _loggingHelper = new(logger);
|
||||
|
||||
private static PluginConfiguration Configuration =>
|
||||
Plugin.Instance!.Configuration;
|
||||
|
||||
public bool IsMovieStale(BaseItem movie)
|
||||
{
|
||||
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;
|
||||
|
||||
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).First(data => data.LastPlayedDate != null);
|
||||
|
||||
_loggingHelper.LogDebugInformation("Most recent user data: {Movie}", movie);
|
||||
|
||||
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))
|
||||
{
|
||||
_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;
|
||||
}
|
||||
}
|
||||
|
||||
if (createdOutsideCutoff && !hasUserData)
|
||||
{
|
||||
_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 (!createdOutsideCutoff && !hasUserData)
|
||||
{
|
||||
_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;
|
||||
}
|
||||
|
||||
}
|
||||
89
Jellyfin.Plugin.MediaCleaner/Helpers/SeriesHelper.cs
Normal file
89
Jellyfin.Plugin.MediaCleaner/Helpers/SeriesHelper.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
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(ILogger logger)
|
||||
{
|
||||
private readonly LoggingHelper _loggingHelper = new(logger);
|
||||
|
||||
private static PluginConfiguration Configuration =>
|
||||
Plugin.Instance!.Configuration;
|
||||
|
||||
private List<BaseItem> ProcessEpisodes(IReadOnlyCollection<BaseItem> episodes)
|
||||
{
|
||||
List<BaseItem> staleEpisodes = [.. episodes
|
||||
.Where(episode =>
|
||||
{
|
||||
bool episodeIsStale = false;
|
||||
|
||||
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();
|
||||
|
||||
foreach (var property in typeof(UserData).GetProperties())
|
||||
{
|
||||
_loggingHelper.LogDebugInformation("{PropertyName}: {PropertyValue}", property.Name, property.GetValue(mostRecentUserData));
|
||||
}
|
||||
|
||||
_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.");
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
_loggingHelper.LogDebugInformation("Stale episodes count matches season episode count. Season is stale.");
|
||||
_loggingHelper.LogDebugInformation("-------------------------------------------------");
|
||||
}
|
||||
|
||||
return seasonIsStale;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<RootNamespace>Jellyfin.Plugin.MediaCleaner</RootNamespace>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<Nullable>enable</Nullable>
|
||||
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
|
||||
@@ -11,7 +11,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Jellyfin.Controller" Version="10.11.3" >
|
||||
<PackageReference Include="Jellyfin.Controller" Version="10.11.3">
|
||||
<ExcludeAssets>runtime</ExcludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Jellyfin.Model" Version="10.11.3">
|
||||
@@ -20,14 +20,17 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
|
||||
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" PrivateAssets="All" />
|
||||
<PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
|
||||
<None Remove="Configuration\settings.html" />
|
||||
<EmbeddedResource Include="Configuration\settings.html" />
|
||||
<None Remove="Pages\*" />
|
||||
<EmbeddedResource Include="Pages\*" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Configuration\configPage.html" />
|
||||
<EmbeddedResource Include="Configuration\configPage.html" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<Timestamp>$([System.DateTime]::UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"))</Timestamp>
|
||||
</PropertyGroup>
|
||||
|
||||
<Target Name="GeneratePluginJson" BeforeTargets="Publish">
|
||||
<WriteLinesToFile File="$(PublishDir)\meta.json" Lines="{
 "guid": "fef007a8-3e8f-4aa8-a22e-486a387f4192",
 "name": "Media Cleaner",
 "category": "Library",
 "overview": "A cleaner for your stale media.",
 "description": "Clean out the stale media from your library using scheduled tasks",
 "timestamp": "$(Timestamp)",
 "targetAbi": "10.11.0",
 "owner": "T-Gander",
 "version": "$(AssemblyVersion)"
 }" Overwrite="true" Encoding="utf-8" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
9
Jellyfin.Plugin.MediaCleaner/Models/MediaInfo.cs
Normal file
9
Jellyfin.Plugin.MediaCleaner/Models/MediaInfo.cs
Normal 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; }
|
||||
}
|
||||
14
Jellyfin.Plugin.MediaCleaner/Models/MovieInfo.cs
Normal file
14
Jellyfin.Plugin.MediaCleaner/Models/MovieInfo.cs
Normal 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
|
||||
{
|
||||
|
||||
}
|
||||
14
Jellyfin.Plugin.MediaCleaner/Models/SeriesInfo.cs
Normal file
14
Jellyfin.Plugin.MediaCleaner/Models/SeriesInfo.cs
Normal 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 series information.
|
||||
/// </summary>
|
||||
public class SeriesInfo : MediaInfo
|
||||
{
|
||||
public IEnumerable<string> Seasons { get; set; } = [];
|
||||
}
|
||||
27
Jellyfin.Plugin.MediaCleaner/Pages/home.css
Normal file
27
Jellyfin.Plugin.MediaCleaner/Pages/home.css
Normal 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;
|
||||
}
|
||||
36
Jellyfin.Plugin.MediaCleaner/Pages/home.html
Normal file
36
Jellyfin.Plugin.MediaCleaner/Pages/home.html
Normal file
@@ -0,0 +1,36 @@
|
||||
<div data-role="page" class="page type-interior pluginConfigurationPage"
|
||||
data-controller="__plugin/home.js">
|
||||
<div data-role="content">
|
||||
<div class="content-primary">
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
151
Jellyfin.Plugin.MediaCleaner/Pages/home.js
Normal file
151
Jellyfin.Plugin.MediaCleaner/Pages/home.js
Normal 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);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -44,8 +46,24 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
||||
[
|
||||
new PluginPageInfo
|
||||
{
|
||||
Name = Name,
|
||||
EmbeddedResourcePath = string.Format(CultureInfo.InvariantCulture, "{0}.Configuration.configPage.html", GetType().Namespace)
|
||||
Name = "Settings",
|
||||
EmbeddedResourcePath = string.Format(CultureInfo.InvariantCulture, "{0}.Configuration.settings.html", GetType().Namespace),
|
||||
},
|
||||
new PluginPageInfo
|
||||
{
|
||||
Name = "Home",
|
||||
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),
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
13
Jellyfin.Plugin.MediaCleaner/PluginServiceRegistrator.cs
Normal file
13
Jellyfin.Plugin.MediaCleaner/PluginServiceRegistrator.cs
Normal 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>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#pragma warning disable RS0030 // Do not use banned APIs
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Database.Implementations;
|
||||
using MediaBrowser.Model.Globalization;
|
||||
using MediaBrowser.Model.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.MediaCleaner.ScheduledTasks;
|
||||
|
||||
/// <summary>
|
||||
/// Task to clean up any detached userdata from the database.
|
||||
/// </summary>
|
||||
public class CleanupUserDataTask : IScheduledTask
|
||||
{
|
||||
private readonly ILocalizationManager _localization;
|
||||
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
|
||||
private readonly ILogger<CleanupUserDataTask> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CleanupUserDataTask"/> class.
|
||||
/// </summary>
|
||||
/// <param name="localization">The localisation Provider.</param>
|
||||
/// <param name="dbProvider">The DB context factory.</param>
|
||||
/// <param name="logger">A logger.</param>
|
||||
public CleanupUserDataTask(ILocalizationManager localization, IDbContextFactory<JellyfinDbContext> dbProvider, ILogger<CleanupUserDataTask> logger)
|
||||
{
|
||||
_localization = localization;
|
||||
_dbProvider = dbProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => _localization.GetLocalizedString("CleanupUserDataTask");
|
||||
|
||||
public Guid PlaceholderId => new("00000000-0000-0000-0000-000000000001");
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Description => _localization.GetLocalizedString("CleanupUserDataTaskDescription");
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Category => _localization.GetLocalizedString("Media");
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Key => nameof(CleanupUserDataTask);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
||||
{
|
||||
var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using (dbContext.ConfigureAwait(false))
|
||||
{
|
||||
var detachedUserData = dbContext.UserData.Where(e => e.ItemId == PlaceholderId);
|
||||
_logger.LogInformation("Deleting {DetachedUserDataCount} detached UserData entries.", detachedUserData.Count());
|
||||
|
||||
await detachedUserData.ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
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 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 IUserManager _userManager;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StaleMediaTask"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger.</param>
|
||||
/// <param name="userManager">User manager.</param>
|
||||
/// <param name="libraryManager">.</param>
|
||||
public StaleMediaTask(ILogger<StaleMediaTask> logger, IUserManager userManager, ILibraryManager libraryManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_userManager = userManager;
|
||||
_libraryManager = libraryManager;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
var query = new InternalItemsQuery
|
||||
{
|
||||
IncludeItemTypes = [BaseItemKind.Movie, BaseItemKind.Series],
|
||||
Recursive = true
|
||||
};
|
||||
List<BaseItem> allItems = [.. _libraryManager.GetItemsResult(query).Items];
|
||||
|
||||
_logger.LogInformation("Total items found: {AllItems}", allItems);
|
||||
|
||||
List<BaseItem> series = [.. allItems.Where(item => item.GetBaseItemKind() == BaseItemKind.Series)];
|
||||
List<BaseItem> movies = [.. allItems.Where(item => item.GetBaseItemKind() == BaseItemKind.Movie && item.UserData.Count > 0)];
|
||||
|
||||
List<BaseItem> staleEpisodes = [.. series.SelectMany(GetStaleEpisodes)];
|
||||
List<BaseItem> staleMovies = [.. GetStaleMovies(movies)];
|
||||
|
||||
_logger.LogInformation("Stale Movies found: {StaleMovies}", staleMovies.Count);
|
||||
if (staleMovies.Count > 0)
|
||||
{
|
||||
_logger.LogInformation("Movies: {Names}", string.Join(", ", staleMovies.Select(movie => movie.Name)));
|
||||
}
|
||||
|
||||
_logger.LogInformation("Stale Episodes found: {StaleEpisodes}", staleEpisodes.Count);
|
||||
if (staleEpisodes.Count > 0)
|
||||
{
|
||||
// Firstly figure out the seasons, and then the Series to find the name.
|
||||
List<string> seriesNames = FindDistinctSeriesNamesFromEpisodes(staleEpisodes);
|
||||
_logger.LogInformation("Series: {Names}", string.Join(", ", seriesNames));
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private List<BaseItem> GetStaleMovies(List<BaseItem> movies)
|
||||
{
|
||||
List<BaseItem> staleMovies = [];
|
||||
foreach (var movie in movies)
|
||||
{
|
||||
var mostRecentUserData = movie.UserData.OrderByDescending(data => data.LastPlayedDate).First();
|
||||
if (mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff))
|
||||
{
|
||||
staleMovies.Add(movie);
|
||||
}
|
||||
}
|
||||
|
||||
return staleMovies;
|
||||
}
|
||||
|
||||
private List<string> FindDistinctSeriesNamesFromEpisodes(List<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
|
||||
});
|
||||
|
||||
List<string> seriesNames = [.. series.Select(series => series.Name).Distinct()];
|
||||
|
||||
return seriesNames;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
var mostRecentUserData = episode.UserData.OrderByDescending(data => data.LastPlayedDate).First();
|
||||
if (mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff))
|
||||
{
|
||||
staleEpisodes.AddRange(episodes);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return staleEpisodes;
|
||||
}
|
||||
|
||||
IEnumerable<TaskTriggerInfo> IScheduledTask.GetDefaultTriggers()
|
||||
{
|
||||
// Run this task every 24 hours
|
||||
yield return new TaskTriggerInfo
|
||||
{
|
||||
Type = TaskTriggerInfoType.IntervalTrigger,
|
||||
IntervalTicks = TimeSpan.FromHours(24).Ticks
|
||||
};
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
The idea behind this plugin is to have an easy way to run a task to find all movies and shows in your media collection that users haven't viewed in a number of cutoff days.
|
||||
|
||||
At the time of writing, the plugin is only capable of logging movies and shows that are stale (Unwatched for 90 days) by running a scheduled task. You will need to view your logs to know the number of stale files.
|
||||
At the time of writing, the plugin is only capable of logging movies and shows that are stale (Unwatched for a user set number of days) by running a scheduled task. You will need to view your logs to know the number of stale files and the names of said files.
|
||||
|
||||
Planned features:
|
||||
- Better logging to show more than just the count.
|
||||
- Better logging to show more than just the count. ✅
|
||||
- A page that shows what media is currently flagged for removal. And a button to confirm removal.
|
||||
- Integration with sonarr and radarr apis to delete your media.
|
||||
- Whitelist for shows to ignore. (Seasonal shows)
|
||||
|
||||
Reference in New Issue
Block a user