Skip to content

Commit

Permalink
#185 Refactor search functionality in TMDBService
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DineshSolanki committed Feb 11, 2024
1 parent 5196171 commit feffb3d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 89 deletions.
209 changes: 121 additions & 88 deletions FoliCon/Modules/TMDB/TMDBService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,120 +70,154 @@ public Task<ImagesWithId> SearchCollectionImages(int collectionId)
public async Task<ResultResponse> 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<ResultResponse> 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<object> 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<object> 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<object> 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<object> 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<SearchMovie> GetMovieSearchContainer(FindContainer findContainer)
Expand Down Expand Up @@ -224,7 +258,6 @@ private static SearchContainer<SearchTv> GetTvSearchContainer(FindContainer find

private static SearchContainer<SearchTv> GetTvSearchContainer(TvShow tvShow)
{
var rd = tvShow;
return new SearchContainer<SearchTv>
{
TotalResults = 1,
Expand Down
5 changes: 4 additions & 1 deletion FoliCon/Modules/utils/TitleCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit feffb3d

Please sign in to comment.