public override IEnumerable <UTCourse> FetchItems() { List <UTCourse> results = new List <UTCourse>(); int lineCount = 0; // Parse the csv file by line foreach (string line in this.Content.TrimStart('\n').Split('\n')) { lineCount++; if (lineCount == 1) { continue; } int columnCount = 0; UTCourse course = new UTCourse() { Campus = "UTSC" }; UTCourseSection courseSection = new UTCourseSection(); CourseSectionTimeSpan span = new CourseSectionTimeSpan(); MatchCollection lineMatches = CsvRegex.Matches(line); foreach (Match column in lineMatches) { string content = column.Value.Trim('\"'); switch (columnCount) { // Course code case (0): { Match codeMatch = CodeRegex.Match(content); course.Code = codeMatch.Groups["code"].Value; course.SemesterPrefix = codeMatch.Groups["prefix"].Value; course.Semester = codeMatch.Groups["semester"].Value; break; } // Section name case (1): { courseSection.Name = content; break; } // Day case (3): { span.Day = UTMTimeParser.ParseDay(content); break; } // Start time case (4): { byte start; UTCourseSectionTimeSpan.TryParseTimeSpanInt(content, out start); span.Start = start; break; } // End time case (5): { byte end; UTCourseSectionTimeSpan.TryParseTimeSpanInt(content, out end); span.End = end; break; } // Location case (6): { courseSection.Location = content.Replace(" ", String.Empty); break; } // Instructor case (7): { courseSection.Instructor = content; break; } default: { break; } } columnCount++; } if (String.IsNullOrEmpty(course.Abbr)) { continue; } // Check if it is the same course if (results.Count > 0) { UTCourse lastCourse = results.Last(); if (lastCourse.Abbr == course.Abbr) { UTCourseSection lastSection = lastCourse.Sections.Last() as UTCourseSection; // Check if it is the same section if (lastSection.Name == courseSection.Name) { // Add the meet time List <CourseSectionTimeSpan> meets = lastSection.ParsedTime.MeetTimes.ToList(); meets.Add(span); lastSection.ParsedTime = new CourseSectionTime(meets); String.Join(" ", lastSection.Location, courseSection.Location); continue; } else { // Add the course section List <CourseSectionTimeSpan> meets = new List <CourseSectionTimeSpan>(); meets.Add(span); courseSection.ParsedTime = new CourseSectionTime(meets); lastCourse.Sections.Add(courseSection); continue; } } } // Add the course List <CourseSectionTimeSpan> meets2 = new List <CourseSectionTimeSpan>(); meets2.Add(span); courseSection.ParsedTime = new CourseSectionTime(meets2); course.Sections.Add(courseSection); results.Add(course); } return(results); }
public override IEnumerable <UTCourse> FetchItems() { List <UTCourse> results = new List <UTCourse>(); this.Content = this.Content.Replace("\n", String.Empty).Replace("\r", String.Empty); MatchCollection courseMatches = TableRegex.Matches(this.Content); foreach (Match courseMatch in courseMatches) { UTCourse course = new UTCourse() { Department = this.Department, Code = courseMatch.Groups["code"].Value, SemesterPrefix = courseMatch.Groups["prefix"].Value, Semester = courseMatch.Groups["semester"].Value, Sections = new List <CourseSection>(), Campus = "UTM" }; Console.Write("UTM Course: " + course.Abbr); // Process name string name = HttpUtility.HtmlDecode(courseMatch.Groups["name"].Value).Replace("<esp233>", "é"); name = AngleRegex.Replace(name, String.Empty); if (name.Contains("SSc")) { course.AddCategory("SSc"); } if (name.Contains("SCI")) { course.AddCategory("SCI"); } if (name.Contains("HUM")) { course.AddCategory("HUM"); } course.Name = CircleRegex.Replace(name, String.Empty).Trim(' '); // Parse detail string[] details = AngleRegex.Replace(courseMatch.Groups["detail"].Value.Replace("<br>", "|"), String.Empty).Split('|'); course.Description = HttpUtility.HtmlDecode(details[0]).Replace("\t", String.Empty).Replace("Course Details", String.Empty).Trim(' '); foreach (string detail in details) { if (detail.StartsWith("Exclusion")) { course.Exclusions = detail.Substring("Exclusion: ".Length).Replace("\t", String.Empty); } else if (detail.StartsWith("Prerequisite")) { course.Prerequisites = detail.Substring("Prerequisites: ".Length).Replace("\t", String.Empty); } else if (detail.StartsWith("Corequisite")) { course.Corequisites = detail.Substring("Corequisites: ".Length).Replace("\t", String.Empty); } } // Process sections and meettime string courseContent = courseMatch.Groups["content"].Value; string[] sectionContents = courseContent.Replace("</tr>", "|").Split('|'); int nameColumn = 0, instructorColumn = 0, dayColumn = 0, startColumn = 0, endColumn = 0, locationColumn = 0; int count = 0; // Assign column number foreach (string column in AngleRegex.Replace(sectionContents[0].Replace("</th>", "|"), String.Empty).Split('|')) { string columnTrim = column.Trim(); switch (columnTrim) { case ("Section"): { nameColumn = count; break; } case ("Instructor"): { instructorColumn = count; break; } case ("Day"): { dayColumn = count; break; } case ("Start"): { startColumn = count; break; } case ("End"): { endColumn = count; break; } case ("Room"): { locationColumn = count; break; } } count++; } // Parse section foreach (string sectionContent in sectionContents) { UTCourseSection section = new UTCourseSection(); string[] meetTimeContent = sectionContent.Replace("</td>", "|").Split('|'); if (meetTimeContent.Length < 4) { continue; } string[] meetTimeTmp = meetTimeContent[dayColumn].Replace("\t", String.Empty).Replace(" ", String.Empty).Replace("<br>", "|").Split('|'); int meetTimeCount = 0; foreach (string meet in meetTimeTmp) { if (meet.Length > 0) { meetTimeCount += 1; } } // Pre-initialize the meet times CourseSectionTime time = new CourseSectionTime(); List <CourseSectionTimeSpan> meets = new List <CourseSectionTimeSpan>(); for (int i = 0; i < meetTimeCount; i++) { meets.Add(new CourseSectionTimeSpan()); } for (int i = 0; i < meetTimeContent.Length; i++) { // Section name if (i == nameColumn) { section.Name = AngleRegex.Replace(meetTimeContent[i], String.Empty).Trim(); } // Instructor else if (i == instructorColumn) { section.Instructor = AngleRegex.Replace(meetTimeContent[i].Replace("<br>", " "), String.Empty).Trim(); } // Day else if (i == dayColumn) { string[] days = AngleRegex.Replace(meetTimeContent[i].Replace("\t", String.Empty).Replace(" ", String.Empty).Replace("<br>", "|"), String.Empty).Split('|'); for (int j = 0; j < days.Length; j++) { if (days[j].Length == 0) { continue; } CourseSectionTimeSpan span = meets[j]; span.Day = UTMTimeParser.ParseDay(days[j]); meets[j] = span; } } // Start time else if (i == startColumn) { int meetCount = 0; foreach (string rawTime in AngleRegex.Replace(meetTimeContent[i].Replace("\t", String.Empty).Replace(" ", String.Empty).Replace("<br>", "|"), String.Empty).Split('|')) { if (rawTime.Length == 0) { continue; } byte startTime; UTCourseSectionTimeSpan.TryParseTimeSpanInt(rawTime, out startTime); CourseSectionTimeSpan span = meets[meetCount]; span.Start = startTime; meets[meetCount] = span; meetCount++; } } // End time else if (i == endColumn) { int meetCount = 0; foreach (string rawTime in AngleRegex.Replace(meetTimeContent[i].Replace("\t", String.Empty).Replace(" ", String.Empty).Replace("<br>", "|"), String.Empty).Split('|')) { if (rawTime.Length == 0) { continue; } byte endTime; UTCourseSectionTimeSpan.TryParseTimeSpanInt(rawTime, out endTime); CourseSectionTimeSpan span = meets[meetCount]; span.End = endTime; meets[meetCount] = span; meetCount++; } } // Location else if (i == locationColumn) { section.Location = AngleRegex.Replace(meetTimeContent[i].Replace("\t", String.Empty).Replace(" ", String.Empty).Replace("<br>", "|"), String.Empty).Replace("|", " ").Trim('\t'); } } time.MeetTimes = meets; section.ParsedTime = time; course.Sections.Add(section); Console.Write(" " + section.Name); } Console.WriteLine(); results.Add(course); } return(results); }
public override IEnumerable <UTCourse> FetchItems() { List <UTCourse> results = new List <UTCourse>(); if (this.Content == null) { return(results); } this.Content = this.Content.Replace("\n", String.Empty).Replace("\r", String.Empty); MatchCollection matches = CourseRegex.Matches(this.Content); // Used to accumulating course meet times string courseName = null; foreach (Match match in matches) { string line = match.Value; if (CodeRegex.Match(line).Success) { courseName = CodeRegex.Match(line).Value; continue; } else { if (courseName == null) { continue; } line = line.Replace("</td>", "|"); line = AngleRegex.Replace(line, String.Empty); line = CircleRegex.Replace(line, String.Empty); string[] properties = line.Split('|'); if (line.ToLower().Contains("cancel")) { continue; } if (properties.Length < 5) { continue; } UTCourse course = new UTCourse() { Code = CodeRegex.Match(courseName).Groups["code"].Value, SemesterPrefix = CodeRegex.Match(courseName).Groups["prefix"].Value, Semester = CodeRegex.Match(courseName).Groups["semester"].Value, Sections = new List <CourseSection>(), Name = properties[0].Trim(' ') + ": " + HttpUtility.HtmlDecode(properties[1].Trim(' ')), Campus = "UTSG", Faculty = "Artsci" }; UTCourseSection section = new UTCourseSection() { Name = properties[0].Trim(' '), Time = HttpUtility.HtmlDecode(properties[3]).Replace(" ", String.Empty).Trim(' ').Trim(' '), Location = HttpUtility.HtmlDecode(properties[4]).Replace(" ", String.Empty).Trim(' '), Instructor = HttpUtility.HtmlDecode(properties[5]).Replace(" ", String.Empty).Trim(' ') }; // Accumulate to last course if (course.Name == " : ") { section.Name = results.Last().Sections.Last().Name; results.Last().Sections.Add(section); } else { course.Sections.Add(section); results.Add(course); Console.WriteLine("Course: " + course.Abbr); } } } return(results); }
public override IEnumerable <UTCourse> FetchItems() { dynamic allCourseData = JsonConvert.DeserializeObject(this.Content); List <UTCourse> allCourses = new List <UTCourse>(); foreach (dynamic coursepair in allCourseData) //Parallel.ForEach<dynamic>((IEnumerable<dynamic>)allCourseData.categorydata, // new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, // delegate(dynamic category) { //dynamic course = pair.Value; dynamic course = coursepair.Value; //foreach (dynamic course in category.course) //{ UTCourse courseObj = new UTCourse(); string code = course.code; courseObj.Code = code.Substring(0, 6); string secstring = course.section; if (code.Length > 8) { courseObj.Semester = code.Substring(9, 1); } else { courseObj.Semester = secstring.Substring(0, 1); } courseObj.SemesterPrefix = code.Substring(6, 2); courseObj.Name = course.courseTitle; courseObj.Campus = "UTSG"; Console.Out.Write("Course: " + courseObj.Abbr); if (course.meetings != null) { //foreach (dynamic activity in course.activities) foreach (dynamic meeting in course.meetings) { dynamic activity = meeting.Value; UTCourseSection section = new UTCourseSection(); //section.Name = activity.name; section.Name = (string)activity.teachingMethod + (string)activity.sectionNumber; //section.Instructor = activity.instructor; List <string> instructors = new List <string>(); //Dictionary<string, string> instructorTable = new Dictionary<string, string>(); foreach (dynamic instructorpair in activity.instructors) { instructors.Add((string)instructorpair.Value.firstName + " " + (string)instructorpair.Value.lastName); } section.Instructor = string.Join(", ", instructors); Console.Out.Write(" " + section.Name); List <CourseSectionTimeSpan> meetTimes = new List <CourseSectionTimeSpan>(); bool hasTime = activity.schedule != null; if (hasTime) { foreach (dynamic timepair in activity.schedule) { dynamic time = timepair.Value; string day = time.meetingDay; if (day != null) { string timeFrom = time.meetingStartTime; string timeTo = time.meetingEndTime; CourseSectionTimeSpan timespan; switch (day) { case "MO": { timespan.Day = DayOfWeek.Monday; break; } case "TU": { timespan.Day = DayOfWeek.Tuesday; break; } case "WE": { timespan.Day = DayOfWeek.Wednesday; break; } case "TH": { timespan.Day = DayOfWeek.Thursday; break; } case "FR": { timespan.Day = DayOfWeek.Friday; break; } default: { throw new Exception("Unknown day: " + day); } } //timespan.Day = ((DayOfWeek)Enum.Parse(typeof(DayOfWeek), day)); timespan.Start = (byte)(Convert.ToInt32(timeFrom.Substring(0, 2)) * 4 + Convert.ToInt32(timeFrom.Substring(3, 2)) / 15); timespan.End = (byte)(Convert.ToInt32(timeTo.Substring(0, 2)) * 4 + Convert.ToInt32(timeTo.Substring(3, 2)) / 15); meetTimes.Add(timespan); } } section.ParsedTime = new CourseSectionTime(meetTimes); } else { section.Time = "TBA"; } Console.Out.WriteLine(section.ParsedTime.ToString()); courseObj.Sections.Add(section); } //} Console.Out.WriteLine(); allCourses.Add(courseObj); } //}); } Console.Out.WriteLine("Total number of courses: {0}", allCourses.Count); return(allCourses); }
public bool TryAddCourse(uint courseId, string courseString, string sectionCode, iCalendar calendar) { IIndexableCollection <Course> courses = (IIndexableCollection <Course>)HttpContext.Current.Application["Courses"]; Course course; if (courses.TryGetItem(courseId, out course)) { if (!courseString.StartsWith(course.Abbr)) { return(false); } foreach (CourseSection section in course.Sections) { if (section.Name == sectionCode) { UTCourseSection utSection = section as UTCourseSection; UTCourse utCourse = course as UTCourse; if (utSection != null && utCourse != null) { int order = 0; foreach (CourseSectionTimeSpan timeSpan in utSection.ParsedTime.MeetTimes) { if (utCourse.SemesterDetail == "Fall" || utCourse.SemesterDetail == "Year") { Event courseEvent = calendar.Create <Event>(); if (utCourse.Faculty == "Engineering") { courseEvent.Start = new iCalDateTime(this.ConvertDateTime(new DateTime(2020, 9, 1), timeSpan.Day, timeSpan.Start), "America/Toronto"); courseEvent.End = new iCalDateTime(this.ConvertDateTime(new DateTime(2020, 9, 1), timeSpan.Day, timeSpan.End), "America/Toronto"); courseEvent.RecurrenceRules.Add(new RecurrencePattern(FrequencyType.Weekly) { //Count = 13 Until = new DateTime(2020, 12, 15) }); } else { courseEvent.Start = new iCalDateTime(this.ConvertDateTime(new DateTime(2018, 9, 6), timeSpan.Day, timeSpan.Start), "America/Toronto"); courseEvent.End = new iCalDateTime(this.ConvertDateTime(new DateTime(2018, 9, 6), timeSpan.Day, timeSpan.End), "America/Toronto"); courseEvent.RecurrenceRules.Add(new RecurrencePattern(FrequencyType.Weekly) { //Count = 13 Until = new DateTime(2020, 12, 15) }); } courseEvent.Summary = utCourse.Abbr + ": " + utCourse.Name + " " + utSection.Name; try { courseEvent.Location = utSection.Location.Split(' ')[order]; } catch { } } if (utCourse.SemesterDetail == "Winter" || utCourse.SemesterDetail == "Year") { Event courseEvent = calendar.Create <Event>(); if (utCourse.Faculty == "Engineering") { courseEvent.Start = new iCalDateTime(this.ConvertDateTime(new DateTime(2021, 1, 3), timeSpan.Day, timeSpan.Start), "America/Toronto"); courseEvent.End = new iCalDateTime(this.ConvertDateTime(new DateTime(2021, 1, 3), timeSpan.Day, timeSpan.End), "America/Toronto"); courseEvent.RecurrenceRules.Add(new RecurrencePattern(FrequencyType.Weekly) { Until = new DateTime(2021, 4, 10) //Count = 13 }); } else { courseEvent.Start = new iCalDateTime(this.ConvertDateTime(new DateTime(2021, 1, 3), timeSpan.Day, timeSpan.Start), "America/Toronto"); courseEvent.End = new iCalDateTime(this.ConvertDateTime(new DateTime(2021, 1, 3), timeSpan.Day, timeSpan.End), "America/Toronto"); courseEvent.RecurrenceRules.Add(new RecurrencePattern(FrequencyType.Weekly) { Until = new DateTime(2021, 4, 10) //Count = 13 }); } courseEvent.Summary = utCourse.Abbr + ": " + utCourse.Name + " " + utSection.Name; try { courseEvent.Location = utSection.Location.Split(' ')[order]; } catch { } } order++; } } return(true); } } } return(false); }
public override IEnumerable <UTCourse> FetchItems() { List <UTCourse> results = new List <UTCourse>(); if (this.Content == null) { return(results); } this.Content = this.Content.Replace("\r\n", String.Empty); MatchCollection matches = CourseRegex.Matches(this.Content); // Used to accumulating course meet times string tempTime = ""; string tempLocation = ""; foreach (Match match in matches) { string line = match.Value; line = line.Replace("</td>", "|"); line = AngleRegex.Replace(line, String.Empty); line = CircleRegex.Replace(line, String.Empty); string[] properties = line.Split('|'); Match codeMatch = CodeRegex.Match(properties[0]); // Get rid of those which the first course in the page does not start with legit course code if (!codeMatch.Success && results.Count == 0) { continue; } string code = codeMatch.Groups["code"].ToString(); string prefix = codeMatch.Groups["prefix"].ToString(); CourseSection courseSection = null; if (properties.Length < 4) { continue; } string semester = properties[1].Trim(' '); string name = properties[2].Trim(' '); string section = properties[3].Trim(' '); string wait = null; string time = null; string location = null; string instructor = null; string matchedLocation = null; // Meaning this section of this course is not cancelled if (properties.Length >= 8) { wait = properties[4].Trim(' '); time = properties[5].Replace(" ", "").Replace(",", ""); location = properties[6].Replace(" ", ""); instructor = properties[7].Trim(' ').Replace(" ", ""); if (wait.Contains("Cancel")) { continue; } CourseSectionTime ptime; if (UTCourseSectionTime.TryParseRawTime(time, out ptime)) { // Construct same location n times matching the meeting times if (!location.Equals(" ")) { matchedLocation = location; for (int i = 1; i < ptime.MeetTimes.Count(); i++) { matchedLocation = String.Join(" ", matchedLocation, location); } } else { // Get the location from the previous section if (section.Equals(" ")) { if (results.Count > 0) { if (results.Last <UTCourse>().Sections.Count > 0) { location = results.Last <UTCourse>().Sections.Last <CourseSection>().Location; if (location != null) { location = location.Split(' ')[0]; matchedLocation = location; for (int i = 1; i < ptime.MeetTimes.Count(); i++) { matchedLocation = String.Join(" ", matchedLocation, location); } } } } } } } } // For some exceptions, section time is not written in one string, need to accumulating meet times if (section.Equals(" ")) { if (properties.Length >= 8) { tempTime += time; tempLocation = String.Join(" ", tempLocation, matchedLocation); } else { tempTime = ""; tempLocation = ""; } } else { if (properties.Length >= 8) { courseSection = new UTCourseSection() { Name = section, WaitList = wait.Equals("Y"), Instructor = instructor, Time = time, Location = location }; } UTCourse lastCourse = null; if (results.Count > 0) { lastCourse = results.Last <UTCourse>(); if (!String.IsNullOrEmpty(tempTime)) { if (lastCourse.Sections.Count > 0) { // Update the last tempTime and tempLocation CourseSection lastSection = lastCourse.Sections.Last <CourseSection>(); lastSection.Time = tempTime; lastSection.Location = tempLocation; } } } tempTime = time; tempLocation = matchedLocation; // Only need to add a section if (!codeMatch.Success) { if (lastCourse != null) { if (courseSection != null) { lastCourse.Sections.Add(courseSection); Console.Write(" {0} ", courseSection.Name); } } continue; } // Construct a course UTCourse course = new UTCourse() { Code = code, Name = name, Semester = semester, SemesterPrefix = prefix, Campus = "UTSG" }; // Add the newly constructed courseSection (if not cancelled) into the course if (courseSection != null) { course.Sections.Add(courseSection); Console.Write("{0}Course: {1} {2}", Environment.NewLine, course.Abbr, courseSection.Name); } else { Console.Write("{0}Course: {1} ", Environment.NewLine, course.Abbr); } results.Add(course); } } if (results.Count > 0) { // Commit the last change to the last section if (results.Last <UTCourse>().Sections.Count > 0) { CourseSection lastSec = results.Last <UTCourse>().Sections.Last <CourseSection>(); lastSec.Time = tempTime; lastSec.Location = tempLocation; } } return(results); }
public override IEnumerable <UTCourse> FetchItems() { dynamic allCourseData = JsonConvert.DeserializeObject(this.Content); List <UTCourse> allCourses = new List <UTCourse>(); foreach (dynamic category in allCourseData.categorydata) //Parallel.ForEach<dynamic>((IEnumerable<dynamic>)allCourseData.categorydata, // new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, // delegate(dynamic category) { foreach (dynamic course in category.course) { UTCourse courseObj = new UTCourse(); string code = course.code; courseObj.Code = code.Substring(0, 6); string secstring = course.section; if (code.Length > 8) { courseObj.Semester = code.Substring(9, 1); } else { courseObj.Semester = secstring.Substring(0, 1); } courseObj.SemesterPrefix = code.Substring(6, 2); courseObj.Name = course.name; courseObj.Campus = "UTSG"; Console.Out.Write("Course: " + courseObj.Abbr); if (course.activities != null) { foreach (dynamic activity in course.activities) { UTCourseSection section = new UTCourseSection(); section.Name = activity.name; section.Instructor = activity.instructor; Console.Out.Write(" " + section.Name); List <CourseSectionTimeSpan> meetTimes = new List <CourseSectionTimeSpan>(); bool hasTime = activity.times != null; if (hasTime) { foreach (dynamic time in activity.times) { string day = time.dayofweek; string timeFrom = time.time_from; string timeTo = time.time_to; CourseSectionTimeSpan timespan; timespan.Day = ((DayOfWeek)Enum.Parse(typeof(DayOfWeek), day)); timespan.Start = (byte)(Convert.ToInt32(timeFrom.Substring(0, 2)) * 4 + Convert.ToInt32(timeFrom.Substring(3, 2)) / 15); timespan.End = (byte)(Convert.ToInt32(timeTo.Substring(0, 2)) * 4 + Convert.ToInt32(timeTo.Substring(3, 2)) / 15); meetTimes.Add(timespan); } section.ParsedTime = new CourseSectionTime(meetTimes); } else { section.Time = "TBA"; } Console.Out.WriteLine(section.ParsedTime.ToString()); courseObj.Sections.Add(section); } } Console.Out.WriteLine(); allCourses.Add(courseObj); } //}); } Console.Out.WriteLine("Total number of courses: {0}", allCourses.Count); return(allCourses); }