static List<StationRoute> CreateRoutes(List<Station> tempStations, string line) { var routes = new List<StationRoute>(); foreach (var station in tempStations) { var route = new StationRoute { FromStation = station, ToStation = tempStations.Last(), Line = line }; for (int i = tempStations.IndexOf(station) + 1; i < tempStations.Count - 1; i++) { route.ViaStation.Add(tempStations[i]); } routes.Add(route); } routes.Remove(routes.Last()); return routes; }
string ExtractHarmonogramToString(StationRoute route) { if (route == null) return ""; var builder = new StringBuilder(); try { foreach (var board in route.RouteHarmonogram.Boards) { foreach (var item in board.HarmonogramItems) { foreach (var minute in item.Minutes) { var line = string.Format("{0}-{1}-{2}-{3}-{4}:{5}:00", route.Line, board.Name, route.FromStation.Name.Replace("-", " "), route.ToStation.Name.Replace("-", " "), item.Hour, minute[0].ToString() + minute[1].ToString()); builder.AppendLine(line); } } } } catch { } var result = builder.ToString(); int lastLineIndex = result.LastIndexOf(Environment.NewLine); if (lastLineIndex > 0) { result = result.Remove(lastLineIndex); } return result; }
static void WriteFile(StationRoute route, string harmonogram) { if (route == null || string.IsNullOrEmpty(harmonogram)) return; if (!Directory.Exists("Harmonograms")) Directory.CreateDirectory("Harmonograms"); File.WriteAllText(Path.Combine("Harmonograms", string.Format("{0}-{1}-{2}.TXT", route.Line, route.FromStation.Name.Replace("-", " "), route.ToStation.Name.Replace("-", " "))), harmonogram); }
public static Harmonogram GetHarmonogram(StationRoute route) { var harm = new Harmonogram(); var htmlDoc = GetHarmonogramHtmlDocument(route); var htmlTables = GetHarnogramHtmlTables(htmlDoc); var htmlBoards = GetHarmonogramHtmlBoards(htmlTables); if (htmlBoards != null && htmlBoards.Count > 0) { foreach (var htmlBoard in htmlBoards) { var harmonogramBoard = new HarmonogramBoard(); var departures = GetDepartures(htmlBoard); foreach (var row in departures) { var allLines = row.Descendants("td").ToList(); if (allLines != null && allLines.Count > 0) { var harmonogramItem = new HarmonogramTimeItem { Hour = allLines.FirstOrDefault(d => d.Attributes != null).InnerText }; allLines.Where(d => (d.Attributes.Count == 0 || (d.Attributes["class"] != null && String.Equals(d.Attributes["class"].Value, "nizkopodlazne", StringComparison.InvariantCultureIgnoreCase)))) .Select(x => x.InnerText) .ToList() .ForEach(it => harmonogramItem.Minutes.Add(it)); harmonogramBoard.HarmonogramItems.Add(harmonogramItem); } } harmonogramBoard.Name = GetHarmonogramBoardName(htmlBoards.IndexOf(htmlBoard), htmlTables); harm.Boards.Add(harmonogramBoard); } } return harm; }
static HtmlDocument GetHarmonogramHtmlDocument(StationRoute route) { var doc = new HtmlDocument { OptionDefaultStreamEncoding = Encoding.UTF8 }; doc.LoadHtml(HtmlPageDownloader.DownloadHtmlPage(string.Format(HarmonogramBaseUrl, route.FromStation.Link))); return doc; }
public List<string> ExtractBoardName(StationRoute route) { var result = new List<string>(); try { foreach (var board in route.RouteHarmonogram.Boards) { foreach (var item in board.HarmonogramItems) { result.Add(board.Name); } } } catch { } return result; }