public void AddDestinationsForAirportsFromFlightsFromDotCom(AirportCollection airports, string startFrom = null)
 {
     TotalStopwatch.Restart();
     for (int i = 0; i < airports.Count; i++)
     {
         AirportStopwatch.Restart();
         Airport airport = airports[i];
         if (!startFrom.IsNullOrEmpty())
         {
             if (airport.IATA.Equals(startFrom))
             {
                 startFrom = null;
             }
             else
             {
                 LogProgressWhenCollectingDestinationAirports(i, airport, airports);
                 continue;
             }
         }
         Driver.Navigate().GoToUrl($"https://www.flightsfrom.com/{airport.IATA}/destinations");
         string          pageSource = Driver.PageSource;
         MatchCollection matches    = Regex.Matches(pageSource, @"<span class=""airport-font-midheader destination-search-item"">(.*?)\s+([A-Z][A-Z][A-Z])");
         foreach (Match match in matches)
         {
             string iata     = match.Groups[2].Value;
             string location = match.Groups[1].Value;
             airport.DestinationAirports.Add(new Airport(iata, location));
         }
         LogProgressWhenCollectingDestinationAirports(i, airport, airports);
     }
 }
示例#2
0
        static void Main(string[] args)
        {
            //worker.GetAllAirportsFromFlightsFromDotCom();

            //Dictionary<string, string> airports = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText("airports.json"));
            //AirportCollection collection = worker.GetAirportCollectionFromDictionary(airports);

            //AirportCollection collection = JsonConvert.DeserializeObject<AirportCollection>(File.ReadAllText("airportsWithDestinations-upToVIG.json"));
            //ChromeWorker_FlightsFromDotCom worker = new ChromeWorker_FlightsFromDotCom();
            //worker.AddDestinationsForAirportsFromFlightsFromDotCom(collection, startFrom: "VIG");

            AirportCollection fullCollection = JsonConvert.DeserializeObject<AirportCollection>(File.ReadAllText("airportsWithDestinations.json"));
            fullCollection.UpdateDestinationsWithCircularReferences();
            AirportToAirportPathsFinder pathsFinder = new AirportToAirportPathsFinder(fullCollection);
            AirportToAirportPaths paths = pathsFinder.FindPathsBetweenTwoAirports("SOF", "ABZ", stopAtFirstResults: false, maxAmountOfTransfers: 1);
            string pathsStr = paths.GetCollectionsAsPaths(excelFriendly: true);

            ChromeWorker_GoogleFlights googleWorker = new ChromeWorker_GoogleFlights();
            googleWorker.LookUpPathsOnGoogleFlights(paths, new DateTime(2021, 7, 28));
        }
 public AirportToAirportPathsFinder(AirportCollection airports)
 {
     Airports = airports;
 }
        private void LogProgressWhenCollectingDestinationAirports(int index, Airport airport, AirportCollection airportCollection)
        {
            TimeSpan timeLeft    = TimeSpan.FromMilliseconds((airportCollection.Count - (index + 1)) * AirportStopwatch.ElapsedMilliseconds);
            string   timeLeftStr = timeLeft.ToString(@"hh\:mm\:ss\:fff");

            TimeSpan elapsedTime    = TimeSpan.FromMilliseconds(TotalStopwatch.ElapsedMilliseconds);
            string   elapsedTimeStr = elapsedTime.ToString(@"hh\:mm\:ss\:fff");

            Debug.WriteLine($"{index + 1}/{airportCollection.Count} scanned ({airport.IATA})." + Environment.NewLine +
                            $"{airportCollection.Count} discovered." + Environment.NewLine +
                            $"{(index + 1) / (airportCollection.Count / 100.0)} %." + Environment.NewLine +
                            $"Total time spent so far: {elapsedTimeStr}." + Environment.NewLine +
                            $"Estimated time left: {timeLeftStr}");
        }
        public void LookUpPathsOnGoogleFlights(AirportToAirportPaths paths, DateTime?date = null)
        {
            date = date ?? DateTime.Now;

            Driver.Navigate().GoToUrl("https://www.google.com/travel/flights/search");
            IWebElement cookiesIframe = GetElementWithXPath(@"/html/body/c-wiz[1]/div[1]/div[1]/div[2]/div[2]/iframe");

            if (cookiesIframe != null)
            {
                Driver.SwitchTo().Frame(cookiesIframe);
                IWebElement signinAgreeButton = GetElementWithXPath("/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div");
                if (signinAgreeButton != null)
                {
                    signinAgreeButton.Click();
                }
                Driver.SwitchTo().DefaultContent();
            }

            for (int i = 0; i < paths.Count; i++)
            {
                AirportCollection path = paths[i];
                for (int j = 0; j < path.Count - 1; j++)
                {
                    Airport airportFrom = path[j];
                    Airport airportTo   = path[j + 1];
                    Driver.ExecuteScript($"window.open('https://www.google.com/travel/flights/search');");
                    ReadOnlyCollection <string> windows = Driver.WindowHandles;
                    Driver.SwitchTo().Window(windows[windows.Count - 1]);


                    IWebElement oneWayOrRoundTripSelection = GetElementWithXPath("/html/body/c-wiz[2]/div/div[2]/div/c-wiz/div/c-wiz/div[2]/div[1]/div[1]/div[1]/div[1]/div/div[1]/div[1]/div/button");
                    oneWayOrRoundTripSelection.Click();

                    IWebElement oneWayButton = GetElementWithXPath("/html/body/c-wiz[2]/div/div[2]/div/c-wiz/div/c-wiz/div[2]/div[1]/div[1]/div[1]/div[1]/div/div[1]/div[2]/div[2]/ul/li[2]");
                    oneWayButton.Click();

                    ReadOnlyCollection <IWebElement> inputs = (ReadOnlyCollection <IWebElement>)Driver.ExecuteScript("return document.querySelectorAll('input')");
                    IWebElement originInput = inputs[0];
                    originInput.Click();
                    Thread.Sleep(500);

                    IWebElement originInputUpdated = inputs[1];
                    originInputUpdated.Clear();
                    Thread.Sleep(500);
                    originInputUpdated.SendKeys(airportFrom.IATA);

                    Thread.Sleep(500);

                    const string getFirstOptionOfAIrportDropdown = "return arguments[0].parentElement.parentElement.parentElement.parentElement.querySelector('ul > li:nth-child(1)')";

                    IWebElement origin_firstChoice = (IWebElement)Driver.ExecuteScript(getFirstOptionOfAIrportDropdown, originInputUpdated);
                    origin_firstChoice.Click();

                    IWebElement destinationInput = inputs[2];
                    destinationInput.Click();
                    Thread.Sleep(100);

                    IWebElement destinationInputUpdated = inputs[3];
                    destinationInputUpdated.Clear();
                    destinationInputUpdated.SendKeys(airportTo.IATA);

                    Thread.Sleep(500);
                    IWebElement destination_firstChoice = (IWebElement)Driver.ExecuteScript(getFirstOptionOfAIrportDropdown, destinationInputUpdated);
                    destination_firstChoice.Click();

                    IWebElement dateInput = inputs[4];
                    dateInput.Click();

                    dateInput = inputs[6];
                    dateInput.SendKeys(date.GetValueOrDefault().ToString("ddd, MMM dd"));

                    Thread.Sleep(500);

                    IWebElement calendarDiv    = (IWebElement)Driver.ExecuteScript("return arguments[0].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement", dateInput);
                    IWebElement dateDoneButton = GetButtonThatContainsSpecificText("button", "Done", calendarDiv);
                    dateDoneButton.Click();
                    Thread.Sleep(500);

                    //IWebElement searchButton = GetButtonThatContainsSpecificText("button", "earch");

                    //searchButton.Click();
                    //Thread.Sleep(2000);

                    IWebElement stopsButton = GetButtonThatContainsSpecificText("button", "tops");
                    stopsButton.Click();

                    IWebElement nonStopOnlySelection = GetButtonThatContainsSpecificText("li", "number of stops");
                    nonStopOnlySelection.Click();

                    IWebElement stopsMenu        = (IWebElement)Driver.ExecuteScript("return arguments[0].parentElement.parentElement.parentElement", nonStopOnlySelection);
                    IWebElement closeStopsButton = (IWebElement)Driver.ExecuteScript("return arguments[0].querySelector('button')", stopsMenu);
                    closeStopsButton.Click();

                    //IWebElement dateInputNew = (IWebElement)Driver.ExecuteScript("return document.querySelectorAll('input')[4]");
                    //dateInputNew.Click();
                }
            }
        }
示例#6
0
 public Airport(string iATA, string location)
 {
     IATA                = iATA;
     Location            = location;
     DestinationAirports = new AirportCollection();
 }