public async Task ShouldProduceSearchResults()
        {
            // Arrange
            var searchParams = new SeriesSearchParams {
                Name = "The Bad Batch"
            };
            var authToken = await GetAuthTokenAsync();

            var sut = CreateDownloader();

            // Act
            var actual = await sut.GetSeriesSearchResultsAsync(searchParams, authToken, null);

            // Assert
            Assert.That(actual, Is.Not.Null);
            Assert.That(actual, Is.Not.Empty);

            TheTvDbSeriesSearchResult response =
                JsonConvert.DeserializeObject <TheTvDbSeriesSearchResult>(actual);

            Assert.That(response, Is.Not.Null);
            Assert.That(response.Data, Is.Not.Null);
            Assert.That(response.Data.Length, Is.GreaterThan(0));
            Assert.That(response.Data[0], Is.Not.Null);
            Assert.That(response.Data[0].SeriesName, Is.Not.Null);
            Assert.That(response.Data[0].SeriesName, Is.Not.Empty);
            Assert.That(response.Data[0].SeriesName, Is.EqualTo("The Bad Batch"));
            Assert.That(response.Data[0].Id, Is.EqualTo((int)KnownTvShowIds.StarWarsTheBadBatch));
        }
Exemple #2
0
        public async Task <string> GetSeriesSearchResultsAsync(SeriesSearchParams searchParams, string authToken, IWebProxy proxy)
        {
            string baseUrl = _configurationValues.ApiBaseUrl;
            string url     = baseUrl + Endpoints.SeriesSearch;

            Dictionary <string, string> queryParams = new Dictionary <string, string>
            {
                { "type", "series" },
            };

            if (!string.IsNullOrWhiteSpace(searchParams?.Name))
            {
                queryParams.Add("query", searchParams.Name);
            }
            // TODO: Search by IMDB ID
            //if (!string.IsNullOrWhiteSpace(searchParams?.ImdbId))
            //    queryParams.Add("imdbId", searchParams.ImdbId);

            string queryString = "?";

            foreach (var param in queryParams)
            {
                if (queryString.Length > 1)
                {
                    queryString += "&";
                }
                queryString += $"{WebUtility.UrlEncode(param.Key)}={WebUtility.UrlEncode(param.Value)}";
            }
            url += queryString;

            string json = await _httpDownloader.GetStringAsync(url,
                                                               new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "Authorization", $"Bearer {authToken}" }
            },
                                                               proxy);

            return(json);
        }
Exemple #3
0
        public async Task ShouldProduceSearchResults()
        {
            // Arrange
            var searchParams = new SeriesSearchParams {
                Name = "The Bad Batch"
            };
            var authToken = await GetAuthTokenAsync();

            var sut = CreateDownloader();

            // Act
            var actual = await sut.GetSeriesSearchResultsAsync(searchParams, authToken, null);

            // Assert
            Assert.That(actual, Is.Not.Null);
            Assert.That(actual, Is.Not.Empty);

            TheTvDbApiResponse <List <SearchResult> > response =
                JsonConvert.DeserializeObject <TheTvDbApiResponse <List <SearchResult> > >(actual);

            Assert.That(response, Is.Not.Null);
            Assert.That(response.Status, Is.EqualTo("success"));
            Assert.That(response.Data, Is.Not.Null);
            Assert.That(response.Data, Has.Count.GreaterThan(0));
            Assert.That(response.Data[0], Is.Not.Null);
            Assert.That(response.Data[0].Name, Is.Not.Null);
            Assert.That(response.Data[0].Name, Is.Not.Empty);
            Assert.That(response.Data[0].Name, Is.EqualTo("The Bad Batch"));
            Assert.That(response.Data[0].Type, Is.Not.Null);
            Assert.That(response.Data[0].Type, Is.Not.Empty);
            Assert.That(response.Data[0].Type, Is.EqualTo("series"));
            Assert.That(response.Data[0].TheTvDbId, Is.Not.Null);
            Assert.That(response.Data[0].TheTvDbId, Is.Not.Empty);
            Assert.That(response.Data[0].TheTvDbId, Is.EqualTo("385376"));
            Assert.That(response.Data[0].Id, Is.Not.Null);
            Assert.That(response.Data[0].Id, Is.Not.Empty);
            Assert.That(response.Data[0].Id, Is.EqualTo("series-385376"));
        }
Exemple #4
0
        public virtual async Task <Series[]> SearchAsync(SeriesSearchParams searchParams,
                                                         IWebProxy proxyPreferences)
        {
            string authToken = await _authenticator.GetAuthTokenAsync(proxyPreferences);

            string json = await _apiDownloader.GetSeriesSearchResultsAsync(searchParams, authToken, proxyPreferences);

            TheTvDbSeriesSearchResult searchResults = JsonConvert.DeserializeObject <TheTvDbSeriesSearchResult>(json);

            TheTvDbSeries[] data = searchResults.Data?.Take(MaxSearchResults).ToArray();

            Dictionary <string, Task <string> > imagesTasks =
                data?
                .Select(x => x.Id)
                .ToDictionary(
                    x => x.ToString(),
                    x => _apiDownloader
                    .GetSeriesImagesAsync(
                        new SeriesIdentifier {
                Id = x.ToString()
            },
                        authToken, proxyPreferences))
            ;

            if (imagesTasks != null)
            {
                await Task.WhenAll(imagesTasks.Values.ToArray());
            }

            List <Series> seriesResults = data?
                                          .Select(x => Adapt(
                                                      x,
                                                      JsonConvert.DeserializeObject <TheTvDbSeriesImageResults>(imagesTasks[x.Id.ToString()]?.Result)))
                                          .ToList();

            return(seriesResults?.ToArray() ?? new Series[0]);
        }
        public virtual async Task <Series[]> SearchAsync(SeriesSearchParams searchParams,
                                                         IWebProxy proxy)
        {
            string authToken = await _authenticator.GetAuthTokenAsync(proxy);

            string json = await _apiDownloader.GetSeriesSearchResultsAsync(searchParams, authToken, proxy);

            var searchResults = Deserialize <List <SearchResult> >(json);

            SearchResult[] data = searchResults.Data?.Take(MaxSearchResults).ToArray();

            Dictionary <string, Task <string> > imagesTasks =
                data?
                .Select(x => x.TheTvDbId)
                .ToDictionary(
                    x => x,
                    x => _apiDownloader
                    .GetSeriesExtendedAsync(
                        new SeriesIdentifier {
                Id = x
            },
                        authToken, proxy))
            ;

            if (imagesTasks != null)
            {
                await Task.WhenAll(imagesTasks.Values.ToArray());
            }

            List <Series> seriesResults = data?
                                          .Select(x => Adapt(
                                                      x,
                                                      Deserialize <SeriesExtendedRecord>(imagesTasks[x.TheTvDbId]?.Result)?.Data))
                                          .ToList();

            return(seriesResults?.ToArray() ?? new Series[0]);
        }
        public async Task AreDataProvidersComparable(KnownTvShowIds showId)
        {
            // Arrange
            var searchTerm     = GetSearchTerm(showId);
            var dateProvider   = new RealTimeDateProvider();
            var httpDownloader = new HttpDownloader();
            var configValues   = new TestConfigurationValues();
            var localPaths     = new TestLocalPaths();

            var v2Config        = new V2.TheTvDbConfigurationValues(configValues);
            var v2ApiDownloader = new V2.TheTvDbApiDownloader(v2Config, httpDownloader);
            var v2Authenticator = new V2.TheTvDbAuthenticator(dateProvider, v2Config, localPaths, v2ApiDownloader);
            var v2DataProvider  = new V2.TheTvDbEpisodeDataProvider(v2Config, v2Authenticator, v2ApiDownloader);

            var v4Config        = new V4.TheTvDbConfigurationValuesV4(configValues);
            var v4ApiDownloader = new V4.TheTvDbApiDownloaderV4(v4Config, httpDownloader);
            var v4Authenticator = new V4.TheTvDbAuthenticatorV4(dateProvider, v4Config, localPaths, v4ApiDownloader);
            var v4DataProvider  = new V4.TheTvDbEpisodeDataProviderV4(v4Authenticator, v4ApiDownloader);

            var searchParams = new SeriesSearchParams {
                Name = searchTerm
            };

            // Act
            var v2SearchResult = await v2DataProvider.SearchAsync(searchParams, null);

            var v4SearchResult = await v4DataProvider.SearchAsync(searchParams, null);

            // Assert
            Assert.That(v2SearchResult, Is.Not.Null, "Expected v2 search results to be not null.");
            Assert.That(v4SearchResult, Is.Not.Null, "Expected v4 search results to be not null.");

            Assert.That(v2SearchResult.Length, Is.GreaterThan(0), "Expected to have v2 search results.");
            Assert.That(v4SearchResult.Length, Is.GreaterThan(0), "Expected to have v4 search results.");

            var v2BestMatch = v2SearchResult.FirstOrDefault(x => x.Id == showId.ToString("D"));

            Assert.That(v2BestMatch, Is.Not.Null, "Expected best matching v2 search result to be not null.");
            Assert.That(v2BestMatch.Id, Is.EqualTo(showId.ToString("D")), "Expected best matching v2 search result to be {0}, but was {1}.", searchTerm, v2BestMatch.Name);

            var v4BestMatch = v4SearchResult.FirstOrDefault(x => x.Id == v2BestMatch.Id);

            Assert.That(v4BestMatch, Is.Not.Null, "Expected equivalent v4 search result.");

            Assert.That(v4BestMatch.Name, Is.EqualTo(v2BestMatch.Name), "Expected names to match.");
            //Assert.That(v4BestMatch.SeriesIdentifier.ImdbId, Is.EqualTo(v2BestMatch.SeriesIdentifier.ImdbId), "Expected IMDB IDs to match.");
            Assert.That(v4BestMatch.Year, Is.EqualTo(v2BestMatch.Year), "Expected years to match.");
            //Assert.That(v4BestMatch.Description, Is.EqualTo(v2BestMatch.Description), "Expected descriptions to match.");

            var seriesIdentifier = GetSeriesIdentifier(showId);
            var v2Series         = await v2DataProvider.GetEpisodesAsync(seriesIdentifier, null);

            var v4Series = await v4DataProvider.GetEpisodesAsync(seriesIdentifier, null);

            Assert.That(v2Series, Is.Not.Null, "Expected v2 series to be not null.");
            Assert.That(v4Series, Is.Not.Null, "Expected v4 series to be not null.");

            Assert.That(v4Series.Id, Is.EqualTo(v2Series.Id), "Expected series IDs to match.");
            Assert.That(v4Series.Name, Is.EqualTo(v2Series.Name), "Expected series names to match.");
            //Assert.That(v4Series.ImdbId, Is.EqualTo(v2Series.ImdbId), "Expected series IMDB IDs to match.");
            //Assert.That(v4Series.Year, Is.EqualTo(v2Series.Year), "Expected series years to match.");
            //Assert.That(v4Series.AirDate, Is.EqualTo(v2Series.AirDate), "Expected series air dates to match.");
            //Assert.That(v4Series.Network, Is.EqualTo(v2Series.Network), "Expected series networks to match.");
            Assert.That(v4Series.Status, Is.EqualTo(v2Series.Status), "Expected series statuses to match.");


            Assert.That(v2Series.Seasons, Is.Not.Null);
            Assert.That(v4Series.Seasons, Is.Not.Null);
            Assert.That(v4Series.Seasons.Count, Is.EqualTo(v2Series.Seasons.Count), "Expected season counts to match.");
            for (int i = 0; i < v4Series.Seasons.Count; i++)
            {
                var v2Season = v2Series.Seasons[i];
                var v4Season = v4Series.Seasons[i];

                Assert.That(v4Season.SeasonNumber, Is.EqualTo(v2Season.SeasonNumber));

                Assert.That(v2Season.Episodes, Is.Not.Null);
                Assert.That(v4Season.Episodes, Is.Not.Null);
                Assert.That(v4Season.Episodes.Count, Is.EqualTo(v2Season.Episodes.Count), "Expected episode counts to match.");

                for (int j = 0; j < v4Season.Episodes.Count; j++)
                {
                    var v2Episode = v2Season.Episodes[j];
                    var v4Episode = v4Season.Episodes[j];

                    Assert.That(v4Episode.EpisodeNumber, Is.EqualTo(v2Episode.EpisodeNumber));
                    Assert.That(IsCloseEnough(v4Episode.Title, v2Episode.Title), "Expected \"{0}\", but was \"{1}\", Season {2} Episode {3}.", v2Episode.Title, v4Episode.Title, v4Season.SeasonNumber, v4Episode.EpisodeNumber);
                }
            }
        }