Exemplo n.º 1
0
        public async Task <string> ScrapeAndStore(int start = 1, int count = 20, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (start < 1)
            {
                throw new TVMazeScraperBadRequestException("Start TVMaze ID shouldn't be less that 1");
            }
            if (count < 1)
            {
                throw new TVMazeScraperBadRequestException("Count of TVMaze shows for processing shouldn't be less that 1");
            }

            int storedShowsCount = 0;
            var scrappedShows    = await _tvMazeService.Scrape((uint)start, (uint)count, cancellationToken);

            if (scrappedShows != null && scrappedShows.Count > 0)
            {
                storedShowsCount = await _tvShowService.StoreTVShows(scrappedShows);
            }

            var result = $"{scrappedShows?.Count} of {storedShowsCount} scrapped TV Maze Shows were stored of {count}";

            _logger.Log(LogLevel.Information, result);

            return(result);
        }
        private async Task <int> ScrapeFrom(uint tvMazeID, ITVShowService tvShowService, ITVMazeService tvMazeService)
        {
            int  updatedTVShows   = 0;
            uint pageSize         = 20;
            int  storedShowsCount = 0;

            do
            {
                var scrappedShows = await tvMazeService.Scrape(tvMazeID, pageSize);

                if (scrappedShows != null && scrappedShows.Count > 0)
                {
                    storedShowsCount = await tvShowService.StoreTVShows(scrappedShows);
                }

                _logger.Log(LogLevel.Information, $"{scrappedShows?.Count} of {storedShowsCount} scrapped TV Maze Shows were stored of {pageSize}");
                tvMazeID += pageSize;
            }while (storedShowsCount > 0);

            return(updatedTVShows);
        }