Exemplo n.º 1
0
        void AnalyzeResort(Resort resort, IWebDriver driver)
        {
            string content;

            driver.Url = resort.Url;

            try
            {
                driver.FindElement(By.ClassName("js-privacywall-agree")).Click(); //agreeing cookies

                IWebElement daily = driver.FindElement(By.CssSelector("section[data-id=\"" + resort.Container + "\"]"));

                List <IWebElement> articles = new List <IWebElement>();
                articles.AddRange(daily.FindElements(By.TagName("article")));

                foreach (IWebElement item in articles)
                {
                    try
                    {
                        driver.ClickOpenNewTab(item);

                        try
                        {
                            content = driver.PageSource;

                            ProcessArticle(content);

                            if (driver.WindowHandles.Count >= 2)
                            {
                                driver.Close(); //close this tab
                                driver.SwitchTabs(0);
                            }

                            foreach (string name in authorname)
                            {
                                if (authorname.Count > 5)
                                {
                                    new Exception().LogErrorToTSV("To many Authors -> It's very likely that the extracting from the authorname didn't worked properly so the article" + title.Preview(20) + " was rejected. There were " + authorname.Count + " many authors");
                                    break;
                                }
                                string temp = "";
                                if (name != "" && date != null && workload != 0 && articleKicker != "" && name.Length <= 100)
                                {
                                    temp = name;
                                    temp = temp.ReplaceChars("", '\r', '\n', '\t');
                                    temp = temp.Trim(' ');

                                    if (temp != "")
                                    {
                                        resort.AddArticle(temp, date, workload, title, articleKicker);
                                    }
                                }
                                else
                                {
                                    new Exception().LogErrorToTSV("something was wrong with the article data so it was not added. Article: author: " + temp.Preview(20) + ", date: " + date.ToString() + ", workload: " + workload.ToString() + ", title: " + title.Preview(20) + ", articleKicker: " + articleKicker.Preview(20));
                                }
                            }
                            authorname.Clear();
                        }
                        catch (Exception e)
                        {
                            e.LogErrorToTSV("while trying to get the content of the article: " + item.FindElement(By.TagName("a")).GetAttribute("href"));
                        }
                    }
                    catch (Exception e)
                    {
                        e.LogErrorToTSV("while opening the Article " + item.FindElement(By.TagName("a")).GetAttribute("href"));
                    }
                }
            }
            catch (Exception e)
            {
                e.LogErrorToTSV("while aggreeing the cookies or getting IWebElement article in resort " + resort.name);
            }
        }