Exemplo n.º 1
0
        private List <string> GetAllSubjects()
        {
            List <string> subjects = new List <string>();

            string response = SimpleHTMLHelper.Post(SubjectsUrl, GetSubjectFormsParams());

            // response will be serialized to json with one html attribute that holds all of the
            // information we're interested in.
            NCStateSubjectsDTO data = JsonConvert.DeserializeObject <NCStateSubjectsDTO>(response);

            HtmlDocument htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(data.subj_html);

            var subjectsNode = from subject in htmlDoc.DocumentNode.Descendants("a")
                               where subject.Attributes["href"].Value == "#"
                               select subject;

            foreach (var subjectItem in subjectsNode)
            {
                subjects.Add(subjectItem.Attributes["data-value"].Value);
            }

            return(subjects);
        }
Exemplo n.º 2
0
        private List <Class> Get(string _subject)
        {
            List <Class> results  = new List <Class>();
            string       response = SimpleHTMLHelper.Post(URL, GetFormParams(_subject));

            // response will be serialized to json with one html attribute that holds all of the
            // information we're interested in.
            NCStateDTO data = JsonConvert.DeserializeObject <NCStateDTO>(response);

            HtmlDocument htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(data.html);

            var courses = from course in htmlDoc.DocumentNode.Descendants("section")
                          where course.Attributes["class"].Value == "course"
                          select course;

            foreach (var course in courses)
            {
                // Course attributes
                string id                = course.Attributes["id"].Value;
                string courseName        = course.Descendants("small").FirstOrDefault().InnerText;
                string courseDescription = course.Descendants("p").FirstOrDefault().InnerText;
                Course oneCourse         = new Course()
                {
                    CourseCode        = id,
                    CourseName        = courseName,
                    CourseDescription = courseDescription
                };

                // Class attributes (represented by a table).
                var classTable = from table in course.Descendants("table")
                                 select table;

                // Get all the rows of the table -- each row represents a class.
                var classes = (from cls in classTable.FirstOrDefault().Descendants("tr")
                               select cls).Skip(2); // First two rows are header rows and can be ignored

                foreach (var cls in classes)
                {
                    // Get all the class attributes (from td's)
                    var attributes = from att in cls.Descendants("td")
                                     select att;

                    // Raw from the HTML
                    string classSection   = attributes.ElementAt(0).InnerText;
                    string component      = attributes.ElementAt(1).InnerText;
                    string classNumber    = attributes.ElementAt(2).InnerText;
                    var    timeNode       = attributes.ElementAt(4);
                    string location       = attributes.ElementAt(5).InnerText;
                    var    instructorNode = attributes.ElementAt(6);
                    string dates          = attributes.ElementAt(7).InnerText;

                    // Cleaned Sub Objects
                    List <TimePeriod> timePeriods = ParseTimePeriods(timeNode);
                    List <Instructor> instructors = ParseInstructors(instructorNode);
                    DateTime          startDate   = DateTime.MinValue;
                    DateTime          endDate     = DateTime.MinValue;

                    if (dates.Contains(" - "))
                    {
                        startDate = ParseDate(dates.Substring(0, dates.IndexOf(" - ")).Trim());
                        endDate   = ParseDate(dates.Substring(dates.IndexOf(" - ") + 3).Trim());
                    }
                    else
                    {
                        DateTime.TryParse(dates, out startDate);
                    }

                    Class oneClass = new Class()
                    {
                        Component   = component,
                        Course      = oneCourse,
                        EndDate     = endDate,
                        Instructors = instructors,
                        Location    = location,
                        Notes       = string.Empty,
                        Number      = classNumber,
                        Section     = classSection,
                        StartDate   = startDate,
                        Times       = timePeriods
                    };

                    results.Add(oneClass);
                }
            }

            return(results);
        }