Exemple #1
0
        public async Task ExecuteProcessAsync(List <ProductSchema> products, EndpointSchema endpoint)
        {
            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
            Browser = await Puppeteer.LaunchAsync(new LaunchOptions
            {
                Headless = true,
                Args     = new[] {
                    "--no-sandbox"
                }
            });

            _logger.LogInformation("Launched Browser.");

            Endpoint = endpoint;

            foreach (var product in products)
            {
                try
                {
                    List <EndpointItem> results = new List <EndpointItem>();

                    if (product.ProductUrls.Count > 0)
                    {
                        var productUrl = product.ProductUrls.Find(x => x.Endpoint == Endpoint.Name);

                        if (productUrl != null)
                        {
                            _logger.LogInformation($"Getting results for Product ({product.Title}) by Custom URL.");
                            results = await GetURLResults(productUrl.URL);
                        }
                    }
                    else
                    {
                        _logger.LogInformation($"Getting results for Product ({product.Title}) by Query string Search.");
                        results = await GetQueryResults(product.Title);
                    }

                    var currentInStocks = await _mongoService.GetInStockByEndpoint(Endpoint.Name);

                    var currentInStockItems = currentInStocks.Select(x => x.EndpointItem).ToList(); // Mapping to a SubList

                    if (results.Count < currentInStockItems.Count)
                    {
                        var missingItems = currentInStockItems.Except(results);

                        foreach (var item in missingItems)
                        {
                            await _mongoService.DeleteInStock(item.PageUrl);

                            item.Available = false;
                            await _webhookClient.SendMessageAsync($"<@&{_roleId}> This item is now out of Stock!", false, DiscordHelpers.BuildEmbed(item));
                        }
                    }

                    if (results.Count < 1)
                    {
                        throw new Exception($"No Results Found for Product ({product.Title}).");
                    }

                    foreach (var res in results)
                    {
                        var instock = currentInStocks.Find(x => x.URL == res.PageUrl);

                        if (instock != null)
                        {
                            continue;
                        }

                        _logger.LogInformation($"Found new stock for product: \"{product.Title}\" @ {endpoint.Name}.");

                        await _mongoService.PostInStock(new InStockSchema()
                        {
                            URL          = res.PageUrl,
                            EndpointName = Endpoint.Name,
                            EndpointItem = res
                        });

                        await _webhookClient.SendMessageAsync($"<@&{_roleId}>", false, DiscordHelpers.BuildEmbed(res));
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.ToString());
                }
            }

            await Browser.CloseAsync();

            _logger.LogInformation("Closed Browser.");
        }