Пример #1
0
        public static RouteStop Parse(XmlNode node, ListOfIDObjects <Stop> stops)
        {
            Stop stop = stops.GetItemByID(node.Attributes["stopID"].Value);
            List <WeekDayCategory> weekDayCategories    = new List <WeekDayCategory>();
            XmlNodeList            weekDayCategoryNodes = node.SelectNodes("WeekDayCategory");

            foreach (XmlNode weekDayCategoryNode in weekDayCategoryNodes)
            {
                weekDayCategories.Add(WeekDayCategory.Parse(weekDayCategoryNode));
            }
            return(new RouteStop(stop, weekDayCategories));
        }
Пример #2
0
        /*      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      *      */

        private HalfRoute DecodeOldTestament(WebClient client, Database database, KeyValuePair <string, string> routeInfo, string halfRoutePhase, bool forceDownload, bool throwErrors)
        {
            /*try
             * {*/
            workBW.ReportProgress(0, "Downloading " + routeInfo.Key + " (" + halfRoutePhase + ") (old testament)...");
            if (!Directory.Exists(Paths.DownloadsFolder + routeInfo.Key))
            {
                Directory.CreateDirectory(Paths.DownloadsFolder + routeInfo.Key);
            }

            string file = null, lineNameCase = null;

            XmlDocument xml = new XmlDocument();

            try
            {
                lineNameCase = routeInfo.Key.Replace("Linia ", "").ToLowerInvariant();
                file         = GetWebpageText(client,
                                              string.Format(@"http://www.ratbv.ro/afisaje/{0}-{1}/div_list_ro.html", lineNameCase, halfRoutePhase),
                                              string.Format(@"{0}{1}\{2} stationList.html", Paths.DownloadsFolder, routeInfo.Key, halfRoutePhase), forceDownload, throwErrors);
            }
            catch (Exception)
            {
                lineNameCase = routeInfo.Key.Replace("Linia ", "").ToUpperInvariant();
                file         = GetWebpageText(client,
                                              string.Format(@"http://www.ratbv.ro/afisaje/{0}-{1}/div_list_ro.html", lineNameCase, halfRoutePhase),
                                              string.Format(@"{0}{1}\{2} stationList.html", Paths.DownloadsFolder, routeInfo.Key, halfRoutePhase), forceDownload, throwErrors);
            }

            List <Tuple <Stop, string> > stopPages = new List <Tuple <Stop, string> >();
            MatchCollection stopMatches            = Regex.Matches(file, @"<a href=""line.*?"" target=""MainFrame"" onclick=.*?>.*?</a>", RegexOptions.Singleline);

            foreach (Match stopMatch in stopMatches)
            {
                xml.LoadXml(stopMatch.Value);
                string stopName = FixStopName(xml.ChildNodes[0].ChildNodes[0].InnerText.Trim());
                database.AddUniqueStop(stopName);
                Stop stop = database.Stops.GetItemByName(stopName);
                stopPages.Add(new Tuple <Stop, string>(stop, string.Format(@"http://www.ratbv.ro/afisaje/{0}-{1}/{2}", lineNameCase, halfRoutePhase, xml.ChildNodes[0].Attributes["href"].Value)));
            }

            HalfRoute halfRoute = new HalfRoute(routeInfo.Value);

            foreach (Tuple <Stop, string> stopPage in stopPages)
            {
                string stopPageText = GetWebpageText(client,
                                                     stopPage.Item2,
                                                     string.Format(@"{0}Linia {1}\{2} {3}.html", Paths.DownloadsFolder, lineNameCase, halfRoutePhase, stopPage.Item1.ID),
                                                     forceDownload, throwErrors);

                RouteStop routeStop = new RouteStop(stopPage.Item1);

                Match stopTableMatch = Regex.Match(stopPageText, @"<body>.*</body>", RegexOptions.Singleline);
                xml.LoadXml(stopTableMatch.Value.Replace("&nbsp;", ";").Replace("&nbsp", ";").Replace("&#355;", "ț").Replace("&#194", "Â").Replace("&#195", "Ă"));
                XmlNodeList tabel2Nodes = xml.SelectSingleNode("body").SelectSingleNode("div", "id", "header").SelectSingleNode("div", "id", "tabele").ChildNodes;

                foreach (XmlNode tabel2Node in tabel2Nodes)
                {
                    string          weekdayCategoryString = tabel2Node.SelectSingleNode("div", "id", "web_class_title").InnerText.Trim();
                    WeekDayCategory weekdayCategory       = new WeekDayCategory(WeekDayCategory.ParseCategoryType(weekdayCategoryString));

                    int lastHour = -1;
                    foreach (XmlNode rowNode in tabel2Node.ChildNodes)
                    {
                        if (rowNode.InnerText.ToLowerInvariant().Contains("ora") || rowNode.InnerText.ToLowerInvariant().Contains("minutul"))
                        {
                            continue;
                        }
                        switch (rowNode.Attributes["id"].Value)
                        {
                        case "web_class_hours":
                            lastHour = Int32.Parse(rowNode.InnerText.Trim());
                            break;

                        case "web_class_minutes":
                            if (rowNode.InnerText.Contains("NU CIRCUL"))
                            {
                                break;
                            }
                            string[] minutesParts = rowNode.InnerText.Trim().Replace(' ', ';').Replace("\n", ";").Replace("\t", ";").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string minutePart in minutesParts)
                            {
                                StopTime stopTime = new StopTime(new TimeSpan(lastHour, Int32.Parse(minutePart.Replace("*", "")), 0), minutePart.Contains('*'));
                                weekdayCategory.StopTimes.Add(stopTime);
                            }
                            break;
                        }
                    }

                    routeStop.WeekDayCategories.Add(weekdayCategory);
                }

                halfRoute.Add(routeStop);
            }

            return(halfRoute);

            /*}
             * catch (Exception E)
             * {
             *  if (throwErrors)
             *      throw E;
             *  else
             *      Console.WriteLine(E.ToString());
             *  return null;
             * }*/
        }
Пример #3
0
        private HalfRoute DecodeNewTestament(WebClient client, Database database, KeyValuePair <string, string> routeInfo, string halfRoutePhase, bool forceDownload, bool throwErrors)
        {
            workBW.ReportProgress(0, "Downloading and processing (new testament) " + routeInfo.Key + " (" + halfRoutePhase + ")...");
            if (!Directory.Exists(Paths.DownloadsFolder + routeInfo.Key))
            {
                Directory.CreateDirectory(Paths.DownloadsFolder + routeInfo.Key);
            }
            string file = GetWebpageText(client,
                                         string.Format(@"{0}timetable/", routeInfo.Value.Replace("/tour", "/" + halfRoutePhase)),
                                         string.Format(@"{0}{1}\{2} main.html", Paths.DownloadsFolder, routeInfo.Key, halfRoutePhase), forceDownload, throwErrors);

            List <Tuple <string, string, bool> > stopPages = new List <Tuple <string, string, bool> >();
            Match       stopListText = Regex.Match(file, @"<div class=""box butoane-statii"">.*?</div>", RegexOptions.Singleline);
            XmlDocument xml          = new XmlDocument();

            xml.LoadXml(stopListText.Value);
            foreach (XmlNode stopNode in xml.SelectNodes("div/a"))
            {
                stopPages.Add(new Tuple <string, string, bool>(FixStopName(stopNode.InnerText), @"http://www.ratbv.ro" + stopNode.Attributes["href"].Value, stopNode.Attributes["class"] != null && stopNode.Attributes["class"].Value.Equals("active")));
                database.AddUniqueStop(stopPages.Last().Item1, GetNewTestamentStationIDFromURL(stopPages.Last().Item2));
            }

            HalfRoute halfRoute = new HalfRoute("");

            foreach (Tuple <string, string, bool> stopPage in stopPages)
            {
                string stopPageText   = GetWebpageText(client, stopPage.Item2, string.Format(@"{0}{1}\{2} {3}.html", Paths.DownloadsFolder, routeInfo.Key, halfRoutePhase, GetNewTestamentStationIDFromURL(stopPage.Item2)), forceDownload, throwErrors);
                Match  stopTableMatch = Regex.Match(stopPageText, @"<div class=""box tabel-statii"">.*?</div>", RegexOptions.Singleline);

                xml.LoadXml(stopTableMatch.Value.Replace("&nbsp;", ";").Replace("&nbsp", ";"));
                RouteStop routeStop = new RouteStop(database.Stops.GetItemByID(GetNewTestamentStationIDFromURL(stopPage.Item2)));
                foreach (XmlNode wdcNode in xml.SelectNodes("div/table/thead/tr/td"))
                {
                    if (!wdcNode.InnerText.Trim().ToUpperInvariant().Equals("ORA"))
                    {
                        routeStop.WeekDayCategories.Add(new WeekDayCategory(WeekDayCategory.ParseCategoryType(wdcNode.InnerText.Trim())));
                    }
                }

                foreach (XmlNode hourTrNode in xml.SelectNodes("div/table/tr"))
                {
                    XmlNodeList hourTrColumnTds = hourTrNode.SelectNodes("td");
                    if (hourTrColumnTds.Count != routeStop.WeekDayCategories.Count + 1)
                    {
                        continue;
                    }
                    int hour = Int32.Parse(hourTrColumnTds[0].InnerText);
                    for (int iWDC = 0; iWDC < routeStop.WeekDayCategories.Count; iWDC++)
                    {
                        string minutes = hourTrColumnTds[iWDC + 1].InnerText.Replace("\n", "").Replace(" ", "");
                        foreach (string minute in minutes.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            routeStop.WeekDayCategories[iWDC].StopTimes.Add(new StopTime(new TimeSpan(hour, Int32.Parse(minute.Replace("*", "")), 0), minute.Contains('*')));
                        }
                    }
                }

                halfRoute.Add(routeStop);
            }

            if (halfRoute.Count > 0)
            {
                halfRoute.Name = halfRoute[0].Stop.Name + " - " + halfRoute[halfRoute.Count - 1].Stop.Name;
            }
            return(halfRoute);
        }