Пример #1
0
        static async Task QueryLoop(Logger logger)
        {
            var scanner = new Scanner(ConfigurationManager.AppSettings["apiKey"]);
            var fromPlace = (await scanner.QueryLocation("Geneva")).First();

            while (true)
            {
                Console.Write("Where to: ");
                var whereTo = Console.ReadLine();
                var toPlace = await GetLocation(whereTo, scanner, logger);
                if (toPlace == null)
                {
                    continue;
                }
                logger.LogOutput("Going to {0}{1}", toPlace.PlaceName, Environment.NewLine);

                Console.Write("Check for next ? weeks: ");
                var howManyWeeks = int.Parse(Console.ReadLine());
                logger.LogInput("Checking for {0} weeks{1}", howManyWeeks, Environment.NewLine);

                var flightResponseSettings = new FlightResponseSettings(
                    sortOrder: SortOrder.Ascending,
                    sortType: SortType.Price,
                    maxStops: 0,
                    outboundDepartureStartTime: new LocalTime(17, 0, 0),
                    outboundDepartureEndTime: new LocalTime(22, 0, 0),
                    inboundDepartureStartTime: new LocalTime(13, 0, 0),
                    inboundDepartureEndTime: new LocalTime(20, 0, 0));

                var today = SystemClock.Instance.Now.InUtc().LocalDateTime.Date;
                var thisFriday = today.Next(IsoDayOfWeek.Friday);

                for (int i = 0; i < howManyWeeks; i++)
                {
                    var outboundDate = thisFriday.PlusWeeks(i);
                    var inboundDate = outboundDate.PlusDays(2);

                    var itineraries = await scanner.QueryFlight(
                        new FlightQuerySettings(
                            new FlightRequestSettings(fromPlace, toPlace, outboundDate, inboundDate, 2),
                            flightResponseSettings));

                    var itinerary = itineraries.FirstOrDefault();
                    if (itinerary == null)
                    {
                        logger.LogOutput("Couldn't find itinerary" + Environment.NewLine);
                    }
                    else
                    {
                        logger.LogOutput(WriteItinerary(itinerary));
                    }

                    logger.LogOutput("----------------------------------------------------{0}", Environment.NewLine);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the FlightQuerySettings with the specified parameters
        /// </summary>
        /// <param name="requestSettings"></param>
        /// <param name="responseSettings"></param>
        public FlightQuerySettings(FlightRequestSettings requestSettings, FlightResponseSettings responseSettings)
        {
            if (requestSettings == null)
            {
                throw new ArgumentNullException(nameof(requestSettings));
            }
            if (responseSettings == null)
            {
                throw new ArgumentNullException(nameof(responseSettings));
            }

            if (!requestSettings.InboundDate.HasValue &&
                (responseSettings.InboundDepartureEndTime.HasValue ||
                 responseSettings.InboundDepartureStartTime.HasValue ||
                 responseSettings.InboundDepartureTime.HasValue))
            {
                throw new ArgumentException("A one-way flight is queried, but response settings assume a return flight");
            }

            FlightRequest  = requestSettings;
            FlightResponse = responseSettings;
        }
Пример #3
0
        private static async Task SearchDetailed()
        {
            //Initialize Scanner
            var scanner = new Scanner(
                ConfigurationManager.AppSettings["apiKey"],
                RetryExecutionStrategy.Default);

            //Query locales
            var locales = await scanner.QueryLocale();
            var currentLocale = locales.FirstOrDefault(locale => locale.Name.StartsWith("English"));

            if (currentLocale == null)
            {
                WriteLine(ErrorColor, "Couldn't find locale, using default instead");
                currentLocale = Locale.Default;
            }

            //Query markets
            var markets = await scanner.QueryMarket(currentLocale);
            var currentMarket = markets.FirstOrDefault(market => market.Name == "Switzerland");

            if (currentMarket == null)
            {
                WriteLine(ErrorColor, "Couldn't find market, using default instead");
                currentMarket = Market.Default;
            }

            //Query currencies
            var currencies = await scanner.QueryCurrency();
            var currentCurrency = currencies.FirstOrDefault(currency => currency.Code == "CHF");

            if (currentCurrency == null)
            {
                WriteLine(ErrorColor, "Couldn't find currency, using default instead");
                currentCurrency = Currency.Default;
            }
            
            //Query location
            const string fromPlaceName = "London";
            var from = (await scanner.QueryLocation(new LocationAutosuggestSettings(fromPlaceName,
                LocationAutosuggestQueryType.Query, currentMarket, currentCurrency, currentLocale))).First();
            if (from == null)
            {
                WriteLine(ErrorColor, "Couldn't find '{0}'", fromPlaceName);
                return;
            }

            //Query destination location
            const string toPlaceName = "New York";
            var to = (await scanner.QueryLocation(new LocationAutosuggestSettings(toPlaceName,
                LocationAutosuggestQueryType.Query, currentMarket, currentCurrency, currentLocale))).FirstOrDefault();
            if (to == null)
            {
                WriteLine(ErrorColor, "Couldn't find '{0}'", toPlaceName);
                return;
            }

            //Setup flight search settings
            var flightResponseSettings = new FlightResponseSettings(
                sortOrder: SortOrder.Ascending,
                sortType: SortType.Price,
                maxStops: 2,
                maxDuration: 14 * 60,
                outboundDepartureStartTime: new LocalTime(08, 0, 0),
                outboundDepartureEndTime: new LocalTime(12, 0, 0),
                inboundDepartureStartTime: new LocalTime(08, 0, 0),
                inboundDepartureEndTime: new LocalTime(18, 30, 0)
                );

            var now = new LocalDate(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            var outboundDate = now.PlusWeeks(1);
            var inboundDate = now.PlusWeeks(2);

            Write("Flights from ");
            Write(ImportantColor, from.PlaceName);
            Write(" to ");
            WriteLine(ImportantColor, to.PlaceName);
            Write(" on ");
            Write(ImportantColor, outboundDate.ToString("d", CultureInfo.InvariantCulture));
            Write(" and back on ");
            WriteLine(ImportantColor, inboundDate.ToString("d", CultureInfo.InvariantCulture));
            
            //Query flights
            var itineraries = await scanner.QueryFlight(
                new FlightQuerySettings(
                    new FlightRequestSettings(from, to, outboundDate, inboundDate, 1,
                        currency: currentCurrency, marketCountry: currentMarket, locale: currentLocale),
                    flightResponseSettings), WriteToDebug());

            itineraries = itineraries
                .Take(5)
                .ToList();

            if (!itineraries.Any())
            {
                WriteLine("No flights");
                return;
            }

            foreach (var itinerary in itineraries)
            {
                WriteItinerary(itinerary, currentCurrency);
            }

            WriteLine("----------------------------------------------");

            //Query bookings (note, this is forbidden by SkyScanner, should only query exact booking details if a user requests them)
            var bookingQueryTasks = itineraries.Select(scanner.QueryBooking);
            var bookingResults = (await Task.WhenAll(bookingQueryTasks))
                .OrderBy(response =>
                    response.BookingOptions
                        .Select(option => option.BookingItems.Sum(item => item.Price))
                        .Min());

            foreach (var response in bookingResults)
            {
                WriteBookingResult(response, currentCurrency);
            }
        }