Code cleanup
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using UmlautAdaptarr.Options;
|
using UmlautAdaptarr.Options;
|
||||||
using UmlautAdaptarr.Providers;
|
|
||||||
using UmlautAdaptarr.Routing;
|
using UmlautAdaptarr.Routing;
|
||||||
using UmlautAdaptarr.Services;
|
using UmlautAdaptarr.Services;
|
||||||
using UmlautAdaptarr.Utilities;
|
using UmlautAdaptarr.Utilities;
|
||||||
|
|||||||
15
UmlautAdaptarr/Providers/ArrClientFactory.cs
Normal file
15
UmlautAdaptarr/Providers/ArrClientFactory.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
namespace UmlautAdaptarr.Providers
|
||||||
|
{
|
||||||
|
public static class ArrClientFactory
|
||||||
|
{
|
||||||
|
public static IEnumerable<TClient> CreateClients<TClient>(
|
||||||
|
Func<string, TClient> constructor, IConfiguration configuration, string configKey) where TClient : ArrClientBase
|
||||||
|
{
|
||||||
|
var hosts = configuration.GetValue<string>(configKey)?.Split(',') ?? throw new ArgumentException($"{configKey} environment variable must be set if the app is enabled");
|
||||||
|
foreach (var host in hosts)
|
||||||
|
{
|
||||||
|
yield return constructor(host.Trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,6 @@ namespace UmlautAdaptarr.Providers
|
|||||||
{
|
{
|
||||||
public class LidarrClient(
|
public class LidarrClient(
|
||||||
IHttpClientFactory clientFactory,
|
IHttpClientFactory clientFactory,
|
||||||
IConfiguration configuration,
|
|
||||||
CacheService cacheService,
|
CacheService cacheService,
|
||||||
IMemoryCache cache,
|
IMemoryCache cache,
|
||||||
ILogger<LidarrClient> logger, IOptions<LidarrInstanceOptions> options) : ArrClientBase()
|
ILogger<LidarrClient> logger, IOptions<LidarrInstanceOptions> options) : ArrClientBase()
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ namespace UmlautAdaptarr.Providers
|
|||||||
{
|
{
|
||||||
public class ReadarrClient(
|
public class ReadarrClient(
|
||||||
IHttpClientFactory clientFactory,
|
IHttpClientFactory clientFactory,
|
||||||
IConfiguration configuration,
|
|
||||||
CacheService cacheService,
|
CacheService cacheService,
|
||||||
IMemoryCache cache,
|
IMemoryCache cache,
|
||||||
IOptions<ReadarrInstanceOptions> options,
|
IOptions<ReadarrInstanceOptions> options,
|
||||||
|
|||||||
@@ -9,14 +9,11 @@ namespace UmlautAdaptarr.Providers
|
|||||||
{
|
{
|
||||||
public class SonarrClient(
|
public class SonarrClient(
|
||||||
IHttpClientFactory clientFactory,
|
IHttpClientFactory clientFactory,
|
||||||
IConfiguration configuration,
|
|
||||||
TitleApiService titleService,
|
TitleApiService titleService,
|
||||||
IOptions<SonarrInstanceOptions> options,
|
IOptions<SonarrInstanceOptions> options,
|
||||||
ILogger<SonarrClient> logger) : ArrClientBase()
|
ILogger<SonarrClient> logger) : ArrClientBase()
|
||||||
{
|
{
|
||||||
public SonarrInstanceOptions SonarrOptions { get; } = options.Value;
|
public SonarrInstanceOptions SonarrOptions { get; } = options.Value;
|
||||||
//private readonly string _sonarrHost = configuration.GetValue<string>("SONARR_HOST") ?? throw new ArgumentException("SONARR_HOST environment variable must be set");
|
|
||||||
//private readonly string _sonarrApiKey = configuration.GetValue<string>("SONARR_API_KEY") ?? throw new ArgumentException("SONARR_API_KEY environment variable must be set");
|
|
||||||
private readonly string _mediaType = "tv";
|
private readonly string _mediaType = "tv";
|
||||||
|
|
||||||
public override async Task<IEnumerable<SearchItem>> FetchAllItemsAsync()
|
public override async Task<IEnumerable<SearchItem>> FetchAllItemsAsync()
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ namespace UmlautAdaptarr.Services
|
|||||||
LidarrClient lidarrClient,
|
LidarrClient lidarrClient,
|
||||||
ReadarrClient readarrClient,
|
ReadarrClient readarrClient,
|
||||||
CacheService cacheService,
|
CacheService cacheService,
|
||||||
IConfiguration configuration,
|
|
||||||
ILogger<ArrSyncBackgroundService> logger) : BackgroundService
|
ILogger<ArrSyncBackgroundService> logger) : BackgroundService
|
||||||
{
|
{
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ namespace UmlautAdaptarr.Services
|
|||||||
_userAgent = _options.UserAgent ?? throw new ArgumentException("UserAgent must be set in appsettings.json");
|
_userAgent = _options.UserAgent ?? throw new ArgumentException("UserAgent must be set in appsettings.json");
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task EnsureMinimumDelayAsync(string targetUri)
|
private static async Task EnsureMinimumDelayAsync(string targetUri)
|
||||||
@@ -91,7 +90,6 @@ namespace UmlautAdaptarr.Services
|
|||||||
{
|
{
|
||||||
_logger.LogError(ex, $"Error proxying request: {UrlUtilities.RedactApiKey(targetUri)}. Error: {ex.Message}");
|
_logger.LogError(ex, $"Error proxying request: {UrlUtilities.RedactApiKey(targetUri)}. Error: {ex.Message}");
|
||||||
|
|
||||||
// Create a response message indicating an internal server error
|
|
||||||
var errorResponse = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError)
|
var errorResponse = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError)
|
||||||
{
|
{
|
||||||
Content = new StringContent($"An error occurred while processing your request: {ex.Message}")
|
Content = new StringContent($"An error occurred while processing your request: {ex.Message}")
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ namespace UmlautAdaptarr.Services
|
|||||||
case "audio":
|
case "audio":
|
||||||
if (lidarrClient.LidarrOptions.Enabled)
|
if (lidarrClient.LidarrOptions.Enabled)
|
||||||
{
|
{
|
||||||
fetchedItem = await lidarrClient.FetchItemByExternalIdAsync(externalId);
|
await lidarrClient.FetchItemByExternalIdAsync(externalId);
|
||||||
fetchedItem = cacheService.GetSearchItemByExternalId(mediaType, externalId);
|
fetchedItem = cacheService.GetSearchItemByExternalId(mediaType, externalId);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ namespace UmlautAdaptarr.Services
|
|||||||
titleElement.Value = newTitle;
|
titleElement.Value = newTitle;
|
||||||
|
|
||||||
logger.LogInformation($"TitleMatchingService - Title changed: '{originalTitle}' to '{newTitle}'");
|
logger.LogInformation($"TitleMatchingService - Title changed: '{originalTitle}' to '{newTitle}'");
|
||||||
break; // Break after the first successful match and modification
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -242,7 +242,7 @@ namespace UmlautAdaptarr.Services
|
|||||||
private static char FindFirstSeparator(string title)
|
private static char FindFirstSeparator(string title)
|
||||||
{
|
{
|
||||||
var match = WordSeperationCharRegex().Match(title);
|
var match = WordSeperationCharRegex().Match(title);
|
||||||
return match.Success ? match.Value.First() : ' '; // Default to space if no separator found
|
return match.Success ? match.Value.First() : ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ReconstructTitleWithSeparator(string title, char separator)
|
private static string ReconstructTitleWithSeparator(string title, char separator)
|
||||||
|
|||||||
@@ -1,162 +0,0 @@
|
|||||||
using Microsoft.Extensions.Caching.Memory;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using UmlautAdaptarr.Models;
|
|
||||||
using UmlautAdaptarr.Providers;
|
|
||||||
using UmlautAdaptarr.Utilities;
|
|
||||||
|
|
||||||
namespace UmlautAdaptarr.Services
|
|
||||||
{
|
|
||||||
public class TitleQueryServiceLegacy(
|
|
||||||
IMemoryCache memoryCache,
|
|
||||||
ILogger<TitleQueryServiceLegacy> logger,
|
|
||||||
IConfiguration configuration,
|
|
||||||
IHttpClientFactory clientFactory,
|
|
||||||
SonarrClient sonarrClient)
|
|
||||||
{
|
|
||||||
private readonly HttpClient _httpClient = clientFactory.CreateClient("HttpClient") ?? throw new ArgumentNullException();
|
|
||||||
private readonly string _sonarrHost = configuration.GetValue<string>("SONARR_HOST") ?? throw new ArgumentException("SONARR_HOST environment variable must be set");
|
|
||||||
private readonly string _sonarrApiKey = configuration.GetValue<string>("SONARR_API_KEY") ?? throw new ArgumentException("SONARR_API_KEY environment variable must be set");
|
|
||||||
private readonly string _umlautAdaptarrApiHost = configuration["Settings:UmlautAdaptarrApiHost"] ?? throw new ArgumentException("UmlautAdaptarrApiHost must be set in appsettings.json");
|
|
||||||
|
|
||||||
/*public async Task<(bool hasGermanUmlaut, string? GermanTitle, string ExpectedTitle)> QueryGermanShowTitleByTVDBId(string tvdbId)
|
|
||||||
{
|
|
||||||
var sonarrCacheKey = $"SearchItem_Sonarr_{tvdbId}";
|
|
||||||
|
|
||||||
if (memoryCache.TryGetValue(sonarrCacheKey, out SearchItem? cachedItem))
|
|
||||||
{
|
|
||||||
return (cachedItem?.HasGermanUmlaut ?? false, cachedItem?.GermanTitle, cachedItem?.ExpectedTitle ?? string.Empty);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var sonarrUrl = $"{_sonarrHost}/api/v3/series?tvdbId={tvdbId}&includeSeasonImages=false&apikey={_sonarrApiKey}";
|
|
||||||
var sonarrApiResponse = await _httpClient.GetStringAsync(sonarrUrl);
|
|
||||||
var shows = JsonConvert.DeserializeObject<dynamic>(sonarrApiResponse);
|
|
||||||
|
|
||||||
if (shows == null)
|
|
||||||
{
|
|
||||||
logger.LogError($"Parsing Sonarr API response for TVDB ID {tvdbId} resulted in null");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
else if (shows.Count == 0)
|
|
||||||
{
|
|
||||||
logger.LogWarning($"No results found for TVDB ID {tvdbId}");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
var expectedTitle = (string)shows[0].title;
|
|
||||||
if (expectedTitle == null)
|
|
||||||
{
|
|
||||||
logger.LogError($"Sonarr Title for TVDB ID {tvdbId} is null");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
string? germanTitle = null;
|
|
||||||
var hasGermanTitle = false;
|
|
||||||
|
|
||||||
var titleApiUrl = $"{_umlautAdaptarrApiHost}/tvshow_german.php?tvdbid={tvdbId}";
|
|
||||||
var titleApiResponse = await _httpClient.GetStringAsync(titleApiUrl);
|
|
||||||
var titleApiResponseData = JsonConvert.DeserializeObject<dynamic>(titleApiResponse);
|
|
||||||
|
|
||||||
if (titleApiResponseData == null)
|
|
||||||
{
|
|
||||||
logger.LogError($"Parsing UmlautAdaptarr TitleQuery API response for TVDB ID {tvdbId} resulted in null");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (titleApiResponseData.status == "success" && !string.IsNullOrEmpty((string)titleApiResponseData.germanTitle))
|
|
||||||
{
|
|
||||||
germanTitle = titleApiResponseData.germanTitle;
|
|
||||||
hasGermanTitle = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
var hasGermanUmlaut = germanTitle?.HasGermanUmlauts() ?? false;
|
|
||||||
|
|
||||||
var result = (hasGermanUmlaut, germanTitle, expectedTitle);
|
|
||||||
memoryCache.Set(showCacheKey, result, new MemoryCacheEntryOptions
|
|
||||||
{
|
|
||||||
Size = 1,
|
|
||||||
SlidingExpiration = hasGermanTitle ? TimeSpan.FromDays(30) : TimeSpan.FromDays(7)
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// This method is being used if the *arrs do a search with the "q" parameter (text search)
|
|
||||||
public async Task<(bool hasGermanUmlaut, string? GermanTitle, string ExpectedTitle)> QueryGermanShowTitleByTitle(string title)
|
|
||||||
{
|
|
||||||
// TVDB doesn't use ß - TODO: Determine if this is true
|
|
||||||
var tvdbCleanTitle = title.Replace("ß", "ss");
|
|
||||||
|
|
||||||
var cacheKey = $"show_{tvdbCleanTitle}";
|
|
||||||
if (memoryCache.TryGetValue(cacheKey, out (bool hasGermanUmlaut, string? GermanTitle, string ExpectedTitle) cachedResult))
|
|
||||||
{
|
|
||||||
return cachedResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
var titleApiUrl = $"{_umlautAdaptarrApiHost}/tvshow_german.php?title={tvdbCleanTitle}";
|
|
||||||
var titleApiResponse = await _httpClient.GetStringAsync(titleApiUrl);
|
|
||||||
var titleApiResponseData = JsonConvert.DeserializeObject<dynamic>(titleApiResponse);
|
|
||||||
|
|
||||||
if (titleApiResponseData == null)
|
|
||||||
{
|
|
||||||
logger.LogError($"Parsing UmlautAdaptarr TitleQuery API response for title {title} resulted in null");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (titleApiResponseData.status == "success" && !string.IsNullOrEmpty((string)titleApiResponseData.germanTitle))
|
|
||||||
{
|
|
||||||
var tvdbId = (string)titleApiResponseData.tvdbId;
|
|
||||||
if (tvdbId == null)
|
|
||||||
{
|
|
||||||
logger.LogError($"Parsing UmlautAdaptarr TitleQuery API response tvdbId {titleApiResponseData} resulted in null");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
var sonarrUrl = $"{_sonarrHost}/api/v3/series?tvdbId={tvdbId}&includeSeasonImages=false&apikey={_sonarrApiKey}";
|
|
||||||
var sonarrApiResponse = await _httpClient.GetStringAsync(sonarrUrl);
|
|
||||||
var shows = JsonConvert.DeserializeObject<dynamic>(sonarrApiResponse);
|
|
||||||
|
|
||||||
if (shows == null)
|
|
||||||
{
|
|
||||||
logger.LogError($"Parsing Sonarr API response for TVDB ID {tvdbId} resulted in null");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
else if (shows.Count == 0)
|
|
||||||
{
|
|
||||||
logger.LogWarning($"No results found for TVDB ID {tvdbId}");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
var expectedTitle = (string)shows[0].title;
|
|
||||||
if (expectedTitle == null)
|
|
||||||
{
|
|
||||||
logger.LogError($"Sonarr Title for TVDB ID {tvdbId} is null");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
string germanTitle ;
|
|
||||||
bool hasGermanTitle;
|
|
||||||
|
|
||||||
germanTitle = titleApiResponseData.germanTitle;
|
|
||||||
hasGermanTitle = true;
|
|
||||||
|
|
||||||
var hasGermanUmlaut = germanTitle?.HasUmlauts() ?? false;
|
|
||||||
|
|
||||||
var result = (hasGermanUmlaut, germanTitle, expectedTitle);
|
|
||||||
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions
|
|
||||||
{
|
|
||||||
Size = 1,
|
|
||||||
SlidingExpiration = hasGermanTitle ? TimeSpan.FromDays(30) : TimeSpan.FromDays(7)
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
logger.LogWarning($"UmlautAdaptarr TitleQuery {titleApiUrl} didn't succeed.");
|
|
||||||
return (false, null, string.Empty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using UmlautAdaptarr.Options;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using UmlautAdaptarr.Options;
|
|
||||||
using UmlautAdaptarr.Options.ArrOptions;
|
using UmlautAdaptarr.Options.ArrOptions;
|
||||||
using UmlautAdaptarr.Providers;
|
using UmlautAdaptarr.Providers;
|
||||||
using UmlautAdaptarr.Services;
|
using UmlautAdaptarr.Services;
|
||||||
@@ -91,6 +89,4 @@ namespace UmlautAdaptarr.Utilities
|
|||||||
return builder.AddServiceWithOptions<GlobalOptions, ProxyService>("Settings");
|
return builder.AddServiceWithOptions<GlobalOptions, ProxyService>("Settings");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user