Пример #1
0
        private async Task <ScraperViewModel> ScrapeOne(INeweggScraperService scraperService, string url)
        {
            var viewModel = new ScraperViewModel
            {
                NumberOfSuccessfullyAdded = 0,
                NumberOfErrors            = 0,
            };
            var message = new ScraperViewModelMessage
            {
                Url = url,
            };

            try
            {
                message.Message = await scraperService.ScrapeFromProductPageAsync(url);

                viewModel.NumberOfSuccessfullyAdded++;
            }
            catch (Exception exeption)
            {
                this.logger.LogWarning($"Exeption occured: {exeption.Message}.");
                viewModel.NumberOfErrors++;
                message.Message       = exeption.Message;
                message.InnerExeption = exeption.InnerException.Message;
            }

            viewModel.Messages.Add(message);
            return(viewModel);
        }
Пример #2
0
        private async Task <ScraperViewModel> ScrapeManyAsync(INeweggScraperService scraperService, string url, int pages)
        {
            var urls      = this.neweggUrlScraper.GetUrlsForScrapingFromProducts(url, pages);
            var count     = urls.Count;
            var counter   = 0;
            var viewModel = new ScraperViewModel
            {
                NumberOfSuccessfullyAdded = 0,
                NumberOfErrors            = 0,
            };

            this.logger.LogInformation(string.Empty + count);
            foreach (var u in urls)
            {
                var message = new ScraperViewModelMessage
                {
                    Url = u,
                };
                try
                {
                    message.Message = await scraperService.ScrapeFromProductPageAsync(u);

                    viewModel.NumberOfSuccessfullyAdded++;
                }
                catch (Exception exeption)
                {
                    this.logger.LogWarning($"Exeption occured: {exeption.Message}.");
                    viewModel.NumberOfErrors++;
                    message.Message       = exeption.Message;
                    message.InnerExeption = exeption.InnerException?.Message;
                }

                viewModel.Messages.Add(message);
                this.logger.LogInformation(string.Empty + (count - (++counter)));
            }

            return(viewModel);
        }