diff --git a/UmlautAdaptarr/Program.cs b/UmlautAdaptarr/Program.cs index 55f3117..622a10e 100644 --- a/UmlautAdaptarr/Program.cs +++ b/UmlautAdaptarr/Program.cs @@ -1,7 +1,5 @@ -using Microsoft.Extensions.Configuration; using System.Net; using UmlautAdaptarr.Options; -using UmlautAdaptarr.Providers; using UmlautAdaptarr.Routing; using UmlautAdaptarr.Services; using UmlautAdaptarr.Utilities; diff --git a/UmlautAdaptarr/Providers/ArrClientFactory.cs b/UmlautAdaptarr/Providers/ArrClientFactory.cs new file mode 100644 index 0000000..55a8fec --- /dev/null +++ b/UmlautAdaptarr/Providers/ArrClientFactory.cs @@ -0,0 +1,15 @@ +namespace UmlautAdaptarr.Providers +{ + public static class ArrClientFactory + { + public static IEnumerable CreateClients( + Func constructor, IConfiguration configuration, string configKey) where TClient : ArrClientBase + { + var hosts = configuration.GetValue(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()); + } + } + } +} \ No newline at end of file diff --git a/UmlautAdaptarr/Providers/LidarrClient.cs b/UmlautAdaptarr/Providers/LidarrClient.cs index 51411be..cde2677 100644 --- a/UmlautAdaptarr/Providers/LidarrClient.cs +++ b/UmlautAdaptarr/Providers/LidarrClient.cs @@ -11,7 +11,6 @@ namespace UmlautAdaptarr.Providers { public class LidarrClient( IHttpClientFactory clientFactory, - IConfiguration configuration, CacheService cacheService, IMemoryCache cache, ILogger logger, IOptions options) : ArrClientBase() @@ -136,7 +135,7 @@ namespace UmlautAdaptarr.Providers { try { - // this should never be called at the moment + // this should never be called at the moment throw new NotImplementedException(); } catch (Exception ex) diff --git a/UmlautAdaptarr/Providers/ReadarrClient.cs b/UmlautAdaptarr/Providers/ReadarrClient.cs index 608ac2c..ce4a772 100644 --- a/UmlautAdaptarr/Providers/ReadarrClient.cs +++ b/UmlautAdaptarr/Providers/ReadarrClient.cs @@ -11,7 +11,6 @@ namespace UmlautAdaptarr.Providers { public class ReadarrClient( IHttpClientFactory clientFactory, - IConfiguration configuration, CacheService cacheService, IMemoryCache cache, IOptions options, diff --git a/UmlautAdaptarr/Providers/SonarrClient.cs b/UmlautAdaptarr/Providers/SonarrClient.cs index 82c218a..b9d9ef6 100644 --- a/UmlautAdaptarr/Providers/SonarrClient.cs +++ b/UmlautAdaptarr/Providers/SonarrClient.cs @@ -9,14 +9,11 @@ namespace UmlautAdaptarr.Providers { public class SonarrClient( IHttpClientFactory clientFactory, - IConfiguration configuration, TitleApiService titleService, IOptions options, ILogger logger) : ArrClientBase() { public SonarrInstanceOptions SonarrOptions { get; } = options.Value; - //private readonly string _sonarrHost = configuration.GetValue("SONARR_HOST") ?? throw new ArgumentException("SONARR_HOST environment variable must be set"); - //private readonly string _sonarrApiKey = configuration.GetValue("SONARR_API_KEY") ?? throw new ArgumentException("SONARR_API_KEY environment variable must be set"); private readonly string _mediaType = "tv"; public override async Task> FetchAllItemsAsync() diff --git a/UmlautAdaptarr/Services/ArrSyncBackgroundService.cs b/UmlautAdaptarr/Services/ArrSyncBackgroundService.cs index f34714a..00fedfb 100644 --- a/UmlautAdaptarr/Services/ArrSyncBackgroundService.cs +++ b/UmlautAdaptarr/Services/ArrSyncBackgroundService.cs @@ -16,7 +16,6 @@ namespace UmlautAdaptarr.Services LidarrClient lidarrClient, ReadarrClient readarrClient, CacheService cacheService, - IConfiguration configuration, ILogger logger) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) diff --git a/UmlautAdaptarr/Services/ProxyService.cs b/UmlautAdaptarr/Services/ProxyService.cs index 95e9df8..e398a91 100644 --- a/UmlautAdaptarr/Services/ProxyService.cs +++ b/UmlautAdaptarr/Services/ProxyService.cs @@ -23,7 +23,6 @@ namespace UmlautAdaptarr.Services _userAgent = _options.UserAgent ?? throw new ArgumentException("UserAgent must be set in appsettings.json"); _logger = logger; _cache = cache; - } 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}"); - // Create a response message indicating an internal server error var errorResponse = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError) { Content = new StringContent($"An error occurred while processing your request: {ex.Message}") diff --git a/UmlautAdaptarr/Services/SearchItemLookupService.cs b/UmlautAdaptarr/Services/SearchItemLookupService.cs index c70292c..eb68495 100644 --- a/UmlautAdaptarr/Services/SearchItemLookupService.cs +++ b/UmlautAdaptarr/Services/SearchItemLookupService.cs @@ -30,7 +30,7 @@ namespace UmlautAdaptarr.Services case "audio": if (lidarrClient.LidarrOptions.Enabled) { - fetchedItem = await lidarrClient.FetchItemByExternalIdAsync(externalId); + await lidarrClient.FetchItemByExternalIdAsync(externalId); fetchedItem = cacheService.GetSearchItemByExternalId(mediaType, externalId); } break; diff --git a/UmlautAdaptarr/Services/TitleMatchingService.cs b/UmlautAdaptarr/Services/TitleMatchingService.cs index 1c6c847..d229474 100644 --- a/UmlautAdaptarr/Services/TitleMatchingService.cs +++ b/UmlautAdaptarr/Services/TitleMatchingService.cs @@ -228,7 +228,7 @@ namespace UmlautAdaptarr.Services titleElement.Value = 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) { 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) diff --git a/UmlautAdaptarr/Services/TitleQueryServiceLegacy.cs b/UmlautAdaptarr/Services/TitleQueryServiceLegacy.cs deleted file mode 100644 index eba17d4..0000000 --- a/UmlautAdaptarr/Services/TitleQueryServiceLegacy.cs +++ /dev/null @@ -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 logger, - IConfiguration configuration, - IHttpClientFactory clientFactory, - SonarrClient sonarrClient) - { - private readonly HttpClient _httpClient = clientFactory.CreateClient("HttpClient") ?? throw new ArgumentNullException(); - private readonly string _sonarrHost = configuration.GetValue("SONARR_HOST") ?? throw new ArgumentException("SONARR_HOST environment variable must be set"); - private readonly string _sonarrApiKey = configuration.GetValue("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(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(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(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(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); - } - } - } -} diff --git a/UmlautAdaptarr/Utilities/ServicesExtensions.cs b/UmlautAdaptarr/Utilities/ServicesExtensions.cs index 1ac3957..8435828 100644 --- a/UmlautAdaptarr/Utilities/ServicesExtensions.cs +++ b/UmlautAdaptarr/Utilities/ServicesExtensions.cs @@ -1,6 +1,4 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using UmlautAdaptarr.Options; +using UmlautAdaptarr.Options; using UmlautAdaptarr.Options.ArrOptions; using UmlautAdaptarr.Providers; using UmlautAdaptarr.Services; @@ -91,6 +89,4 @@ namespace UmlautAdaptarr.Utilities return builder.AddServiceWithOptions("Settings"); } } - - }