static async Task RunAsync() { Trains trains = await GetStations(GlobalVar.FROM, GlobalVar.TO, GlobalVar.DATE, GlobalVar.TIME); foreach (var train in trains.GetTrains()) { //var tasksPassages = new List<Task<bool>>(); //for (var i = 0; i < train.Stations.Count - 1; i++) //{ // tasksPassages.Add(ContigentCheckPassage(train.Stations[i].Name, train.Stations[i + 1].Name, // GlobalVar.DATE, train.Stations[i].DepartureTime, trains.JSFViewState, train.ID)); //} //var listPassages = await Task.WhenAll(tasksPassages); //for (var i = 0; i < train.Stations.Count - 1; i++) //{ // train.AddPassage(new Passage(train.Stations[i].Name, train.Stations[i + 1].Name, listPassages[i])); //} for (var i = 0; i < train.Stations.Count - 1; i++) { var isFree = await ContigentCheckPassage(train.Stations[i].Name, train.Stations[i + 1].Name, GlobalVar.DATE, train.Stations[i].DepartureTime, trains.JSFViewState, train.ID); train.AddPassage(new Passage(train.Stations[i].Name, train.Stations[i + 1].Name, isFree)); } } foreach (var train in trains.GetTrains()) { List <Passage> passages = new List <Passage>(); int i = 0; while (i < train.Passages.Count) { int j = i + 1; while ((train.Passages[j].IsFree == train.Passages[i].IsFree) && (j < train.Passages.Count - 1)) { j++; } Passage passage = new Passage(train.Passages[i].From, train.Passages[j].To, train.Passages[i].IsFree); i = j + 1; passages.Add(passage); } Console.WriteLine("Train {0} has following passages:", train.Name); foreach (var passage in passages) { Console.WriteLine("Passage from {0} to {1} is {2}", passage.From, passage.To, (passage.IsFree ? "free" : "paid")); } } Console.WriteLine("END"); }
static async Task <Trains> GetStations(string from, string to, string date, string time) { var response = await GetZSSKInfo(from, to, date, time); var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(response); //trains. var JSFViewState = htmlDoc.DocumentNode .SelectSingleNode("//input[@name='javax.faces.ViewState']") .Attributes["value"].Value; Trains trains = new Trains(JSFViewState); var trainsNodes = htmlDoc.DocumentNode .SelectNodes("//tr[@class='tmp-item-line ']/td[@class='tmp-valign-top' and @colspan='3']"); if (trainsNodes == null) { throw new NullReferenceException("No trains were fetched."); } string patternForID = @"searchForm:inetConnection.*?:.*?:.*?'"; // getting ready for scraping stations var nodesOfStations = htmlDoc.DocumentNode.SelectNodes("//*[@id='r0_train_R615']/table"); // loading all the tables where stations are preserved for each train HtmlNode stationNode = null; for (int i = 0; i < nodesOfStations.Count; i++) { stationNode = nodesOfStations[i]; if (stationNode.ParentNode.ParentNode.ParentNode.Attributes["class"].Value.Contains("ic")) { nodesOfStations.Remove(stationNode); } } // going to scrape the whole structure for (var k = 0; k < trainsNodes.Count; k++) { var node = trainsNodes[k]; Train train = new Train(); train.Name = node.InnerText; // get the train name train.Name = Regex.Replace(train.Name, @"\s+", " "); // exclude spaces from train name if (!Regex.IsMatch(train.Name, @"R .*")) // Checks if train is a fast train { continue; } HtmlNodeCollection trainNodes = null; try { trainNodes = node.ParentNode.SelectSingleNode("./td[3]/div").ChildNodes; // Getting all possible *a* href links (Like Listok a miestenka, Miestenka, Listok) } catch { if (GlobalVar.LOGS) { Console.WriteLine("In train {0} href link for buying tickets couldn't be found", train.Name); } continue; } HtmlNode trainNode = null; foreach (var nodeTrain in trainNodes) // getting the last *a* element in train nodes (Get the attribute value for "Listok") { if (nodeTrain.Name == "a" && nodeTrain.InnerText == "Lístok") { trainNode = nodeTrain; } } if (trainNode == null) // it means there was no button "Listok" { continue; } train.ID = Regex.Match(trainNode.Attributes["onclick"].Value, patternForID).Value; train.ID = train.ID.Remove(train.ID.Length - 1); // Scraping stations from now on var nodeHelp = nodesOfStations[k]; // same index as train If train is not a fast train it will not get here (because it skipped earlier) var stationNodes = nodeHelp.SelectNodes("./tbody"); // this can be minimized for (int i = 0; i < stationNodes.Count; i = i + 3) { string departureTime = stationNodes[i].SelectSingleNode("./tr[2]/td[4]/strong").InnerText; // scraping the first station string name = stationNodes[i].SelectSingleNode("./tr[2]/td[2]").InnerText; departureTime = Regex.Replace(departureTime, @"\s+", ""); Station station = new Station(name, departureTime); train.AddStation(station); // end of scraping the first station var otherStations = stationNodes[i + 1].SelectNodes("./tr"); // scraping stations between the first and the last stations if (otherStations != null) { for (int j = 0; j < otherStations.Count; j++) { name = otherStations[j].SelectSingleNode("./td[2]").InnerText; departureTime = otherStations[j].SelectSingleNode("./td[4]/strong[2]").InnerText; departureTime = Regex.Replace(departureTime, @"\s+", ""); station = new Station(name, departureTime); train.AddStation(station); } // end of scraping stations between the first and the last stations } if (i + 3 == stationNodes.Count) // scraping the last station { name = stationNodes[i + 2].SelectSingleNode("./tr[1]/td[2]").InnerText; departureTime = stationNodes[i + 2].SelectSingleNode("./tr[1]/td[4]/strong").InnerText; departureTime = Regex.Replace(departureTime, @"\s+", ""); station = new Station(name, departureTime); train.AddStation(station); } // end of scraping the last station } trains.AddTrain(train); // adding final train to list of trains } return(trains); }