Intermediate commit

This commit is contained in:
pcjones
2024-02-12 01:57:41 +01:00
parent abc2c84aa2
commit 27ba8a1f19
20 changed files with 1027 additions and 365 deletions

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Text;
using System.Xml.Linq;
using UmlautAdaptarr.Models;
@@ -9,15 +10,12 @@ namespace UmlautAdaptarr.Controllers
{
public abstract class SearchControllerBase(ProxyService proxyService, TitleMatchingService titleMatchingService) : ControllerBase
{
protected readonly ProxyService _proxyService = proxyService;
protected readonly TitleMatchingService _titleMatchingService = titleMatchingService;
private readonly bool TODO_FORCE_TEXT_SEARCH_ORIGINAL_TITLE = false;
private readonly bool TODO_FORCE_TEXT_SEARCH_GERMAN_TITLE = false;
protected async Task<IActionResult> BaseSearch(string options,
string domain,
IDictionary<string, string> queryParameters,
string? germanTitle = null,
string? expectedTitle = null,
bool hasGermanUmlaut = false)
SearchItem? searchItem = null)
{
try
{
@@ -26,37 +24,66 @@ namespace UmlautAdaptarr.Controllers
return NotFound($"{domain} is not a valid URL.");
}
// Generate title variations for renaming process
var germanTitleVariations = !string.IsNullOrEmpty(germanTitle) ? _titleMatchingService.GenerateTitleVariations(germanTitle) : new List<string>();
// Check if "q" parameter exists for multiple search request handling
if (hasGermanUmlaut && !string.IsNullOrEmpty(germanTitle) && !string.IsNullOrEmpty(expectedTitle) && queryParameters.ContainsKey("q"))
var initialSearchResult = await PerformSingleSearchRequest(domain, queryParameters) as ContentResult;
if (initialSearchResult == null)
{
// Add original search query to title variations
var q = queryParameters["q"];
if (!germanTitleVariations.Contains(q))
return null;
}
string inititalProcessedContent = string.Empty;
// Rename titles in the single search content
if (!string.IsNullOrEmpty(initialSearchResult?.Content))
{
inititalProcessedContent = ProcessContent(initialSearchResult.Content, searchItem?.TitleMatchVariations, searchItem?.ExpectedTitle);
}
var additionalTextSearch = searchItem != null
&& !string.IsNullOrEmpty(searchItem.ExpectedTitle)
&& (TODO_FORCE_TEXT_SEARCH_GERMAN_TITLE || TODO_FORCE_TEXT_SEARCH_ORIGINAL_TITLE ||
// TODO check if this is a good idea
(searchItem.TitleSearchVariations.Length > 0 && !(searchItem.TitleSearchVariations.Length == 1 && searchItem.TitleSearchVariations[0] == searchItem.ExpectedTitle)));
if (additionalTextSearch)
{
// Aggregate the initial search result with additional results
// Remove identifiers for subsequent searches
// TODO rework this
queryParameters.Remove("tvdbid");
queryParameters.Remove("tvmazeid");
queryParameters.Remove("imdbid");
var titleSearchVariations = new List<string>(searchItem?.TitleSearchVariations);
string searchQuery = string.Empty;
if (queryParameters.TryGetValue("q", out string? q))
{
germanTitleVariations.Add(queryParameters["q"]!);
searchQuery = q ?? string.Empty;
// Add original search query to title variations
if (!titleSearchVariations.Remove(searchQuery))
{
titleSearchVariations.Add(searchQuery);
}
}
var expectedTitle = searchItem.ExpectedTitle;
if (TODO_FORCE_TEXT_SEARCH_ORIGINAL_TITLE)
{
if (expectedTitle != searchQuery && !titleSearchVariations.Contains(expectedTitle))
{
titleSearchVariations.Add(expectedTitle);
}
}
// Handle multiple search requests based on German title variations
var aggregatedResult = await AggregateSearchResults(domain, queryParameters, germanTitleVariations, expectedTitle);
// Rename titles in the aggregated content
var processedContent = ProcessContent(aggregatedResult.Content, germanTitleVariations, expectedTitle);
return Content(processedContent, aggregatedResult.ContentType, aggregatedResult.ContentEncoding);
}
else
{
var singleSearchResult = await PerformSingleSearchRequest(domain, queryParameters);
// Rename titles in the single search content
var contentResult = singleSearchResult as ContentResult;
if (contentResult != null)
{
var processedContent = ProcessContent(contentResult.Content ?? "", germanTitleVariations, expectedTitle);
return Content(processedContent, contentResult.ContentType!, Encoding.UTF8);
}
return singleSearchResult;
var aggregatedResult = await AggregateSearchResults(domain, queryParameters, titleSearchVariations, searchItem.TitleMatchVariations, expectedTitle);
aggregatedResult.AggregateItems(inititalProcessedContent);
return Content(aggregatedResult.Content, aggregatedResult.ContentType, aggregatedResult.ContentEncoding);
}
initialSearchResult!.Content = inititalProcessedContent;
return initialSearchResult;
}
catch (Exception ex)
{
@@ -70,7 +97,7 @@ namespace UmlautAdaptarr.Controllers
private async Task<IActionResult> PerformSingleSearchRequest(string domain, IDictionary<string, string> queryParameters)
{
var requestUrl = UrlUtilities.BuildUrl(domain, queryParameters);
var responseMessage = await _proxyService.ProxyRequestAsync(HttpContext, requestUrl);
var responseMessage = await proxyService.ProxyRequestAsync(HttpContext, requestUrl);
var content = await responseMessage.Content.ReadAsStringAsync();
var encoding = responseMessage.Content.Headers.ContentType?.CharSet != null ?
@@ -82,18 +109,17 @@ namespace UmlautAdaptarr.Controllers
}
private string ProcessContent(string content, List<string> germanTitleVariations, string? expectedTitle)
private string ProcessContent(string content, string[]? titleMatchVariations = null, string? expectedTitle = null)
{
// Check if German title and expected title are provided for renaming
if (!string.IsNullOrEmpty(expectedTitle) && germanTitleVariations.Count != 0)
{
// Process and rename titles in the content
content = _titleMatchingService.RenameTitlesInContent(content, germanTitleVariations, expectedTitle);
}
return content;
return titleMatchingService.RenameTitlesInContent(content, titleMatchVariations, expectedTitle);
}
public async Task<AggregatedSearchResult> AggregateSearchResults(string domain, IDictionary<string, string> queryParameters, List<string> germanTitleVariations, string expectedTitle)
public async Task<AggregatedSearchResult> AggregateSearchResults(
string domain,
IDictionary<string, string> queryParameters,
IEnumerable<string> titleSearchVariations,
string[] titleMatchVariations,
string expectedTitle)
{
string defaultContentType = "application/xml";
Encoding defaultEncoding = Encoding.UTF8;
@@ -101,23 +127,23 @@ namespace UmlautAdaptarr.Controllers
var aggregatedResult = new AggregatedSearchResult(defaultContentType, defaultEncoding);
foreach (var titleVariation in germanTitleVariations.Distinct())
foreach (var titleVariation in titleSearchVariations)
{
queryParameters["q"] = titleVariation; // Replace the "q" parameter for each variation
var requestUrl = UrlUtilities.BuildUrl(domain, queryParameters);
var responseMessage = await _proxyService.ProxyRequestAsync(HttpContext, requestUrl);
var responseMessage = await proxyService.ProxyRequestAsync(HttpContext, requestUrl);
var content = await responseMessage.Content.ReadAsStringAsync();
// Only update encoding from the first response
if (!encodingSet && responseMessage.Content.Headers.ContentType?.CharSet != null)
{
aggregatedResult.ContentEncoding = Encoding.GetEncoding(responseMessage.Content.Headers.ContentType.CharSet); ;
aggregatedResult.ContentEncoding = Encoding.GetEncoding(responseMessage.Content.Headers.ContentType.CharSet);
aggregatedResult.ContentType = responseMessage.Content.Headers.ContentType?.MediaType ?? defaultContentType;
encodingSet = true;
}
// Process and rename titles in the content
content = _titleMatchingService.RenameTitlesInContent(content, germanTitleVariations, expectedTitle);
content = ProcessContent(content, titleMatchVariations, expectedTitle);
// Aggregate the items into a single document
aggregatedResult.AggregateItems(content);
@@ -129,7 +155,7 @@ namespace UmlautAdaptarr.Controllers
public class SearchController(ProxyService proxyService,
TitleMatchingService titleMatchingService,
TitleQueryService titleQueryService) : SearchControllerBase(proxyService, titleMatchingService)
SearchItemLookupService searchItemLookupService) : SearchControllerBase(proxyService, titleMatchingService)
{
[HttpGet]
public async Task<IActionResult> MovieSearch([FromRoute] string options, [FromRoute] string domain)
@@ -165,54 +191,19 @@ namespace UmlautAdaptarr.Controllers
q => q.Key,
q => string.Join(",", q.Value));
string? searchKey = null;
string? searchValue = null;
SearchItem? searchItem = null;
string mediaType = "tv";
if (queryParameters.TryGetValue("tvdbid", out string? tvdbId))
if (queryParameters.TryGetValue("tvdbid", out string? tvdbId) && !string.IsNullOrEmpty(tvdbId))
{
searchKey = "tvdbid";
searchValue = tvdbId;
searchItem = await searchItemLookupService.GetOrFetchSearchItemByExternalId(mediaType, tvdbId);
}
else if (queryParameters.TryGetValue("q", out string? title))
else if (queryParameters.TryGetValue("q", out string? title) && !string.IsNullOrEmpty(title))
{
searchKey = "q";
searchValue = title;
searchItem = await searchItemLookupService.GetOrFetchSearchItemByTitle(mediaType, title);
}
// Perform the search if a valid search key was identified
if (searchKey != null && searchValue != null)
{
var (hasGermanUmlaut, germanTitle, expectedTitle) = searchKey == "tvdbid"
? await titleQueryService.QueryGermanShowTitleByTVDBId(searchValue)
: await titleQueryService.QueryGermanShowTitleByTitle(searchValue);
if (!string.IsNullOrEmpty(germanTitle) && !string.IsNullOrEmpty(expectedTitle))
{
var initialSearchResult = await BaseSearch(options, domain, queryParameters, germanTitle, expectedTitle, hasGermanUmlaut);
// Additional search with german title because the automatic tvdbid association often fails at the indexer too if there are umlauts
if (hasGermanUmlaut && searchKey == "tvdbid")
{
// Remove identifiers for subsequent searches
queryParameters.Remove("tvdbid");
queryParameters.Remove("tvmazeid");
queryParameters.Remove("imdbid");
// Aggregate the initial search result with additional results
var germanTitleVariations = _titleMatchingService.GenerateTitleVariations(germanTitle);
var aggregatedResult = await AggregateSearchResults(domain, queryParameters, germanTitleVariations, expectedTitle);
// todo processedContent wie in BaseSearch
aggregatedResult.AggregateItems((initialSearchResult as ContentResult)?.Content ?? "");
return Content(aggregatedResult.Content, aggregatedResult.ContentType, aggregatedResult.ContentEncoding);
}
return initialSearchResult;
}
}
return await BaseSearch(options, domain, queryParameters);
return await BaseSearch(options, domain, queryParameters, searchItem);
}
[HttpGet]