Ejemplo n.º 1
0
        public async Task <IEnumerable <RemoteSearchResult> > GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
        {
            if (searchInfo.TryGetProviderId(MetadataProvider.Tmdb, out var tmdbId))
            {
                var series = await _tmdbClientManager
                             .GetSeriesAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), searchInfo.MetadataLanguage, searchInfo.MetadataLanguage, cancellationToken)
                             .ConfigureAwait(false);

                if (series != null)
                {
                    var remoteResult = MapTvShowToRemoteSearchResult(series);

                    return(new[] { remoteResult });
                }
            }

            if (searchInfo.TryGetProviderId(MetadataProvider.Imdb, out var imdbId))
            {
                var findResult = await _tmdbClientManager
                                 .FindByExternalIdAsync(imdbId, FindExternalSource.Imdb, searchInfo.MetadataLanguage, cancellationToken)
                                 .ConfigureAwait(false);

                var tvResults = findResult?.TvResults;
                if (tvResults != null)
                {
                    var imdbIdResults = new RemoteSearchResult[tvResults.Count];
                    for (var i = 0; i < tvResults.Count; i++)
                    {
                        var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]);
                        remoteResult.SetProviderId(MetadataProvider.Imdb, imdbId);
                        imdbIdResults[i] = remoteResult;
                    }

                    return(imdbIdResults);
                }
            }

            if (searchInfo.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId))
            {
                var findResult = await _tmdbClientManager
                                 .FindByExternalIdAsync(tvdbId, FindExternalSource.TvDb, searchInfo.MetadataLanguage, cancellationToken)
                                 .ConfigureAwait(false);

                var tvResults = findResult?.TvResults;
                if (tvResults != null)
                {
                    var tvIdResults = new RemoteSearchResult[tvResults.Count];
                    for (var i = 0; i < tvResults.Count; i++)
                    {
                        var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]);
                        remoteResult.SetProviderId(MetadataProvider.Tvdb, tvdbId);
                        tvIdResults[i] = remoteResult;
                    }

                    return(tvIdResults);
                }
            }

            var tvSearchResults = await _tmdbClientManager.SearchSeriesAsync(searchInfo.Name, searchInfo.MetadataLanguage, cancellationToken : cancellationToken)
                                  .ConfigureAwait(false);

            var remoteResults = new RemoteSearchResult[tvSearchResults.Count];

            for (var i = 0; i < tvSearchResults.Count; i++)
            {
                remoteResults[i] = MapSearchTvToRemoteSearchResult(tvSearchResults[i]);
            }

            return(remoteResults);
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <RemoteSearchResult> > GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
        {
            if (searchInfo.TryGetProviderId(MetadataProvider.Tmdb, out var tmdbId))
            {
                var series = await _tmdbClientManager
                             .GetSeriesAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), searchInfo.MetadataLanguage, searchInfo.MetadataLanguage, cancellationToken)
                             .ConfigureAwait(false);

                if (series != null)
                {
                    var remoteResult = MapTvShowToRemoteSearchResult(series);

                    return(new[] { remoteResult });
                }
            }

            if (searchInfo.TryGetProviderId(MetadataProvider.Imdb, out var imdbId))
            {
                var findResult = await _tmdbClientManager
                                 .FindByExternalIdAsync(imdbId, FindExternalSource.Imdb, searchInfo.MetadataLanguage, cancellationToken)
                                 .ConfigureAwait(false);

                var tvResults = findResult?.TvResults;
                if (tvResults != null)
                {
                    var imdbIdResults = new RemoteSearchResult[tvResults.Count];
                    for (var i = 0; i < tvResults.Count; i++)
                    {
                        var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]);
                        remoteResult.SetProviderId(MetadataProvider.Imdb, imdbId);
                        imdbIdResults[i] = remoteResult;
                    }

                    return(imdbIdResults);
                }
            }

            if (searchInfo.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId))
            {
                var findResult = await _tmdbClientManager
                                 .FindByExternalIdAsync(tvdbId, FindExternalSource.TvDb, searchInfo.MetadataLanguage, cancellationToken)
                                 .ConfigureAwait(false);

                var tvResults = findResult?.TvResults;
                if (tvResults != null)
                {
                    var tvIdResults = new RemoteSearchResult[tvResults.Count];
                    for (var i = 0; i < tvResults.Count; i++)
                    {
                        var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]);
                        remoteResult.SetProviderId(MetadataProvider.Tvdb, tvdbId);
                        tvIdResults[i] = remoteResult;
                    }

                    return(tvIdResults);
                }
            }

            var tvSearchResults = await _tmdbClientManager.SearchSeriesAsync(searchInfo.Name, searchInfo.MetadataLanguage, cancellationToken : cancellationToken)
                                  .ConfigureAwait(false);

            var tmdbSearchResults = new TMDbLib.Objects.TvShows.TvShow[tvSearchResults.Count];

            var remoteResults = new RemoteSearchResult[tvSearchResults.Count];

            for (var i = 0; i < tvSearchResults.Count; i++)
            {
                tmdbSearchResults[i] = await _tmdbClientManager
                                       .GetSeriesAsync(Convert.ToInt32(tvSearchResults[i].Id, CultureInfo.InvariantCulture), searchInfo.MetadataLanguage, searchInfo.MetadataLanguage, cancellationToken)
                                       .ConfigureAwait(false);

                remoteResults[i] = MapTvShowToRemoteSearchResult(tmdbSearchResults[i]);
            }

            int resultCountHelper = 0;

            for (var i = 0; i < remoteResults.Length; i++)
            {
                resultCountHelper += 1 + remoteResults[i].EpisodeGroups.Results.Count;
            }

            var remoteResultEpisodeGroups = new RemoteSearchResult[resultCountHelper];

            for (var i = 0; i < remoteResultEpisodeGroups.Length; i++)
            {
                for (var j = 0; j < tmdbSearchResults.Length; j++)
                {
                    remoteResultEpisodeGroups[i++] = MapTvShowToRemoteSearchResult(tmdbSearchResults[j]);

                    for (var k = 0; k < tmdbSearchResults[j].EpisodeGroups.Results.Count; k++)
                    {
                        remoteResultEpisodeGroups[i++] = MapTvShowEpisodeGroupToRemoteSearchResult(tmdbSearchResults[j], k);
                    }
                }
            }

            return(remoteResultEpisodeGroups);
        }