public async Task <IActionResult> StarResult(StarSearch search) { int threads = 4; if (ModelState.IsValid) { search.SearchDays = Math.Min(search.SearchDays, 14); search.Destination = search.Destination.MyTrim(); search.Origin = search.Origin.MyTrim(); if (search.Origin.MyLength() == 3 && search.Destination.MyLength() == 3) { var dates = System.Linq.Enumerable.Range(0, search.SearchDays).Select(i => search.OutDate.AddDays(i)).ToList(); dates.Shuffle(); var res = new System.Collections.Concurrent.ConcurrentDictionary <DateTime, FlysasLib.SearchResult>(); await Dasync.Collections.ParallelForEachExtensions.ParallelForEachAsync <DateTime>(dates, async date => { if (!res.ContainsKey(date)) //this looks smart, but doesn't realy save a lot of calls... { var q = new SASQuery { OutDate = date, From = search.Origin, To = search.Destination, Adults = search.Pax, Mode = search.SASMode ? SASQuery.SearhMode.POINTS : SASQuery.SearhMode.STAR }; FlysasLib.SearchResult searchResult = await _client.SearchAsync(q); if (searchResult.tabsInfo != null && searchResult.tabsInfo.outboundInfo != null) { foreach (var dayWithNoSeats in searchResult.tabsInfo.outboundInfo.Where(tab => tab.points == 0)) { res.TryAdd(dayWithNoSeats.date, null); } } res.TryAdd(date, searchResult); } }, threads, false ); search.Results = res.Where(r => r.Value?.outboundFlights != null).SelectMany(r => r.Value.outboundFlights).ToList(); if (search.MaxLegs > 0) { search.Results = search.Results.Where(r => r.segments.Count() <= search.MaxLegs).ToList(); } } else { search.Results = new List <FlysasLib.FlightBaseClass>(); } } return(View(nameof(Star), search)); }
public async System.Threading.Tasks.Task Run(string input) { var parser = new Parser(); if (!Command(input)) { foreach (string query in input.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { SASQuery req = null; try { req = parser.Parse(query.Trim()); req.Mode = options.Mode; } catch (ParserException ex) { txtOut.Write("Syntax error:" + ex.Message); } catch { txtOut.Write("Syntax error:"); } if (req != null) { SearchResult result = null; try { result = await client.SearchAsync(req); } catch { txtOut.WriteLine("Error"); } if (result != null) { if (result.errors != null && result.errors.Any()) { txtOut.WriteLine("flysas.com says: " + result.errors.First().errorMessage); } else { var printer = new TablePrinter(txtOut); txtOut.WriteLine("*********Outbound*******"); printer.PrintFlights(result.outboundFlights, options, req.From, req.To); if (req.InDate.HasValue) { txtOut.WriteLine("*********Inbound*******"); printer.PrintFlights(result.inboundFlights, options, req.To, req.From); } } } txtOut.Write(Environment.NewLine + Environment.NewLine); } } } }
public async System.Threading.Tasks.Task InputLoop() { string input = null; var parser = new Parser(); while (!nameof(Commands.Quit).Equals(input, StringComparison.OrdinalIgnoreCase)) { txtOut.WriteLine("Syntax: Origin-Destination outDate [inDate]"); txtOut.Write(">>"); input = txtIn.ReadLine(); if (!Command(input)) { foreach (string query in input.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { SASQuery req = null; try { req = parser.Parse(query); req.Mode = options.Mode; } catch (ParserException ex) { txtOut.Write("Syntax error:" + ex.Message); } catch { txtOut.Write("Syntax error:"); } if (req != null) { SearchResult result = null; try { result = await client.SearchAsync(req); } catch { txtOut.WriteLine("Error"); } if (result != null) { if (result.errors != null && result.errors.Any()) { txtOut.WriteLine("flysas.com says: " + result.errors.First().errorMessage); } else { var printer = new TablePrinter(txtOut); txtOut.WriteLine("*********Outbound*******"); printer.PrintFlights(result.outboundFlights, options); if (req.InDate.HasValue) { txtOut.WriteLine("*********Inbound*******"); printer.PrintFlights(result.inboundFlights, options); } } } txtOut.Write(Environment.NewLine + Environment.NewLine); } } } } }
private async Task <Result> Search(string to, int offsetDays) { SASQuery query = new SASQuery { Mode = "STAR", OutDate = OutStart.AddDays(offsetDays), From = From, To = to, Adt = Pax }; bool hasReturn = InStart.HasValue; if (hasReturn) { query.InDate = InStart.Value.AddDays(offsetDays); } SearchResult result = null; try { result = await client.SearchAsync(query); } catch { Console.Error.WriteLine("Unexpected error in query."); return(new Result { OutBound = new List <FlightBaseClass>(), InBound = new List <FlightBaseClass>() }); } if (result != null) { if (result.errors != null && result.errors.Any() && !HideErrors) { Console.Error.WriteLine("flysas.com says: " + result.errors.First().errorMessage); } { IEnumerable <FlightBaseClass> validOutbound = new List <FlightBaseClass>(); IEnumerable <FlightBaseClass> validInbound = new List <FlightBaseClass>(); if (result.outboundFlights != null) { validOutbound = result.outboundFlights.Where(this.FilterFlight); } if (result.inboundFlights != null) { validInbound = result.inboundFlights.Where(this.FilterFlight); } return(new Result { OutBound = validOutbound, InBound = validInbound }); } } return(new Result { OutBound = new List <FlightBaseClass>(), InBound = new List <FlightBaseClass>() }); }