예제 #1
0
        private static void pollingHotelFromHtmlPage(String connString, String filePath)
        {
            Boolean       success = false;
            DirectoryInfo di      = new DirectoryInfo(filePath);

            FileInfo[] files = di.GetFiles("*.html", SearchOption.TopDirectoryOnly);
            foreach (FileInfo fi in files)
            {
                HtmlDocument doc = new HtmlDocument();
                doc.Load(fi.FullName);
                String             region   = System.Text.RegularExpressions.Regex.Match(fi.FullName, @"in (.+?).html").Groups[1].Value;
                HtmlNodeCollection nodeList = doc.DocumentNode.SelectNodes("//ol/li");
                display($"{region}: {nodeList.Count}");
                Int32 index = 1;
                //HtmlNode node = doc.DocumentNode.SelectSingleNode($"//ol/li[{index++}]");
                HtmlNode node = doc.DocumentNode.SelectSingleNode($"//ol[1]");
                while (node != null)
                {
                    HotelInfo hotel = new HotelInfo(node, region);
                    success = DataOperation.AddHotel(connString, hotel);
                    node    = doc.DocumentNode.SelectSingleNode($"//ol/li[{index++}]");
                }
                ;
                //foreach (HtmlNode node in nodeList)
                //{
                //    HotelInfo hotel = new HotelInfo(node, region);
                //    success = DataOperation.AddHotel(connString, hotel);
                //}
            }
        }
예제 #2
0
        private static void pollingEmail(string connString)
        {
            Boolean success = false;
            WebDriverSettingInfo webDriverSetting = new WebDriverSettingInfo(hideCommand: true, hideBrowser: false);
            List <HotelInfo>     hotels           = DataOperation.GetHotels(connString);

            display($"polling Email: {hotels.Count}:", isWriteLine: true, addTime: true);
            ChromeDriver driver = Utilities.WebDriverExtension.GetChromeDriver(hideBrowser: webDriverSetting.HideBrowser, hideCommand: webDriverSetting.HideCommand, isMaximized: true);
            Int32        count  = 0;

            foreach (HotelInfo hotel in hotels)
            {
                display($"[{hotel.Id}] {hotel.Name} -> ", isWriteLine: false, addTime: true);
                try
                {
                    driver.Navigate().GoToUrl("https://www.google.ca");
                    IWebElement element;
                    element = driver.FindElement(By.Id("lst-ib"));
                    element.SendKeys($"{hotel.Name}, {hotel.Region}");
                    element.SendKeys(Keys.Return);
                    element = driver.FindElement(By.XPath("//a[@class='ab_button' and text()='Website']"));
                    element.Click();

                    hotel.Update(driver);
                }
                catch (Exception ex)
                {
                    hotel.Comments = ex.Message;
                }
                success = DataOperation.AddHotel(connString, hotel);
                ++count;
                display($" [Done]", isWriteLine: true);
            }
            driver.Quit();
        }
예제 #3
0
        private static void pollingHotel(String connString)
        {
            WebDriverSettingInfo webDriverSetting = new WebDriverSettingInfo(hideCommand: true, hideBrowser: false);
            Boolean success = false;
            String  baseUrl = "https://ca.hotels.com/search.do?resolved-location=CITY:{id}:UNKNOWN:UNKNOWN&f-star-rating=3,2&destination-id={id}&q-destination={region}&q-check-in={checkin}&q-check-out={checkout}&q-rooms=1&q-room-0-adults=2&q-room-0-children=0";

            baseUrl = baseUrl.Replace("{checkin}", "2018-09-25");
            baseUrl = baseUrl.Replace("{checkout}", "2018-09-26");
            Dictionary <String, String> cities = new Dictionary <string, string>()
            {
                { "170961", "Kitchener, Ontario, Canada" },
                { "1633548", "Hamilton, Ontario" },
                { "169995", "Quebec, Quebec, Canada" },
                { "169714", "Ottawa, Ontario, Canada" },
                { "167228", "Edmonton, Alberta, Canada" },
                { "169003", "Calgary, Alberta, Canada" },
                { "169712", "Vancouver, British Columbia, Canada" },
                { "169716", "Montreal, Quebec, Canada" },
                { "1636865", "Toronto, Ontario, Canada" },
                { "170165", "Winnipeg, Manitoba, Canada" }
            };
            ChromeDriver driver = Utilities.WebDriverExtension.GetChromeDriver(hideBrowser: webDriverSetting.HideBrowser, hideCommand: webDriverSetting.HideCommand, isMaximized: true);

            foreach (String key in cities.Keys)
            {
                display($"{cities[key]} -> ", isWriteLine: false, addTime: true);
                String url = baseUrl.Replace("{id}", key);
                url = url.Replace("{region}", cities[key]);
                driver.Navigate().GoToUrl(url);
                Int32 count = scrollToBottom(driver, "info unavailable-info", 2, true);
                IReadOnlyCollection <IWebElement> elements = driver.FindElementsByXPath("//ol/li");
                display($" pages: [{count}] -> [{elements.Count}]", isWriteLine: false);
                count = 0;
                foreach (IWebElement element in elements)
                {
                    HotelInfo hotel = new HotelInfo(driver, element, cities[key]);
                    if (!String.IsNullOrEmpty(hotel.Name))
                    {
                        success = DataOperation.AddHotel(connString, hotel);
                        ++count;
                        if (count % 10 == 0)
                        {
                            display($".", isWriteLine: false);
                        }
                    }
                }
                display($" Done[{count}]", isWriteLine: true);
            }
            driver.Quit();
        }