예제 #1
0
        /// <inheritdoc />
        public override List <BasicInfo> GetResponse(List <string> entities, bool outputResults)
        {
            if (!TmdbServiceConfiguration.HasValue)
            {
                TmdbServiceConfiguration.SetServiceConfigurationIfNeed(
                    GetServiceConfiguration(outputResults)
                    );
            }

            // Use HashSet to avoid duplicated data which can produce errors in further work.
            var searchResults = new HashSet <BasicInfo>();

            foreach (string movie in entities)
            {
                SearchContainer <SearchMovie> response = _tmdbClient.SearchMovieAsync(movie).Result;
                if (response.Results.IsNullOrEmpty())
                {
                    _logger.Warn($"{movie} wasn't processed.");
                    GlobalMessageHandler.OutputMessage($"{movie} wasn't processed.");
                    continue;
                }

                // Get first search result from response and ignore all the rest.
                SearchMovie searchResult = response.Results.First();
                if (outputResults)
                {
                    GlobalMessageHandler.OutputMessage($"Got {searchResult.Title} from {Tag}");
                }

                TmdbMovieInfo extractedInfo = _dataMapper.Transform(searchResult);
                searchResults.Add(extractedInfo);
            }
            return(searchResults.ToList());
        }
        public override async Task <bool> GetResponse(BufferBlock <string> entitiesQueue,
                                                      BufferBlock <BasicInfo> responsesQueue, bool outputResults)
        {
            if (!TmdbServiceConfiguration.HasValue)
            {
                TmdbServiceConfiguration.SetServiceConfigurationIfNeed(
                    await GetServiceConfiguration(outputResults)
                    );
            }

            // Use HashSet to avoid duplicated data which can produce errors in further work.
            var searchResults = new HashSet <BasicInfo>();

            while (await entitiesQueue.OutputAvailableAsync())
            {
                string movie = await entitiesQueue.ReceiveAsync();

                SearchContainer <SearchMovie> response = await _tmdbClient.SearchMovieAsync(movie);

                if (response.Results.IsNullOrEmpty())
                {
                    _logger.Warn($"{movie} wasn't processed.");
                    GlobalMessageHandler.OutputMessage($"{movie} wasn't processed.");
                    continue;
                }

                // Get first search result from response and ignore all the rest.
                SearchMovie searchResult = response.Results.First();
                if (outputResults)
                {
                    GlobalMessageHandler.OutputMessage($"Got {searchResult.Title} from {Tag}");
                }

                TmdbMovieInfo extractedInfo = _dataMapper.Transform(searchResult);
                if (searchResults.Add(extractedInfo))
                {
                    await responsesQueue.SendAsync(extractedInfo);
                }
            }
            return(searchResults.Count != 0);
        }