Refactored sonarr controller to be able to return anime or tv series based on the media found in the server. Also updated models

This commit is contained in:
2026-03-08 23:39:17 -06:00
parent a676a8e8ec
commit 87bf40dab9
14 changed files with 134 additions and 79 deletions

View File

@@ -13,13 +13,6 @@ using System.Linq;
namespace Jellyfin.Plugin.MediaCleaner.Controllers;
public record ConnectionTestRequest(string Address, string ApiKey);
public record RadarrMovie(
[property: JsonPropertyName("id")] int? Id,
[property: JsonPropertyName("title")] string? Title
);
[Route("radarr")]
public class RadarrController : Controller
{

View File

@@ -5,35 +5,15 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using System.Net.Http.Headers;
using System;
using System.Web;
using System.Text.Json;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Globalization;
using Jellyfin.Plugin.MediaCleaner.Enums;
using Jellyfin.Plugin.MediaCleaner.Helpers;
namespace Jellyfin.Plugin.MediaCleaner.Controllers;
public record SonarrSeries(
[property: JsonPropertyName("id")] int? Id,
[property: JsonPropertyName("title")] string? Title,
[property: JsonPropertyName("seasons")] IReadOnlyList<Season> Seasons
);
public record EpisodeDeletionDetails(
[property: JsonPropertyName("id")] int? EpisodeId,
[property: JsonPropertyName("episodeFileId")] int? EpisodeFileId,
[property: JsonPropertyName("seasonNumber")] int? SeasonNumber
);
public record EpisodeIdLists(IReadOnlyList<int> EpisodeIds, IReadOnlyList<int> EpisodeFileIds);
public record Season(
[property: JsonPropertyName("seasonNumber")] int? SeasonNumber
);
[Route("sonarr")]
public class SonarrController : Controller
{
@@ -69,7 +49,7 @@ public class SonarrController : Controller
HttpHelper httpHelper = new(ServerType.Sonarr);
var responseBody = await httpHelper.SendHttpRequestAsync(
HttpMethod.Get,
$"/api/v3/episode?seriesId={sonarrSeries.Id?.ToString(CultureInfo.InvariantCulture)}"
$"/api/v3/episode?seriesId={sonarrSeries.Id.ToString(CultureInfo.InvariantCulture)}"
).ConfigureAwait(false);
var episodesResponseObj = JsonSerializer.Deserialize<List<EpisodeDeletionDetails>>(responseBody.GetRawText());
@@ -122,7 +102,9 @@ public class SonarrController : Controller
SonarrSeries staleSeries = new(
Id: retrievedSeries.Id,
Title: retrievedSeries.Title,
Seasons: [.. seriesInfo.Seasons.Select(season => new Season(SeasonNumber: int.Parse(season, CultureInfo.InvariantCulture)))]
Seasons: [.. seriesInfo.Seasons.Select(season => new Season(SeasonNumber: int.Parse(season, CultureInfo.InvariantCulture)))],
Ended: retrievedSeries.Ended,
TvdbId: retrievedSeries.TvdbId
);
var episodesToPurgeResult = await GetSonarrEpisodeInfo(staleSeries).ConfigureAwait(false);

View File

@@ -2,6 +2,8 @@ using Jellyfin.Plugin.MediaCleaner.Data;
using Jellyfin.Plugin.MediaCleaner;
using Jellyfin.Plugin.MediaCleaner.Models;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Jellyfin.Plugin.MediaCleaner.Controllers;
@@ -12,8 +14,19 @@ public class StateController(MediaCleanerState state) : Controller
private static Configuration Configuration =>
Plugin.Instance!.Configuration;
[HttpGet("getSeriesInfo")]
public IActionResult GetSeriesInfo() => Ok(_state.GetSeriesInfo());
[HttpGet("getTvSeriesInfo")]
public async Task<IActionResult> GetTvSeriesInfo()
{
var tvSeriesInfo = await _state.GetTvSeriesInfo().ConfigureAwait(false);
return Ok(tvSeriesInfo);
}
[HttpGet("getAnimeSeriesInfo")]
public async Task<IActionResult> GetAnimeSeriesInfo()
{
var animeSeriesInfo = await _state.GetAnimeSeriesInfo().ConfigureAwait(false);
return Ok(animeSeriesInfo);
}
[HttpGet("getMovieInfo")]
public IActionResult GetMovieInfo() => Ok(_state.GetMovieInfo());

View File

@@ -1,15 +1,16 @@
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.Helpers;
using Jellyfin.Plugin.MediaCleaner.Models;
using Jellyfin.Plugin.MediaCleaner.ScheduledTasks;
using MediaBrowser.Controller.Library;
using Microsoft.Extensions.Logging;
using Jellyfin.Plugin.MediaCleaner.Enums;
using System.Net.Http;
using System;
using System.Text.Json;
using System.Globalization;
namespace Jellyfin.Plugin.MediaCleaner.Data;
@@ -24,15 +25,54 @@ public class MediaCleanerState(ILogger<StaleMediaScanner> logger, ILibraryManage
_mediaInfo = await _staleMediaScanner.ScanStaleMedia().ConfigureAwait(false);
}
public IEnumerable<SeriesInfo> GetSeriesInfo()
public async Task<IEnumerable<SeriesInfo>> GetTvSeriesInfo()
{
// Filter only TV
// Get all series on tv sonarr server
HttpHelper tvHttpHelper = new HttpHelper(ServerType.Sonarr);
var tvSeriesResponse = await tvHttpHelper.SendHttpRequestAsync(HttpMethod.Get,"/api/v3/series").ConfigureAwait(false);
var tvSeries = JsonSerializer.Deserialize<IEnumerable<SonarrSeries>>(tvSeriesResponse.GetRawText());
if(tvSeries == null)
{
return [];
}
lock (_lock)
{
return _mediaInfo.OfType<SeriesInfo>();
var allSeries = _mediaInfo.OfType<SeriesInfo>();
var tvSeriesInfo = allSeries
.Where(series => tvSeries.Any(tv => tv.TvdbId == int.Parse(series.TvdbId, CultureInfo.InvariantCulture)));
return [.. tvSeriesInfo];
}
}
public IEnumerable<MovieInfo> GetMovieInfo()
public async Task<IEnumerable<SeriesInfo>> GetAnimeSeriesInfo()
{
// Get all series on anime sonarr server
HttpHelper animeHttpHelper = new HttpHelper(ServerType.SonarrAnime);
var animeSeriesResponse = await animeHttpHelper.SendHttpRequestAsync(HttpMethod.Get,"/api/v3/series").ConfigureAwait(false);
var animeSeries = JsonSerializer.Deserialize<List<SonarrSeries>>(animeSeriesResponse.GetRawText());
if(animeSeries == null)
{
return Enumerable.Empty<SeriesInfo>();
}
lock (_lock)
{
var allSeries = _mediaInfo.OfType<SeriesInfo>();
var animeSeriesInfo = allSeries
.Where(series => animeSeries.Any(anime => anime.TvdbId == int.Parse(series.TvdbId, CultureInfo.InvariantCulture)));
return animeSeriesInfo;
}
}
public IEnumerable<MovieInfo> GetMovieInfo()
{
lock (_lock)
{

View File

@@ -0,0 +1,3 @@
namespace Jellyfin.Plugin.MediaCleaner.Models;
public record ConnectionTestRequest(string Address, string ApiKey);

View File

@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;
namespace Jellyfin.Plugin.MediaCleaner.Models;
public record EpisodeDeletionDetails(
[property: JsonPropertyName("id")] int? EpisodeId,
[property: JsonPropertyName("episodeFileId")] int? EpisodeFileId,
[property: JsonPropertyName("seasonNumber")] int? SeasonNumber
);

View File

@@ -0,0 +1,5 @@
using System.Collections.Generic;
namespace Jellyfin.Plugin.MediaCleaner.Models;
public record EpisodeIdLists(IReadOnlyList<int> EpisodeIds, IReadOnlyList<int> EpisodeFileIds);

View File

@@ -4,6 +4,6 @@ namespace Jellyfin.Plugin.MediaCleaner.Models;
public abstract class MediaInfo
{
public required string? TmdbId { get; set; }
public required string TmdbId { get; set; }
public required string Name { get; set; }
}

View File

@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;
namespace Jellyfin.Plugin.MediaCleaner.Models;
public record RadarrMovie(
[property: JsonPropertyName("id")] int? Id,
[property: JsonPropertyName("title")] string? Title
);

View File

@@ -12,5 +12,5 @@ public class SeriesInfo : MediaInfo
{
public Guid SeriesId { get; set; }
public IEnumerable<string> Seasons { get; set; } = [];
public required string? TvdbId { get; set; }
public required string TvdbId { get; set; }
}

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace Jellyfin.Plugin.MediaCleaner.Models;
public record SonarrSeries(
[property: JsonPropertyName("id")] int Id,
[property: JsonPropertyName("title")] string? Title,
[property: JsonPropertyName("seasons")] IReadOnlyList<Season> Seasons,
[property: JsonPropertyName("ended")] bool Ended,
[property: JsonPropertyName("tvdbId")] int TvdbId
// [property: JsonPropertyName("tmdbId")] int TmdbId,
// [property: JsonPropertyName("imdbId")] int ImdbId
);
public record Season(
[property: JsonPropertyName("seasonNumber")] int? SeasonNumber
);

View File

@@ -34,6 +34,7 @@
</table>
<button id="seriesDeleteButton" class="delete-button raised button-submit emby-button" style="visibility: hidden;">Delete</button>
<br>
<h3 id="animeSeriesTitle"></h3>
<table id="animeSeriesTable">
<thead>

View File

@@ -20,14 +20,24 @@ const refreshFrontEnd = async () => {
finishLoading();
}
const getMediaCleanerSeriesInfo = async () => {
const response = await fetch("/mediacleaner/state/getSeriesInfo");
const getMediaCleanerTvSeriesInfo = async () => {
const response = await fetch("/mediacleaner/state/getTvSeriesInfo");
if(!response.ok){
throw new Error(`Response status: ${response.status}`)
}
return response.json();
return await response.json();
};
const getMediaCleanerAnimeSeriesInfo = async () => {
const response = await fetch("/mediacleaner/state/getAnimeSeriesInfo");
if(!response.ok){
throw new Error(`Response status: ${response.status}`)
}
return await response.json();
};
const getMediaCleanerMovieInfo = async () => {
@@ -37,7 +47,7 @@ const getMediaCleanerMovieInfo = async () => {
throw new Error(`Response status: ${response.status}`)
}
return response.json();
return await response.json();
};
const updateMediaCleanerState = async () => {
@@ -83,7 +93,7 @@ const getMediaCleanerMoviesTitle = async () => {
const populateTables = async () => {
var moviesInfo = await getMediaCleanerMovieInfo();
var seriesInfo = await getMediaCleanerSeriesInfo();
var seriesInfo = await getMediaCleanerTvSeriesInfo();
var animeSeriesInfo = await getMediaCleanerAnimeSeriesInfo();
var seriesTable = document.getElementById("seriesTable");
@@ -160,7 +170,7 @@ const populateTables = async () => {
}
}
else{
var columnCount = animeSeriesTableBody.tHead.rows[0].cells.length;
var columnCount = animeSeriesTable.tHead.rows[0].cells.length;
var row = animeSeriesTableBody.insertRow(-1);
var cell1 = row.insertCell(0);
cell1.colSpan = columnCount;
@@ -226,7 +236,7 @@ const addClickHandlersToDeleteButtons = () => {
const deleteAnimeSeriesButtonElement = document.getElementById("animeSeriesDeleteButton");
deleteMoviesButtonElement.addEventListener("click", deleteFromRadarr);
deleteSeriesButtonElement.addEventListener("click", deleteFromSonarr);
deleteAnimeSeriesButtonElement.addEventListener("click", deleteFromSonarrAnime);
deleteAnimeSeriesButtonElement.addEventListener("click", deleteFromAnimeSonarr);
}
const getCheckedMedia = (table) => {

View File

@@ -103,7 +103,7 @@ public sealed class StaleMediaScanner
movie.ProviderIds.TryGetValue("Tmdb", out string? tmdbId);
return new MovieInfo
{
TmdbId = tmdbId,
TmdbId = tmdbId ?? string.Empty,
Name = movie.Name
};
});
@@ -189,34 +189,6 @@ public sealed class StaleMediaScanner
List<BaseItem> staleSeasons = [.. GetStaleSeasonsWithShortCircuitOnNonStaleSeason(seasons)];
// [ ..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);
@@ -236,11 +208,12 @@ public sealed class StaleMediaScanner
{
series.ProviderIds.TryGetValue("Tvdb", out string? tvdbId);
series.ProviderIds.TryGetValue("Tmdb", out string? tmdbId);
return new SeriesInfo
{
SeriesId = series.Id,
TmdbId = tmdbId,
TvdbId = tvdbId,
TmdbId = tmdbId ?? string.Empty,
TvdbId = tvdbId ?? string.Empty,
Name = series.Name,
Seasons = [.. seasons.Where(season => season.ParentId == series.Id).Select(season => season.Name.Replace("Season ", "", StringComparison.OrdinalIgnoreCase))]
};