示例#1
0
        private DateTime ParseDataApresentacao(IHtmlAnchorElement a)
        {
            // writetxt(MontaHint('30/09/2003','Legislação Societária','Apresentação  ','29/10/2003 13:07','P'));
            var javascript = a.GetAttribute("onmouseover");
            var items      = javascript.Split(',');
            var date       = items[3].Replace("\"", "").Replace("\'", "");
            var data       = DateTime.ParseExact(date, "dd/MM/yyyy H:mm", new CultureInfo("pt-BR"));

            return(data);
        }
        public async Task <IDocument> NavigateToNextPage(IDocument document)
        {
            // Obter todos os botões de navegação
            IHtmlCollection <IElement> buttonsToNav = document.QuerySelectorAll("li.pagination-item");

            // Obter o último
            IElement buttonNextPage = buttonsToNav.Last();

            // Obter o elemento ancora dentro do botao de navegação
            IHtmlAnchorElement buttonGoNextPage = buttonNextPage.ChildNodes.OfType <IHtmlAnchorElement>().FirstOrDefault();

            // Vamos tentar retirar o número da página e colocá-la numa varíavel

            try
            {
                string nextWebsiteLinkPage = buttonGoNextPage.GetAttribute("href");

                string toBeSearched = "pag=";

                string numberPageString = nextWebsiteLinkPage.Substring(nextWebsiteLinkPage.IndexOf(toBeSearched) + toBeSearched.Length);

                if (numberPageString != string.Empty)
                {
                    if (Convert.ToInt32(numberPageString) > NumberPage)
                    {
                        NumberPage = Convert.ToInt32(numberPageString);
                    }

                    // Para evitar um eterno loop.
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (FormatException ex)
            {
                _ = new LogWriter("format", ex.Message.ToString());
            }

            // Navegar para a página
            return(await(buttonGoNextPage?.NavigateAsync()));
        }
        async Task <IEnumerable <IDeal> > IScraper.Scrape(CancellationToken token)
        {
            Url       url      = Url.Create("https://www.gog.com");
            IDocument document = await context.OpenAsync(url, token);

            token.ThrowIfCancellationRequested();

            IHtmlAnchorElement giveawayAnchor = document.Body.QuerySelector <IHtmlAnchorElement>(".giveaway-banner");

            if (giveawayAnchor == null)
            {
                logger.Info("No giveaway found");
                return(new List <IDeal>());
            }

            string onclickContent = giveawayAnchor.GetAttribute("onclick");
            Match  match          = Regex.Match(onclickContent, "'.+'");

            if (!match.Success)
            {
                logger.Info("No onclick found");
                return(new List <IDeal>());
            }

            string encodedJson = match.Value.Trim('\'');
            string json        = Regex.Unescape(encodedJson);

            GoodOldGamesData data = JsonConvert.DeserializeObject <GoodOldGamesData>(json);

            return(new List <IDeal>
            {
                new Deal
                {
                    Image = $"https://images-1.gog-statics.com/{data.Logo.Image}.png",
                    Link = $"https://www.gog.com{data.GameUrl}",
                    Title = data.Title,
                    End = DateTimeOffset.FromUnixTimeMilliseconds(data.EndTime).UtcDateTime
                }
            });
        }
        public async Task <IEnumerable <IDeal> > Scrape(CancellationToken token)
        {
            List <IDeal> deals = new List <IDeal>();

            DocumentRequest request  = DocumentRequest.Get(Url.Create(URL));
            IDocument       document = await context.OpenAsync(request, token);

            token.ThrowIfCancellationRequested();

            IHtmlElement body = document.Body;

            IEnumerable <IHtmlListItemElement> items = body.QuerySelectorAll <IHtmlListItemElement>(".grid-tile");

            foreach (IHtmlListItemElement element in items)
            {
                IHtmlDivElement titleElement = element.QuerySelector <IHtmlDivElement>(".card-title");
                string          title        = titleElement.TextContent.Trim();

                IHtmlAnchorElement linkElement         = element.QuerySelector <IHtmlAnchorElement>(".thumb-link");
                IHtmlImageElement  imageElement        = element.QuerySelector <IHtmlImageElement>(".product_image");
                IHtmlDivElement    priceElement        = element.QuerySelector <IHtmlDivElement>(".card-price");
                IHtmlDivElement    availabilityElement = element.QuerySelector <IHtmlDivElement>(".product-availability-label");
                string             offerStartDate      = availabilityElement.GetAttribute("data-freeofferstartdate");
                string             offerEndDate        = availabilityElement.GetAttribute("data-freeofferenddate");

                bool hasStartDate = DateTime.TryParseExact(offerStartDate, "ddd MMM dd HH:mm:ss Z yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime startDate);
                bool hasEndDate   = DateTime.TryParseExact(offerEndDate, "ddd MMM dd HH:mm:ss Z yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime endDate);

                if (!hasStartDate && !hasEndDate)
                {
                    logger.Info($"{title} has no start or end date");
                    continue;
                }

                DateTime now = DateTime.Now;
                if ((hasStartDate && now < startDate) || (hasEndDate && now > endDate))
                {
                    logger.Info($"{title} is not active right now");
                    continue;
                }

                string price = priceElement.TextContent.Trim();
                if (price.ToLower() != "free to play")
                {
                    logger.Info($"{title} is not free");
                    continue;
                }

                deals.Add(new Deal
                {
                    Discount = 100,
                    End      = hasEndDate ? endDate : (DateTime?)null,
                    Start    = hasStartDate ? startDate : (DateTime?)null,
                    Title    = title,
                    Link     = $"https://store.ubi.com/{linkElement.GetAttribute("href")}",
                    Image    = imageElement.GetAttribute("data-desktop-src")
                });
            }

            return(deals);
        }