public SearchMonitoringTask(SearchMonitoringTaskGroup ownerGroup, ScraperBase store)
 {
     this.GourpId           = ownerGroup.Id;
     this.Store             = store;
     this.MonitoringOptions = new List <MonitoringOptions>();
     this.MonitoringOptions.Add(ownerGroup.Options);
 }
示例#2
0
        public UrlMonitoringTask(string url)
        {
            Uri parsedUri = new Uri(url);

            foreach (var scraper in Session.Current.AvailableScrapers)
            {
                Uri scraperUri = new Uri(scraper.WebsiteBaseUrl);
                var result     = Uri.Compare(parsedUri, scraperUri, UriComponents.Host, UriFormat.SafeUnescaped,
                                             StringComparison.InvariantCultureIgnoreCase);

                if (result != 0)
                {
                    continue;
                }
                this._scraper = scraper;
                this.Url      = url;
                try
                {
                    this._oldDetails = _scraper.GetProductDetails(Url, this.TokenSource.Token);
                }
                catch (Exception e)
                {
                    Logger.Instance.WriteErrorLog($"Error occured while obtaining product info. Please Try Again.. \n msg={e.Message}");
                    throw new Exception("Error occured while obtaining product info");
                }

                return;
            }

            throw new Exception("We don't support this website yet");
        }
示例#3
0
        public static HashSet <Product> ScrapeCurrentProductsList(ScraperBase website, CancellationToken token)
        {
            List <Product> products = new List <Product>();

            Utils.TrySeveralTimes(() => website.ScrapeAllProducts(out products, ScrappingLevel.Url, token), AppSettings.Default.ProxyRotationRetryCount);


            return(new HashSet <Product>(products));
        }
示例#4
0
        public IReadOnlyCollection <Product> GetCurrentProducts(ScraperBase website)
        {
            if (_allShopTasks.TryGetValue(website, out var task))
            {
                return(task.CurrentProducts.ToList().AsReadOnly());
            }

            throw new KeyNotFoundException($"Can't retrieve product list of {website.WebsiteName}, \n because monitoring on this website not runnning");
        }
示例#5
0
        public void StartMonitoringTask(ScraperBase website)
        {
            if (!_allShopTasks.TryGetValue(website, out var task))
            {
                throw new KeyNotFoundException("Can't start task. Task with this name not found");
            }

            task.Start();
        }
示例#6
0
        public void RemoveMonitoringTask(ScraperBase website)
        {
            if (!_allShopTasks.TryGetValue(website, out var task))
            {
                throw new KeyNotFoundException($"Can't remove {website} monitoring, because it is not registered");
            }

            task.TokenSource.Cancel();
            _allShopTasks.Remove(website);
        }
示例#7
0
 public void AddNewProductHandler(ScraperBase website, NewProductHandler handler)
 {
     if (_allShopTasks.TryGetValue(website, out var task))
     {
         task.Handler += handler;
     }
     else
     {
         throw new KeyNotFoundException($"Can't add new product handler to {website.WebsiteName}, \n because monitoring on this website not running");
     }
 }
示例#8
0
        public async Task RegisterMonitoringTaskAsync(ScraperBase website, CancellationToken token)
        {
            if (_allShopTasks.ContainsKey(website))
            {
                Logger.Instance.WriteVerboseLog("Requested shop monitoring task is already added (Adding Process Skipped)");
                return;
            }

            var curList = await Task.Run(() => ScrapeCurrentProductsList(website, token), token);

            var task = new ShopMonitoringTask()
            {
                Website         = website,
                CurrentProducts = curList,
                TokenSource     = new CancellationTokenSource(),
            };

            _allShopTasks.Add(website, task);
        }