278 lines
9.9 KiB
C#
278 lines
9.9 KiB
C#
using Jellyfin.Plugin.MediaCleaner.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Net.Http;
|
|
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 System.Security.Cryptography.X509Certificates;
|
|
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
|
|
{
|
|
private static Configuration Configuration =>
|
|
Plugin.Instance!.Configuration;
|
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public SonarrController(HttpClient httpClient)
|
|
{
|
|
_httpClient = httpClient;
|
|
|
|
// Set the default request headers
|
|
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", Configuration.SonarrAPIKey);
|
|
}
|
|
|
|
private async Task<ObjectResult> GetSonarrSeriesInfo(SeriesInfo seriesInfo){
|
|
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
|
_httpClient,
|
|
Configuration.SonarrAddress,
|
|
HttpMethod.Get,
|
|
$"/api/v3/series?tvdbId={Uri.EscapeDataString(seriesInfo.TvdbId ?? string.Empty)}"
|
|
).ConfigureAwait(false);
|
|
|
|
var seriesResponseObj = JsonSerializer.Deserialize<List<SonarrSeries>>(responseBody.GetRawText());
|
|
var series = seriesResponseObj?.FirstOrDefault();
|
|
|
|
if (series == null)
|
|
{
|
|
return NotFound("Series not found in Sonarr library.");
|
|
}
|
|
|
|
return Ok(series);
|
|
}
|
|
|
|
private async Task<ObjectResult> GetSonarrEpisodeInfo(SonarrSeries sonarrSeries){
|
|
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
|
_httpClient,
|
|
Configuration.SonarrAddress,
|
|
HttpMethod.Get,
|
|
$"/api/v3/episode?seriesId={sonarrSeries.Id?.ToString(CultureInfo.InvariantCulture)}"
|
|
).ConfigureAwait(false);
|
|
|
|
var episodesResponseObj = JsonSerializer.Deserialize<List<EpisodeDeletionDetails>>(responseBody.GetRawText());
|
|
|
|
if(episodesResponseObj == null){
|
|
return NotFound("No episodes in response object.");
|
|
}
|
|
|
|
var seasonNumbers = new HashSet<int>(sonarrSeries.Seasons
|
|
.Where(s => s.SeasonNumber.HasValue)
|
|
.Select(s => s.SeasonNumber!.Value));
|
|
|
|
var staleEpisodesResponseObj = episodesResponseObj
|
|
.Where(episodeDeletionDetail => episodeDeletionDetail.SeasonNumber != null &&
|
|
seasonNumbers.Contains(episodeDeletionDetail.SeasonNumber.Value))
|
|
.ToList();
|
|
|
|
var episodeIds = staleEpisodesResponseObj
|
|
.Select(episodeDeletionDetail => episodeDeletionDetail.EpisodeId)
|
|
.Where(id => id.HasValue)
|
|
.Select(id => id!.Value)
|
|
.ToList();
|
|
|
|
var episodeFileIds = staleEpisodesResponseObj
|
|
.Select(episodeDeletionDetail => episodeDeletionDetail.EpisodeFileId)
|
|
.Where(id => id.HasValue)
|
|
.Select(id => id!.Value)
|
|
.ToList();
|
|
|
|
return Ok(new EpisodeIdLists(episodeIds, episodeFileIds));
|
|
}
|
|
|
|
[HttpPost("deleteSeriesFromSonarr")]
|
|
public async Task<IActionResult> DeleteSeriesFromRadarr([FromBody] SeriesInfo seriesInfo){
|
|
|
|
if (seriesInfo == null || string.IsNullOrEmpty(seriesInfo.TvdbId))
|
|
{
|
|
return BadRequest("Invalid series information provided.");
|
|
}
|
|
|
|
try
|
|
{
|
|
var sonarrSeriesInfoResult = await GetSonarrSeriesInfo(seriesInfo).ConfigureAwait(false);
|
|
|
|
if(sonarrSeriesInfoResult.StatusCode != StatusCodes.Status200OK || sonarrSeriesInfoResult.Value is not SonarrSeries){
|
|
return sonarrSeriesInfoResult;
|
|
}
|
|
|
|
SonarrSeries retrievedSeries = (SonarrSeries)sonarrSeriesInfoResult.Value;
|
|
SonarrSeries staleSeries = new(
|
|
Id: retrievedSeries.Id,
|
|
Title: retrievedSeries.Title,
|
|
Seasons: [.. seriesInfo.Seasons.Select(season => new Season(SeasonNumber: int.Parse(season, CultureInfo.InvariantCulture)))]
|
|
);
|
|
|
|
var episodesToPurgeResult = await GetSonarrEpisodeInfo(staleSeries).ConfigureAwait(false);
|
|
if (episodesToPurgeResult.StatusCode != StatusCodes.Status200OK || episodesToPurgeResult.Value is not EpisodeIdLists)
|
|
{
|
|
return sonarrSeriesInfoResult;
|
|
}
|
|
|
|
EpisodeIdLists episodesToPurge = (EpisodeIdLists)episodesToPurgeResult.Value;
|
|
|
|
await UnmonitorSeasons(staleSeries).ConfigureAwait(false);
|
|
await UnmonitorEpisodeIds(episodesToPurge.EpisodeIds).ConfigureAwait(false);
|
|
await DeleteEpisodeFiles(episodesToPurge.EpisodeFileIds).ConfigureAwait(false);
|
|
|
|
return Ok();
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, $"An unexpected error occurred. {e.Message}");
|
|
}
|
|
}
|
|
|
|
private async Task<ObjectResult> UnmonitorSeasons(SonarrSeries staleSeries){
|
|
if (staleSeries == null)
|
|
{
|
|
return BadRequest("No stale series provided.");
|
|
}
|
|
|
|
var series = await HttpHelper.SendHttpRequestAsync(
|
|
_httpClient,
|
|
Configuration.SonarrAddress,
|
|
HttpMethod.Get,
|
|
$"/api/v3/series/{staleSeries.Id}"
|
|
).ConfigureAwait(false);
|
|
|
|
var seriesDict = JsonSerializer.Deserialize<Dictionary<string, object>>(series.GetRawText());
|
|
if (seriesDict == null)
|
|
{
|
|
throw new InvalidOperationException("Failed to deserialize season.");
|
|
}
|
|
var seasons = series.GetProperty("seasons").EnumerateArray().ToList();
|
|
|
|
var staleSeasonNumbers = staleSeries.Seasons
|
|
.Select(s => s.SeasonNumber)
|
|
.ToHashSet();
|
|
|
|
var updatedSeasons = seasons.Select(season =>
|
|
{
|
|
var seasonNumber = season.GetProperty("seasonNumber").GetInt32();
|
|
|
|
if (staleSeasonNumbers.Contains(seasonNumber))
|
|
{
|
|
var seasonDict = JsonSerializer.Deserialize<Dictionary<string, object>>(season.GetRawText());
|
|
if (seasonDict == null)
|
|
{
|
|
throw new InvalidOperationException("Failed to deserialize season.");
|
|
}
|
|
seasonDict["monitored"] = false;
|
|
return seasonDict;
|
|
}
|
|
|
|
return JsonSerializer.Deserialize<Dictionary<string, object>>(season.GetRawText());
|
|
}).ToArray();
|
|
|
|
seriesDict["seasons"] = updatedSeasons;
|
|
|
|
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
|
_httpClient,
|
|
Configuration.SonarrAddress,
|
|
HttpMethod.Put,
|
|
$"/api/v3/series/{staleSeries.Id}",
|
|
seriesDict
|
|
).ConfigureAwait(false);
|
|
|
|
return Ok(responseBody);
|
|
}
|
|
|
|
private async Task<ObjectResult> DeleteEpisodeFiles(IReadOnlyList<int> episodeFileIds)
|
|
{
|
|
if (episodeFileIds == null || episodeFileIds.Count == 0)
|
|
{
|
|
return BadRequest("No episode file IDs provided.");
|
|
}
|
|
|
|
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
|
_httpClient,
|
|
Configuration.SonarrAddress,
|
|
HttpMethod.Delete,
|
|
"/api/v3/episodefile/bulk",
|
|
new { episodeFileIds }
|
|
).ConfigureAwait(false);
|
|
|
|
return Ok(responseBody);
|
|
}
|
|
|
|
private async Task<ObjectResult> UnmonitorEpisodeIds(IReadOnlyList<int> episodeIds)
|
|
{
|
|
if (episodeIds == null || episodeIds.Count == 0)
|
|
{
|
|
return BadRequest("No episode IDs provided.");
|
|
}
|
|
|
|
var responseBody = await HttpHelper.SendHttpRequestAsync(
|
|
_httpClient,
|
|
Configuration.SonarrAddress,
|
|
HttpMethod.Put,
|
|
"/api/v3/episode/monitor",
|
|
new { episodeIds, monitored = false }
|
|
).ConfigureAwait(false);
|
|
|
|
return Ok(responseBody);
|
|
}
|
|
|
|
[HttpPost("testConnection")]
|
|
public async Task<IActionResult> TestConnection([FromBody] ConnectionTestRequest request)
|
|
{
|
|
if (request == null || string.IsNullOrWhiteSpace(request.Address) || string.IsNullOrWhiteSpace(request.ApiKey))
|
|
{
|
|
return BadRequest("Address and ApiKey are required.");
|
|
}
|
|
|
|
var address = request.Address.Trim();
|
|
if (!address.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
|
|
!address.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
address = "http://" + address;
|
|
}
|
|
|
|
try
|
|
{
|
|
using var httpRequest = new HttpRequestMessage(HttpMethod.Get, address);
|
|
httpRequest.Headers.Add("X-Api-Key", request.ApiKey);
|
|
|
|
var response = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false);
|
|
return Ok(new { success = response.IsSuccessStatusCode });
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
return StatusCode(StatusCodes.Status502BadGateway, e.Message);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
|
|
}
|
|
}
|
|
}
|