public static async Task <CourseDetail> GetCourseDetail(string courseId) { var url = "https://aps.ntut.edu.tw/course/tw/Select.jsp"; var parameters = new Dictionary <string, object>() { { "code", courseId }, { "format", -1 } }; var response = await Request(url, "POST", parameters); string responseString = await ConvertStreamToString(await response.Content.ReadAsStreamAsync(), true); response.Dispose(); if (responseString.Contains("《尚未登錄入口網站》 或 《應用系統連線已逾時》")) { throw new SessionExpiredException(); } return(CourseDetail.Parse(responseString)); }
private void ApplyCourseDetail(CourseDetail detail) { courseNameTextBlock.Text = detail.Name; string teachersText = ""; string classesText = ""; string classRoomsText = ""; for (int i = 0, count = detail.Teachers.Count; i < count; i++) { teachersText += detail.Teachers[i] + (i == count - 1 ? "" : "、"); } for (int i = 0, count = detail.Classes.Count; i < count; i++) { classesText += detail.Classes[i] + (i == count - 1 ? "" : "、"); } for (int i = 0, count = detail.ClassRooms.Count; i < count; i++) { classRoomsText += detail.ClassRooms[i] + (i == count - 1 ? "" : "、"); } var detailText = string.Format( "課號:\t\t{0}\n" + "名稱:\t\t{1}\n" + "學分:\t\t{2}\n" + "時數:\t\t{3}\n" + "類型:\t\t{4}\n" + "授課教師:\t{5}\n" + "開課班級:\t{6}\n" + "上課教室:\t{7}\n" + "修課人數:\t{8}\n" + "徹選人數:\t{9}\n\n" + "{10}", detail.CourseId, detail.Name, detail.Credits, detail.Hours, detail.Type, teachersText, classesText, classRoomsText, detail.PeopleCount, detail.QuitPeopleCount, detail.Note); detailTextBlock.Text = detailText; for (int i = 0, count = detail.Students.Count; i < count; i++) { var student = detail.Students[i]; var margin = new Thickness(10, 10, 10, 10); TextBlock classTextBlock = new TextBlock(); TextBlock idTextBlock = new TextBlock(); TextBlock nameTextBlock = new TextBlock(); TextBlock englishNameTextBlock = new TextBlock(); TextBlock classStatusTextBlock = new TextBlock(); TextBlock schoolStatusTextBlock = new TextBlock(); Button searchButton = new Button(); classTextBlock.Margin = idTextBlock.Margin = nameTextBlock.Margin = englishNameTextBlock.Margin = classStatusTextBlock.Margin = schoolStatusTextBlock.Margin = margin; classTextBlock.FontSize = idTextBlock.FontSize = nameTextBlock.FontSize = englishNameTextBlock.FontSize = classStatusTextBlock.FontSize = schoolStatusTextBlock.FontSize = 15; classTextBlock.Text = student.Class; idTextBlock.Text = student.StudentId; nameTextBlock.Text = student.Name; englishNameTextBlock.Text = student.EnglishName; classStatusTextBlock.Text = student.ClassStatus; schoolStatusTextBlock.Text = student.SchoolStatus; searchButton.Content = "查詢課表"; searchButton.Click += (sender, e) => { Frame.Navigate(typeof(CurriculumPage), student.StudentId); }; Grid.SetColumn(classTextBlock, 0); Grid.SetColumn(idTextBlock, 1); Grid.SetColumn(nameTextBlock, 2); Grid.SetColumn(englishNameTextBlock, 3); Grid.SetColumn(classStatusTextBlock, 4); Grid.SetColumn(schoolStatusTextBlock, 5); Grid.SetColumn(searchButton, 6); Grid.SetRow(classTextBlock, i); Grid.SetRow(idTextBlock, i); Grid.SetRow(nameTextBlock, i); Grid.SetRow(englishNameTextBlock, i); Grid.SetRow(classStatusTextBlock, i); Grid.SetRow(schoolStatusTextBlock, i); Grid.SetRow(searchButton, i); studentsGrid.Children.Add(classTextBlock); studentsGrid.Children.Add(idTextBlock); studentsGrid.Children.Add(nameTextBlock); studentsGrid.Children.Add(englishNameTextBlock); studentsGrid.Children.Add(classStatusTextBlock); studentsGrid.Children.Add(schoolStatusTextBlock); studentsGrid.Children.Add(searchButton); RowDefinition rowDefinition = new RowDefinition(); rowDefinition.Height = GridLength.Auto; studentsGrid.RowDefinitions.Add(rowDefinition); } }
public static CourseDetail Parse(string html) { CourseDetail detail = new CourseDetail(); var courseId = new Regex("<tr><th>課 號<td>([^\n]*)\n").Match(html).Groups[1].Value; var schoolYear = new Regex("<tr><th>學 年 度<td>([^<]*)</tr>\n").Match(html).Groups[1].Value; var semester = new Regex("<tr><th>學 期<td>([^<]*)</tr>\n").Match(html).Groups[1].Value; var name = new Regex("<tr><th>課程名稱\n").Match(html).Groups[1].Value; var phase = new Regex("<tr><th>階 段<td>([^<]*)</tr>\n").Match(html).Groups[1].Value; var credits = new Regex("<tr><th>學 分<td>([^<]*)</tr>\n").Match(html).Groups[1].Value; var hours = new Regex("<tr><th>時 數<td>([^<]*)</tr>\n").Match(html).Groups[1].Value; var type = new Regex("<tr><th>類 別<td>([^<]*)</tr>\n").Match(html).Groups[1].Value; var peopleCount = new Regex("<tr><th>修課人數<td>([^<]*)</tr>\n").Match(html).Groups[1].Value; var quitPeopleCount = new Regex("<tr><th>撤選人數<td>([^<]*)</tr>\n").Match(html).Groups[1].Value; var language = new Regex("<tr><th>授課語言<td>([^\n]*)\n").Match(html).Groups[1].Value; var audit = new Regex("<tr><th>隨班附讀<td>([^\n]*)\n").Match(html).Groups[1].Value; var pratice = new Regex("<tr><th>實驗、實習<td>([^\n]*)\n").Match(html).Groups[1].Value; var note = new Regex("<tr><th>備 註<td>([^\n]*)\n").Match(html).Groups[1].Value; detail.CourseId = courseId; detail.SchoolYear = int.Parse(schoolYear); detail.Semester = int.Parse(semester); detail.Name = name; detail.Phase = int.Parse(phase); detail.Credits = float.Parse(credits); detail.Hours = float.Parse(hours); detail.Type = type; detail.PeopleCount = int.Parse(peopleCount); detail.QuitPeopleCount = int.Parse(quitPeopleCount); detail.Language = language; detail.Audit = audit == "是"; detail.Pratice = pratice == "是"; detail.Note = note; switch (detail.Type) { case "○": detail.Type = "部訂共同必修"; break; case "△": detail.Type = "校訂共同必修"; break; case "☆": detail.Type = "共同選修"; break; case "●": detail.Type = "部訂專業必修"; break; case "▲": detail.Type = "校訂專業必修"; break; case "★": detail.Type = "專業選修"; break; } // TODO: Fix the folowing parsing algorithm //Regex regex = new Regex( // "<tr><th>授課教師\n" + // "<td>(?:<a href=\"Teach.jsp[^\"]+\">(?<teachers>[^<]*)</a>(?:<br>)?\\s*\n*)*\\s*\n*" + // "(?:<A href=\"[^\"]+\">《查詢教學大綱與進度表》</A><BR>\n+)?" + // "<tr><th>開課班級\n" + // "<td>(?:<a href=\"[^\"]+\">(?<classes>[^<]*)</a><br>\n)*\\s?\n" + // "<tr><th>教 室\n" + // "<td>(?:<a href=\"[^\"]+\">(?<classRooms>[^<]*)</a><br>\n)*\\s?\n" + // "<tr><th>修課人數<td>([^<]*)</tr>\n" + // "<tr><th>撤選人數<td>([^<]*)</tr>\n" + // "<tr><th>教學助理\n" + // "<td>(?:(?!<tr>).|\n)+" // , RegexOptions.Multiline | RegexOptions.IgnoreCase); //var match = regex.Match(html); //foreach (Capture capture in match.Groups["teachers"].Captures) // detail.Teachers.Add(capture.Value); //foreach (Capture capture in match.Groups["classes"].Captures) // detail.Classes.Add(capture.Value); //foreach (Capture capture in match.Groups["classRooms"].Captures) // detail.ClassRooms.Add(capture.Value); var studentsRegex = new Regex("^<tr><td>([^\\s]*)\\s*<td><a href=\"[^\"]+\">([^<]*)</a><td>([^<]*)<td>\\s*([^<]+)<td><div align=center>([^<]*)</div><td(?: bgcolor=#ffcccc)?>([^\\s]*)", RegexOptions.Multiline | RegexOptions.IgnoreCase); var studentsMatches = studentsRegex.Matches(html); foreach (Match studentMatch in studentsMatches) { Student student = new Student(); student.Class = studentMatch.Groups[1].Value; student.StudentId = studentMatch.Groups[2].Value; student.Name = studentMatch.Groups[3].Value; student.EnglishName = studentMatch.Groups[4].Value; student.ClassStatus = studentMatch.Groups[5].Value; student.SchoolStatus = studentMatch.Groups[6].Value; detail.Students.Add(student); } return(detail); }