Exemple #1
0
        private TimeTable parse(IDocument document, string id, LimitedTimeOption limitedTimeOption = null)
        {
            IEnumerable <IElement> destinations = document.QuerySelectorAll(defaultDestinationQuery);
            string defaultDestination           = destinations != null?string.Join(" または ", destinations.Select(x => x.InnerHtml.Trim())) : null;

            IHtmlCollection <IElement> htmlCollection = document.QuerySelectorAll(tableQuery);
            var hourTable = new HourTable
            {
                IndexedHours = new List <IndexedHour>()
            };
            var weekdayTable = new DayTable
            {
                Buses = new List <Bus>()
            };
            var saturdayTable = new DayTable
            {
                Buses = new List <Bus>()
            };
            var sundayTable = new DayTable
            {
                Buses = new List <Bus>()
            };

            if (htmlCollection.Length < 1)
            {
                throw new TbusParserException("not found diagram-table");
            }
            if (htmlCollection[0] == null)
            {
                throw new TbusParserException("not found table");
            }

            var specialDays = new List <(DateTime day, DayTable dayTable)>();

            foreach (var element in htmlCollection[0].QuerySelectorAll(specialdayDateQuery))
            {
                string specialDayText = element?.InnerHtml
                                        ?? throw new TbusParserException("not found special day");
                if (tryParseDate(specialDayText, out DateTime specialDay))
                {
                    specialDays.Add((specialDay, new DayTable {
                        Buses = new List <Bus>()
                    }));
                }
                else
                {
                    throw new TbusParserException("cannot parse special day");
                }
            }

            int index = 0;

            foreach (var element in htmlCollection[0].QuerySelectorAll(hourTableQuery))
            {
                string hourText = element.QuerySelector(hourQuery)?.InnerHtml
                                  ?? throw new TbusParserException("not found hour");
                if (tryParseInt(hourText, out int hour))
                {
                    hourTable.IndexedHours.Add(new IndexedHour(index, hour));
                }
                else
                {
                    throw new TbusParserException("cannot parse hour");
                }

                parseDayTableInHour(element.QuerySelector(weekdayQuery), hour, weekdayTable, defaultDestination);
                parseDayTableInHour(element.QuerySelector(saturdayQuery), hour, saturdayTable, defaultDestination);
                parseDayTableInHour(element.QuerySelector(sundayQuery), hour, sundayTable, defaultDestination);
                int specialDayIndex = 0;
                foreach (var e in element.QuerySelectorAll(specialdayQuery))
                {
                    if (specialDays.Count <= specialDayIndex)
                    {
                        throw new TbusParserException("out range special days");
                    }
                    parseDayTableInHour(e, hour, specialDays[specialDayIndex].dayTable, defaultDestination);
                    specialDayIndex++;
                }

                index++;
            }

            var timeTable = new TimeTable()
            {
                Id                = id,
                CreatedAt         = DateTime.Now,
                LimitedTimeOption = limitedTimeOption,
                HourTable         = hourTable,
                WeekdayTable      = weekdayTable,
                SaturdayTable     = saturdayTable,
                SundayTable       = sundayTable,
                SpecialDays       = specialDays.ToDictionary(x => x.day, x => x.dayTable)
            };

            return(timeTable);
        }