Ejemplo n.º 1
0
        public static WeekDayCategory Parse(XmlNode node)
        {
            WeekDayCategoryType type      = (WeekDayCategoryType)Enum.Parse(typeof(WeekDayCategoryType), node.Attributes["type"].Value);
            List <StopTime>     stopTimes = new List <StopTime>();

            string[] stopTimeStrings = node.Attributes["stopTimes"].Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string stopTimeString in stopTimeStrings)
            {
                stopTimes.Add(StopTime.Parse(stopTimeString));
            }
            return(new WeekDayCategory(type, stopTimes));
        }
Ejemplo n.º 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;
             * }*/
        }