Ejemplo n.º 1
0
        private List <Flight> ParseFlights(string departureCity, string destinationCity, string airCompany, IWebElement webElement)
        {
            var list = new List <Flight>();
            var rows = webElement.FindElements(By.XPath("./tbody/tr"));

            for (var i = 0; i < rows.Count; i++)
            {
                try
                {
                    webElement = _driver.FindElements(By.XPath(ResultsTablePath)).First();
                    var rowArray         = webElement.FindElements(By.XPath("./tbody/tr")).ToArray();
                    var row              = rowArray[i];
                    var departureDateRaw = row.FindElement(By.XPath("./td[1]")).Text.Split(", ")[0];

                    DateTime.TryParseExact(departureDateRaw, "dd.MM.yyyy", null, System.Globalization.DateTimeStyles.None, out var departureDate);
                    var destinationDate = departureDate;
                    var secondTd        = row.FindElement(By.XPath("./td[2]"));
                    var secondTdRows    = secondTd.Text.Split("\r\n");
                    if (secondTdRows[1].Contains("+1"))
                    {
                        destinationDate = destinationDate.AddDays(1);
                    }
                    var departures          = secondTdRows[0].Split(" ");
                    var departureTime       = departures.First();
                    var departureCityCode   = departures.Last();
                    var destinations        = secondTdRows[1].Split(" ");
                    var destinationTime     = destinations.First();
                    var destinationCityCode = destinations.Last().Replace(" +1", "");

                    var flightNo  = row.FindElement(By.XPath("./td[3]")).Text;
                    var airCraft  = row.FindElement(By.XPath("./td[4]")).Text.Split("\r\n").Last();
                    var newFlight =
                        new Flight
                    {
                        DepartureDate          = departureDate,
                        DepartureTime          = departureTime,
                        DepartureCity          = departureCity,
                        DepartureCityCode      = departureCityCode,
                        DepartureAirportCode   = departureCityCode,
                        DestinationDate        = destinationDate,
                        DestinationTime        = destinationTime,
                        DestinationAirportCode = destinationCityCode,
                        OperatorName           = Name,
                        FlightNo          = flightNo,
                        FlightCompanyName = airCompany,
                        AircraftKind      = airCraft,
                        OperatorId        = OperatorId,
                        CollectDate       = DateTime.Now
                    };
                    list.Add(newFlight);
                }
                catch (Exception ex)
                {
                    Screenshot screen = ((ITakesScreenshot)_driver).GetScreenshot();
                    screen.SaveAsFile($"AnexError{DateTime.Now.ToString("yyyy.MM.dd.HH.m")}.png", ScreenshotImageFormat.Png);

                    Debug.WriteLine(ex.ToString());
                    _logger?.LogError(ex, "[Anex] ParseFlights");
                }
            }
            Thread.Sleep(1200);

            return(list);
        }
Ejemplo n.º 2
0
        private List <Flight> ParseFlights(string departureCity, string departureCityCode, string destinationCountry, string destinationCity, IWebElement flightTable)
        {
            var list = new List <Flight>();
            var rows = flightTable.FindElements(By.XPath("./div[@class='day-wrapper']"));

            try
            {
                _logger?.LogInformation($"[Pegas] departure city {departureCity} destinationCity {destinationCity}");
                _logger?.LogWarning($"[Pegas] Город вылета {departureCity}. " +
                                    $"Страна прилёта {destinationCountry}. Город прилёта {destinationCity}. " +
                                    $"Найдено рейсов: {rows.Count}");
                foreach (var row in rows)
                {
                    var departureDateRaw = row.FindElement(By.XPath("./div[@class='day-header']/span")).Text.Split(" ")[0];

                    var departureDate   = DateTime.Parse(departureDateRaw);
                    var destinationDate = departureDate;
                    var flights         = row.FindElements(By.XPath("./div[@class='data']//div[contains(@class,'f-row')]"));
                    foreach (var flight in flights)
                    {
                        var flightNo = flight.FindElement(By.XPath(@"./div[@class='flight']")).Text;
                        _logger?.LogInformation($"[Pegas] departure city {departureCity} destinationCity {destinationCity} flightNo {flightNo}");

                        string[] departureAirInfo = flight.FindElement(By.XPath(@"./div[@class='avia']")).Text.Replace("\"", string.Empty).Split("\r\n");
                        var      airCompany       = string.Empty;
                        var      aircraft         = string.Empty;
                        if (departureAirInfo != null && departureAirInfo.Any())
                        {
                            airCompany = departureAirInfo[0].Trim();
                            if (departureAirInfo.Count() > 1)
                            {
                                aircraft = departureAirInfo[1].Trim();
                            }
                        }

                        bool isConnecting = flight.GetAttribute("class").Contains("connector");

                        string[] departureTimePlaceInfo = flight.FindElement(By.XPath(@"./div[@class='departure-item']")).Text.Replace("\"", string.Empty).Split("\r\n");

                        var departureTime    = departureTimePlaceInfo[0].Trim();
                        var departureAirport = departureTimePlaceInfo[1].Trim();

                        string[] returnTimePlaceInfo = flight.FindElement(By.XPath(@"./div[@class='return-item']")).Text.Replace("\"", string.Empty).Split("\r\n");

                        try
                        {
                            // do debug better
                            var plusDayElm = flight.FindElement(By.XPath(@"./span[@class='flight-date-diff']"));
                            if (plusDayElm != null)
                            {
                                var dateDiff = plusDayElm.Text.Contains("+") ?
                                               1 : -1;
                                destinationDate = destinationDate.AddDays(dateDiff);
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.ToString());
                        }

                        var destinationTime    = returnTimePlaceInfo[0].Trim();
                        var destinationAirport = returnTimePlaceInfo[1].Trim();
                        var newFlight          =
                            new Flight
                        {
                            DepartureDate          = departureDate,
                            DepartureTime          = departureTime,
                            DepartureCity          = departureCity,
                            DepartureCityCode      = departureCityCode,
                            DepartureAirportCode   = departureAirport,
                            DestinationDate        = destinationDate,                      //Attention
                            DestinationTime        = destinationTime,
                            DestinationCity        = destinationCity,
                            DestinationAirportCode = destinationAirport,
                            OperatorName           = Name,
                            OperatorId             = 1,
                            FlightNo          = flightNo,
                            FlightCompanyName = airCompany,
                            CollectDate       = DateTime.Now,
                            AircraftKind      = aircraft,
                            IsConnecting      = isConnecting
                        };
                        list.Add(newFlight);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                _logger?.LogError(ex, "[Pegas] ParseFlights");
                throw;
            }
            return(list);
        }