public void TryParseTimeSpanIntBasics() { byte result; UTCourseSectionTimeSpan.TryParseTimeSpanInt("11:15", out result); Assert.AreEqual(45, result); UTCourseSectionTimeSpan.TryParseTimeSpanInt("11:30", out result); Assert.AreEqual(46, result); UTCourseSectionTimeSpan.TryParseTimeSpanInt("11", out result); Assert.AreEqual(44, result); }
public void TryParseRawTimeSpanBasics() { CourseSectionTimeSpan span; UTCourseSectionTimeSpan.TryParseRawTimeSpan("T", out span); Assert.AreEqual("Tuesday 0:00-0:00", span.ToString()); UTCourseSectionTimeSpan.TryParseRawTimeSpan("F9", out span); Assert.AreEqual("Friday 9:00-10:00", span.ToString()); UTCourseSectionTimeSpan.TryParseRawTimeSpan("W2:30", out span); Assert.AreEqual("Wednesday 14:30-15:30", span.ToString()); UTCourseSectionTimeSpan.TryParseRawTimeSpan("M2-4", out span); Assert.AreEqual("Monday 14:00-16:00", span.ToString()); UTCourseSectionTimeSpan.TryParseRawTimeSpan("W10:30-12", out span); Assert.AreEqual("Wednesday 10:30-12:00", span.ToString()); UTCourseSectionTimeSpan.TryParseRawTimeSpan("W7-9", out span); Assert.AreEqual("Wednesday 19:00-21:00", span.ToString()); }
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 void TryParseTimeSpanIntNotInt() { byte result; Assert.AreEqual(false, UTCourseSectionTimeSpan.TryParseTimeSpanInt("a:30:30", out result)); }
public void TryParseTimeSpanIntPrecision() { byte result; Assert.AreEqual(false, UTCourseSectionTimeSpan.TryParseTimeSpanInt("11:32", out result)); }
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); }