public static Dictionary <string, Dictionary <string, string> > GetAll(PlanProvider provider) { var document = provider.GetList(); var classSelect = document.All.Where(e => e.TagName.ToLower() == "select") .Where(s => s.Attributes.Where(a => a.Name == "name" && a.Value == "oddzialy").Any()).First(); var teacherSelect = document.All.Where(e => e.TagName.ToLower() == "select").Where(s => s.Attributes.Where(a => a.Name == "name" && a.Value == "nauczyciele").Any()).First(); var roomSelect = document.All.Where(e => e.TagName.ToLower() == "select") .Where(s => s.Attributes.Where(a => a.Name == "name" && a.Value == "sale").Any()).First(); return(new Dictionary <string, Dictionary <string, string> >() { { "classes", classSelect.Children.Where(o => o.Attributes.Any()) .ToDictionary(o => o.InnerHtml, o => o.Attributes.First().Value) }, { "teachers", teacherSelect.Children.Where(o => o.Attributes.Any()) .ToDictionary(o => o.InnerHtml, o => o.Attributes.First().Value) }, { "rooms", roomSelect.Children.Where(o => o.Attributes.Any()) .ToDictionary(o => o.InnerHtml, o => o.Attributes.First().Value) } }); }
/// <summary> /// Experimental function, gets the whole lesson plan and parses, it may take a while. /// </summary> public static LessonPlan GetEntirePlan(PlanProvider provider) { var ids = ListParser.GetAll(provider); var classes = ids["classes"]; var teachers = ids["teachers"]; var rooms = ids["rooms"]; var lessons = new List <Lesson>(); foreach (var classId in classes) { lessons.AddRange(LessonParser.GetLessonsForClass(provider, classId.Value)); } foreach (var teacherId in teachers) { lessons.AddRange(LessonParser.GetLessonsForTeacher(provider, teacherId.Value)); } foreach (var roomId in rooms) { lessons.AddRange(LessonParser.GetLessonsForClass(provider, roomId.Value)); } return(new LessonPlan() { Classes = classes, Teachers = teachers, Rooms = rooms, Lessons = lessons }); }
public static Dictionary <string, string> GetRooms(PlanProvider provider) { var document = provider.GetList(); var roomSelect = document.All.Where(e => e.TagName.ToLower() == "select") .Where(s => s.Attributes.Where(a => a.Name == "name" && a.Value == "sale").Any()).First(); return(roomSelect.Children.Where(o => o.Attributes.Any()) .ToDictionary(o => o.InnerHtml, o => o.Attributes.First().Value)); }
public static string GetRoom(PlanProvider provider, string roomName) { var classes = GetRooms(provider); if (classes.ContainsKey(roomName)) { return(classes[roomName]); } else { throw new InvalidNameException("Room with this name doesn't exist."); } }
public static string GetTeacher(PlanProvider provider, string teacherName) { var classes = GetTeachers(provider); if (classes.ContainsKey(teacherName)) { return(classes[teacherName]); } else { throw new InvalidNameException("Teacher with this name doesn't exist."); } }
public static string GetClass(PlanProvider provider, string className) { var classes = GetClasses(provider); if (classes.ContainsKey(className)) { return(classes[className]); } else { throw new InvalidNameException("Class with this name doesn't exist."); } }
public static List <Lesson> GetLessonsForTeacher(PlanProvider provider, string teacherId) { var document = provider.GetTeacher(teacherId); var lessonTable = document.All.First(e => e.ClassName == "tabela").Children.First(); var rows = lessonTable.Children.Where(e => e.TagName.ToLower() == "tr") .Where(tr => tr.Children.Any(e => e.Attributes.Any())); var allLessons = new List <Lesson>(); foreach (var row in rows) { var number = Int32.Parse(row.Children.Where(r => r.ClassName == "nr").Select(r => r.InnerHtml).First()); var textPeriod = row.Children.Where(r => r.ClassName == "g").Select(r => r.InnerHtml).First() .Split('-'); var period = new Period() { Start = TimeSpan.Parse(textPeriod[0]).ToString(), End = TimeSpan.Parse(textPeriod[1]).ToString() }; var lessons = row.Children.Where(r => r.ClassName == "l").ToList(); foreach (var lesson in lessons) { //? I using `QuerySelectorAll` instead of Linq `Where` because sometimes the items are packed into a blank parent `span` element. //? I have no influence on this, it is the Optivum plan generator's result. var dayOfWeek = lessons.IndexOf(lesson) + 1; var groups = lesson.QuerySelectorAll("*").ToList(); groups.Add(lesson); //? Sometimes the lessons doesn't have a separate div. groups = groups.Where(e => e.Children.Any(c => c.ClassName == "p")).ToList(); foreach (var group in groups) { var names = group.Children.Where(e => e.ClassName == "p").Select(e => e.InnerHtml).ToList(); var classes = group.Children.Where(e => e.ClassName == "o").Select(e => e.Attributes[0].Value) .ToList(); var rooms = group.Children.Where(e => e.ClassName == "s").Select(e => e.Attributes[0].Value) .ToList(); //? Protection against lessons without property. In my school, lessons without set teacher are popular. string name = null; string classId = null; string roomId = null; if (names.Any()) { name = names.First(); } if (classes.Any()) { classId = new string(classes.First().Where(Char.IsDigit).ToArray()); } if (rooms.Any()) { roomId = new string(rooms.First().Where(Char.IsDigit).ToArray()); } allLessons.Add(new Lesson() { Number = number, Period = period, DayOfWeek = dayOfWeek, Name = name, ClassId = classId, TeacherId = teacherId, RoomId = roomId }); } } } return(allLessons); }