From feffb3d1f81020cccbb1f3455299f70dd91b8a36 Mon Sep 17 00:00:00 2001 From: Dinesh Solanki <15937452+dineshsolanki@users.noreply.github.com> Date: Mon, 12 Feb 2024 00:36:14 +0530 Subject: [PATCH] #185 Refactor search functionality in TMDBService The search functionality in TMDBService has been refactored to improve readability and maintainability. This refactor separates the search process into separate methods based on the media type - movie, collection, tv show, or multi. --- FoliCon/Modules/TMDB/TMDBService.cs | 209 +++++++++++++++----------- FoliCon/Modules/utils/TitleCleaner.cs | 5 +- 2 files changed, 125 insertions(+), 89 deletions(-) diff --git a/FoliCon/Modules/TMDB/TMDBService.cs b/FoliCon/Modules/TMDB/TMDBService.cs index e06e6fc..053b081 100644 --- a/FoliCon/Modules/TMDB/TMDBService.cs +++ b/FoliCon/Modules/TMDB/TMDBService.cs @@ -70,120 +70,154 @@ public Task SearchCollectionImages(int collectionId) public async Task SearchAsync(string query, string searchMode) { Logger.Info("Searching for {Query} in {SearchMode}", query, searchMode); - object r = null; - var mediaType = ""; - if (searchMode == MediaTypes.Movie) + var (r, mediaType) = searchMode switch { - if (query.ToLower(CultureInfo.InvariantCulture).Contains("collection")) - { - r = await _serviceClient.SearchCollectionAsync(query); - mediaType = MediaTypes.Collection; - } - else - { - r = await _serviceClient.SearchMovieAsync(query); - mediaType = MediaTypes.Movie; - } - } - else if (searchMode == MediaTypes.Tv) + "Movie" => await SearchMoviesAsync(query), + "TV" => await SearchTvShowAsync(query), + "Auto (Movies & TV Shows)" => await SearchMultiAsync(query), + _ => (null, "") + }; + return new ResultResponse { - r = await _serviceClient.SearchTvShowAsync(query); - mediaType = MediaTypes.Tv; + Result = r, + MediaType = mediaType + }; + } + + private async Task<(object Result, string MediaType)> SearchMoviesAsync(string query) + { + object r; + string mediaType; + if (query.ToLower(CultureInfo.InvariantCulture).Contains("collection")) + { + r = await _serviceClient.SearchCollectionAsync(query); + mediaType = MediaTypes.Collection; } - else if (searchMode == MediaTypes.Mtv) + else { - r = await _serviceClient.SearchMultiAsync(query); - mediaType = MediaTypes.Mtv; + r = await _serviceClient.SearchMovieAsync(query); + mediaType = MediaTypes.Movie; } + return (r, mediaType); + } - var response = new ResultResponse - { - Result = r, - MediaType = mediaType - }; - return response; + private async Task<(object Result, string MediaType)> SearchTvShowAsync(string query) + { + var r = await _serviceClient.SearchTvShowAsync(query); + var mediaType = MediaTypes.Tv; + return (r, mediaType); + } + + private async Task<(object Result, string MediaType)> SearchMultiAsync(string query) + { + var r = await _serviceClient.SearchMultiAsync(query); + var mediaType = MediaTypes.Mtv; + return (r, mediaType); } public async Task SearchWithParamsAsync(ParsedTitle parsedTitle, string searchMode) { Logger.Debug("Searching for {ParsedTitle} in {SearchMode}", parsedTitle, searchMode); - object r = null; + var mediaType = ""; - var query = parsedTitle.Title; + object? searchResult = null; + if (searchMode == MediaTypes.Movie) { - if (query.ToLower(CultureInfo.InvariantCulture).Contains("collection")) - { - r = parsedTitle.IdType switch - { - IdType.None => await _serviceClient.SearchCollectionAsync(query), - IdType.Tvdb => GetMovieSearchContainer( - await _serviceClient.FindAsync(FindExternalSource.TvDb, parsedTitle.Id)), - IdType.Tmdb => GetCollectionSearchContainer( - await _serviceClient.GetCollectionAsync(Convert.ToInt32(parsedTitle.Id))), - IdType.Imdb => GetMovieSearchContainer( - await _serviceClient.FindAsync(FindExternalSource.Imdb, parsedTitle.Id)), - _ => await _serviceClient.SearchCollectionAsync(query) - }; - - mediaType = MediaTypes.Collection; - } - else - { - r = parsedTitle.IdType switch - { - IdType.None => parsedTitle.Year != 0 - ? await _serviceClient.SearchMovieAsync(query: query, year: parsedTitle.Year) - : await _serviceClient.SearchMovieAsync(query), - IdType.Tvdb => GetMovieSearchContainer(await _serviceClient.FindAsync(FindExternalSource.TvDb, parsedTitle.Id)), - IdType.Tmdb => GetMovieSearchContainer(await _serviceClient.GetMovieAsync(Convert.ToInt32(parsedTitle.Id))), - IdType.Imdb => await _serviceClient.FindAsync(FindExternalSource.Imdb, parsedTitle.Id), - _ => parsedTitle.Year != 0 - ? await _serviceClient.SearchMovieAsync(query: query, year: parsedTitle.Year) - : await _serviceClient.SearchMovieAsync(query) - }; - - mediaType = MediaTypes.Movie; - } + searchResult = parsedTitle.Title.ToLower(CultureInfo.InvariantCulture).Contains("collection") + ? await SearchCollection(parsedTitle) + : await SearchMovie(parsedTitle); + mediaType = parsedTitle.Title.ToLower(CultureInfo.InvariantCulture).Contains("collection") + ? MediaTypes.Collection + : MediaTypes.Movie; } else if (searchMode == MediaTypes.Tv) { - r = parsedTitle.IdType switch - { - IdType.None => parsedTitle.Year != 0 - ? await _serviceClient.SearchTvShowAsync(query: query, firstAirDateYear: parsedTitle.Year) - : await _serviceClient.SearchTvShowAsync(query), - IdType.Tvdb => GetTvSearchContainer(await _serviceClient.FindAsync(FindExternalSource.TvDb, parsedTitle.Id)), - IdType.Tmdb => GetTvSearchContainer(await _serviceClient.GetTvShowAsync(Convert.ToInt32(parsedTitle.Id))), - IdType.Imdb => GetTvSearchContainer(await _serviceClient.FindAsync(FindExternalSource.Imdb, parsedTitle.Id)), - _ => parsedTitle.Year != 0 - ? await _serviceClient.SearchTvShowAsync(query: query, firstAirDateYear: parsedTitle.Year) - : await _serviceClient.SearchTvShowAsync(query) - }; + searchResult = await SearchTvShow(parsedTitle); mediaType = MediaTypes.Tv; } else if (searchMode == MediaTypes.Mtv) { - r = parsedTitle.IdType switch - { - IdType.None => parsedTitle.Year != 0 - ? await _serviceClient.SearchMultiAsync(query: query, year: parsedTitle.Year) - : await _serviceClient.SearchMultiAsync(query), - IdType.Tvdb => GetMultiSearchContainer(await _serviceClient.FindAsync(FindExternalSource.TvDb, parsedTitle.Id)), - IdType.Imdb => GetMultiSearchContainer(await _serviceClient.FindAsync(FindExternalSource.Imdb, parsedTitle.Id)), - _ => parsedTitle.Year != 0 - ? await _serviceClient.SearchMultiAsync(query: query, year: parsedTitle.Year) - : await _serviceClient.SearchMultiAsync(query) - }; + searchResult = await SearchMulti(parsedTitle); mediaType = MediaTypes.Mtv; } - var response = new ResultResponse + return new ResultResponse { - Result = r, + Result = searchResult, MediaType = mediaType }; - return response; + } + + private async Task SearchMovie(ParsedTitle parsedTitle) + { + var query = parsedTitle.Title; + return parsedTitle.IdType switch + { + IdType.None => parsedTitle.Year != 0 + ? await _serviceClient.SearchMovieAsync(query:query, year:parsedTitle.Year) + : await _serviceClient.SearchMovieAsync(query), + IdType.Tvdb => GetMovieSearchContainer(await _serviceClient.FindAsync(FindExternalSource.TvDb, + parsedTitle.Id)), + IdType.Tmdb => GetMovieSearchContainer(await _serviceClient.GetMovieAsync(Convert.ToInt32(parsedTitle.Id))), + IdType.Imdb => await _serviceClient.FindAsync(FindExternalSource.Imdb, parsedTitle.Id), + _ => parsedTitle.Year != 0 + ? await _serviceClient.SearchMovieAsync(query, parsedTitle.Year) + : await _serviceClient.SearchMovieAsync(query) + }; + } + + private async Task SearchCollection(ParsedTitle parsedTitle) + { + var query = parsedTitle.Title; + return parsedTitle.IdType switch + { + IdType.None => await _serviceClient.SearchCollectionAsync(query), + IdType.Tvdb => GetMovieSearchContainer(await _serviceClient.FindAsync(FindExternalSource.TvDb, + parsedTitle.Id)), + IdType.Tmdb => GetCollectionSearchContainer( + await _serviceClient.GetCollectionAsync(Convert.ToInt32(parsedTitle.Id))), + IdType.Imdb => GetMovieSearchContainer(await _serviceClient.FindAsync(FindExternalSource.Imdb, + parsedTitle.Id)), + _ => await _serviceClient.SearchCollectionAsync(query) + }; + } + + private async Task SearchTvShow(ParsedTitle parsedTitle) + { + var query = parsedTitle.Title; + return parsedTitle.IdType switch + { + IdType.None => parsedTitle.Year != 0 + ? await _serviceClient.SearchTvShowAsync(query:query, firstAirDateYear:parsedTitle.Year) + : await _serviceClient.SearchTvShowAsync(query), + IdType.Tvdb => + GetTvSearchContainer(await _serviceClient.FindAsync(FindExternalSource.TvDb, parsedTitle.Id)), + IdType.Tmdb => GetTvSearchContainer(await _serviceClient.GetTvShowAsync(Convert.ToInt32(parsedTitle.Id))), + IdType.Imdb => + GetTvSearchContainer(await _serviceClient.FindAsync(FindExternalSource.Imdb, parsedTitle.Id)), + _ => parsedTitle.Year != 0 + ? await _serviceClient.SearchTvShowAsync(query, parsedTitle.Year) + : await _serviceClient.SearchTvShowAsync(query) + }; + } + + private async Task SearchMulti(ParsedTitle parsedTitle) + { + var query = parsedTitle.Title; + return parsedTitle.IdType switch + { + IdType.None => parsedTitle.Year != 0 + ? await _serviceClient.SearchMultiAsync(query:query, year: parsedTitle.Year) + : await _serviceClient.SearchMultiAsync(query), + IdType.Tvdb => GetMultiSearchContainer(await _serviceClient.FindAsync(FindExternalSource.TvDb, + parsedTitle.Id)), + IdType.Imdb => GetMultiSearchContainer(await _serviceClient.FindAsync(FindExternalSource.Imdb, + parsedTitle.Id)), + _ => parsedTitle.Year != 0 + ? await _serviceClient.SearchMultiAsync(query:query, year:parsedTitle.Year) + : await _serviceClient.SearchMultiAsync(query) + }; } private static SearchContainer GetMovieSearchContainer(FindContainer findContainer) @@ -224,7 +258,6 @@ private static SearchContainer GetTvSearchContainer(FindContainer find private static SearchContainer GetTvSearchContainer(TvShow tvShow) { - var rd = tvShow; return new SearchContainer { TotalResults = 1, diff --git a/FoliCon/Modules/utils/TitleCleaner.cs b/FoliCon/Modules/utils/TitleCleaner.cs index 9f48265..4c60026 100644 --- a/FoliCon/Modules/utils/TitleCleaner.cs +++ b/FoliCon/Modules/utils/TitleCleaner.cs @@ -78,7 +78,10 @@ private static (string, IdType, string, int) ExtractShowIdAndYear(string title) title = Regex.Replace(title, showIdPattern, "", RegexOptions.IgnoreCase | RegexOptions.Compiled); } - if (!yearMatch.Success) return (title, showIdType, showId, 0); + if (!yearMatch.Success) + { + return (title, showIdType, showId, 0); + } var year = Convert.ToInt32(yearMatch.Groups[1].Value); title = Regex.Replace(title, yearPattern, "", RegexOptions.IgnoreCase | RegexOptions.Compiled);