From 52a33d4c3ee4bcf05ea75fc5d8f125b756334db9 Mon Sep 17 00:00:00 2001 From: Yishan Zhao Date: Sun, 13 Oct 2019 18:30:01 -0400 Subject: [PATCH] Add exception handling --- CourseSelection/Algorithm.cs | 51 ++---- CourseSelection/CourseSelection.csproj | 3 +- CourseSelection/Dataset.cs | 219 ------------------------- 3 files changed, 19 insertions(+), 254 deletions(-) delete mode 100644 CourseSelection/Dataset.cs diff --git a/CourseSelection/Algorithm.cs b/CourseSelection/Algorithm.cs index 71d610c..3c1e504 100644 --- a/CourseSelection/Algorithm.cs +++ b/CourseSelection/Algorithm.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Windows; using System.Text; +using System.Threading.Tasks; namespace CourseSelection { @@ -176,50 +177,34 @@ public class Crawler public string TermID = "202001"; //public bool IsExcludeFC = true; - public Course GetCourse(string courseName) + public async Task GetCourse(string courseName) { Course ret; URL url = new URL(courseName, TermID); var httpClient = new HttpClient(); - var html = httpClient.GetStringAsync(url).Result; + string html = await httpClient.GetStringAsync(url); var htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(html); string fullName; List divs = null; - try - { - divs = htmlDocument - .DocumentNode - .Descendants("div") - .Where(node => node.GetAttributeValue("id", "") == courseName) - .First() - .Descendants("div") - .Where(node => node.GetAttributeValue("class", "") - .Equals("section delivery-f2f")).ToList(); - - fullName = htmlDocument - .DocumentNode - .Descendants("span") - .Where(node => node.GetAttributeValue("class", "") == "course-title") - .First() - .InnerText; - - } - catch - { - MessageBox.Show( - "Unable to get the course info\nPlease check if there is any typo in course name", - "Error", - MessageBoxButton.OK, - MessageBoxImage.Error - ); - - return null; - } - + divs = htmlDocument + .DocumentNode + .Descendants("div") + .Where(node => node.GetAttributeValue("id", "") == courseName) + .First() + .Descendants("div") + .Where(node => node.GetAttributeValue("class", "") + .Equals("section delivery-f2f")).ToList(); + + fullName = htmlDocument + .DocumentNode + .Descendants("span") + .Where(node => node.GetAttributeValue("class", "") == "course-title") + .First() + .InnerText; List
sections = new List
(); // Enumerate through each section foreach (var div in divs) diff --git a/CourseSelection/CourseSelection.csproj b/CourseSelection/CourseSelection.csproj index 0bc092b..a491008 100644 --- a/CourseSelection/CourseSelection.csproj +++ b/CourseSelection/CourseSelection.csproj @@ -33,7 +33,7 @@ publish.htm true 3 - 2.0.1.%2a + 2.0.2.%2a true true true @@ -117,7 +117,6 @@ App.xaml Code - diff --git a/CourseSelection/Dataset.cs b/CourseSelection/Dataset.cs deleted file mode 100644 index c42a988..0000000 --- a/CourseSelection/Dataset.cs +++ /dev/null @@ -1,219 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CourseSelection -{ - /* - public static class Dataset - { - public static void Initialize() - { - CMSC250 = new Course( - "Discrete Structures", - new Section( - "CMSC250", - "0205", - new KeyValuePair( - "Discussion", - new Class( - new Weekday( - DayOfWeek.Monday, - new Schedule( - "0800", - "0850" - ) - ), - new Weekday( - DayOfWeek.Wednesday, - new Schedule( - "0800", - "0850" - ) - ) - ) - ), - new KeyValuePair( - "Lecture", - new Class( - new Weekday( - DayOfWeek.Tuesday, - new Schedule( - "1230", - "1345" - ) - ), - new Weekday( - DayOfWeek.Thursday, - new Schedule( - "1230", - "1345" - ) - ) - ) - ) - ) - ); - - CMSC216 = new Course( - "Introduction to Computer Systems", - new Section( - "CMSC216", - "0301", - new KeyValuePair( - "Discussion", - new Class( - new Weekday( - DayOfWeek.Monday, - new Schedule( - "0900", - "0950" - ) - ), - new Weekday( - DayOfWeek.Wednesday, - new Schedule( - "0900", - "0950" - ) - ) - ) - ), - new KeyValuePair( - "Lecture", - new Class( - new Weekday( - DayOfWeek.Tuesday, - new Schedule( - "1530", - "1645" - ) - ), - new Weekday( - DayOfWeek.Thursday, - new Schedule( - "1530", - "1645" - ) - ) - ) - ) - ) - ); - - MATH240 = new Course( - "Introduction to Linear Algebra", - new Section( - "MATH240", - "0312", - new KeyValuePair( - "Discussion", - new Class( - new Weekday( - DayOfWeek.Tuesday, - new Schedule( - "1100", - "1150" - ) - ), - new Weekday( - DayOfWeek.Thursday, - new Schedule( - "1100", - "1150" - ) - ) - ) - ), - new KeyValuePair( - "Lecture", - new Class( - new Weekday( - DayOfWeek.Monday, - new Schedule( - "1500", - "1550" - ) - ), - new Weekday( - DayOfWeek.Wednesday, - new Schedule( - "1500", - "1550" - ) - ), - new Weekday( - DayOfWeek.Friday, - new Schedule( - "1500", - "1550" - ) - ) - ) - ) - ) - ); - FIRE276 = new Course( - "FIRE", - new Section( - "FIRE276", - "0101", - new KeyValuePair( - "Lecture", - new Class( - new Weekday( - DayOfWeek.Tuesday, - new Schedule( - "1400", - "1450" - ) - ) - ) - ), - new KeyValuePair( - "Lab", - new Class( - new Weekday( - DayOfWeek.Tuesday, - new Schedule( - "0900", - "1000" - ) - ), - new Weekday( - DayOfWeek.Thursday, - new Schedule( - "0900", - "1000" - ) - ), - new Weekday( - DayOfWeek.Wednesday, - new Schedule( - "1000", - "1100" - ), - new Schedule( - "1130", - "1430" - ) - ) - ) - ) - ) - ); - - Courses = new Course[4] { CMSC216, CMSC250, MATH240, FIRE276 }; - } - - public static Course[] Courses; - - public static Course CMSC250; - public static Course CMSC216; - public static Course MATH240; - public static Course FIRE276; - } - */ -}