예제 #1
0
        private bool CheckCourseAvailabilityFromMasterClassSchedule(Course course)
        {
            InsertLog("Checking availability of " + course.CourseCode + " " + course.Section + "...", Color.Black);

            WebManager schedulePage = new WebManager("https://banweb.cityu.edu.hk/pls/PROD/hwscrssh_cityu.P_DispOneSection");

            try
            {
                String[] courseSubjectAndCode = Regex.Split(course.CourseCode, @"(?<=\D)(?=\d)");
                schedulePage.SetReferer("https://banweb.cityu.edu.hk/pls/PROD/hwscrssh_cityu.P_GetCrse")
                .AddParameter("term", AddDropTerm)
                .AddParameter("subj", courseSubjectAndCode[0])
                .AddParameter("crse", courseSubjectAndCode[1])
                .AddParameter("camp", "")
                .AddParameter("web_avail", "")
                .Load();
                InsertLog(schedulePage.ToString());

                // Retrive the course details
                foreach (HtmlAgilityPack.HtmlNode row in schedulePage.GetHtmlNodeCollection("//div[@class='body']//tr[@bgcolor='#ffccff']"))
                {
                    HtmlAgilityPack.HtmlNodeCollection courseDetails = row.SelectNodes("td");

                    if (courseDetails[1].InnerText == course.Section)
                    {
                        courseDetails[6].SelectSingleNode("span[@style='display:none']")?.Remove();
                        int.TryParse(courseDetails[6].InnerText.Substring(2), out course.AvaliableQuota);

                        courseDetails[8].SelectSingleNode("span[@style='display:none']")?.Remove();
                        int.TryParse(courseDetails[8].InnerText, out course.WaitlistQuota);
                        break;
                    }
                }

                return(course.AvaliableQuota > 0 || course.WaitlistQuota > 0);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                if (ex is WebException)
                {
                    InsertLog("Server returned error: " + ex.Message, Color.Red);

                    if (ex.Message == "Break in attempt" || ex.Message == "Session timeout")
                    {
                        if (PrepareLogin() && Login())
                        {
                            return(CheckCourseAvailabilityFromMasterClassSchedule(course));
                        }
                    }
                }
                else if (ex is NullReferenceException || ex is ArgumentOutOfRangeException)
                {
                    InsertLog("Cannot retrieve the course details.", Color.Red);
                }
                else
                {
                    InsertLog(ex.ToString(), Color.Red);
                }
            }

            return(false);
        }
예제 #2
0
        private List <Course> GetCourseListFromMasterClassSchedule(String courseCode, List <String> courseSectionList)
        {
            InsertLog("Retrieving course details of " + courseCode + "...", Color.Black);

            WebManager schedulePage = new WebManager("https://banweb.cityu.edu.hk/pls/PROD/hwscrssh_cityu.P_DispOneSection");

            try
            {
                String[] courseSubjectAndCode = Regex.Split(courseCode, @"(?<=\D)(?=\d)");
                schedulePage.SetReferer("https://banweb.cityu.edu.hk/pls/PROD/hwscrssh_cityu.P_GetCrse")
                .AddParameter("term", AddDropTerm)
                .AddParameter("subj", courseSubjectAndCode[0])
                .AddParameter("crse", courseSubjectAndCode[1])
                .AddParameter("camp", "")
                .AddParameter("web_avail", "")
                .Load();
                InsertLog(schedulePage.ToString());

                // Retrieve the course title
                String courseTitle = "";
                String htmlSource  = schedulePage.GetHtmlNode("//div[@class='body']/b[1]").InnerText;
                if (htmlSource.IndexOf("Course : " + courseCode + " ") >= 0)
                {
                    courseTitle = htmlSource.Substring(htmlSource.IndexOf("Course : " + courseCode + " ") + courseCode.Length + 10);

                    InsertLog("title: " + courseTitle, Color.Green);
                }
                else
                {
                    throw new NullReferenceException();
                }

                // Retrive the course details
                List <Course> toReturn = new List <Course>();
                HtmlAgilityPack.HtmlNodeCollection courseTable = schedulePage.GetHtmlNodeCollection("//div[@class='body']//tr[@bgcolor='#ffccff']");
                if (courseTable != null)
                {
                    foreach (HtmlAgilityPack.HtmlNode row in courseTable)
                    {
                        HtmlAgilityPack.HtmlNodeCollection courseDetails = row.SelectNodes("td");

                        // Ignore the duplicate rows
                        if (courseDetails[0].InnerText != "" && courseSectionList.Remove(courseDetails[1].InnerText))
                        {
                            Course course = new Course();

                            course.CRN        = courseDetails[0].InnerText;
                            course.CourseCode = courseCode;
                            course.Section    = courseDetails[1].InnerText;
                            course.Title      = courseTitle;

                            course.Credit     = Convert.ToSingle(courseDetails[2].InnerText);
                            course.Date       = courseDetails[9].InnerText;
                            course.DayOfWeek  = courseDetails[10].InnerText;
                            course.Time       = courseDetails[11].InnerText;
                            course.Room       = courseDetails[13].InnerText + ", " + courseDetails[12].InnerText;
                            course.Instructor = courseDetails[14].InnerText;

                            courseDetails[6].SelectSingleNode("span[@style='display:none']")?.Remove();
                            int.TryParse(courseDetails[6].InnerText, out course.AvaliableQuota);
                            course.MaxQuota = Convert.ToInt32(courseDetails[7].InnerText);

                            courseDetails[8].SelectSingleNode("span[@style='display:none']")?.Remove();
                            int.TryParse(courseDetails[8].InnerText, out course.WaitlistQuota);

                            toReturn.Add(course);
                        }
                    }
                }

                String notFoundSections = "";
                foreach (String courseSection in courseSectionList)
                {
                    notFoundSections += (notFoundSections == "" ? "" : ", ") + courseSection;
                }
                if (notFoundSections != "")
                {
                    InsertLog("Cannot find section(s) " + notFoundSections + ".", Color.Red);
                }

                return(toReturn);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                if (ex is WebException)
                {
                    InsertLog("Server returned error: " + ex.Message, Color.Red);

                    if (ex.Message == "Break in attempt" || ex.Message == "Session timeout")
                    {
                        if (PrepareLogin() && Login())
                        {
                            return(GetCourseListFromMasterClassSchedule(courseCode, courseSectionList));
                        }
                    }
                }
                else if (ex is NullReferenceException || ex is ArgumentOutOfRangeException)
                {
                    InsertLog("Cannot retrieve the course details.", Color.Red);
                }
                else
                {
                    InsertLog(ex.ToString(), Color.Red);
                }
            }

            return(null);
        }