示例#1
0
        /// <summary>
        /// Get all the courses in all of the pages present in the UofT Course Calendar,
        /// and save it in a .json format called "courselist.json"
        /// </summary>
        public void GetCourseList()
        {
            // Start and navigate the browser to the webpage containing all the courses
            Browser.Initialize();
            Browser.WebInstance.Url = "https://fas.calendar.utoronto.ca/search-courses";

            // Get a list of courses and save it
            List <Course> courses      = new List <Course>();
            bool          isAtLastPage = false;

            do
            {
                var coursesOnPage = GetCoursesOnPage();
                courses.AddRange(coursesOnPage);

                if (CourseListPage.IsAtLastPage())
                {
                    isAtLastPage = true;
                }
                else
                {
                    CourseListPage.GotoNextPage();
                }
            }while (!isAtLastPage);

            string json = JsonConvert.SerializeObject(courses);

            File.WriteAllText("courses.json", json);

            Browser.Close();
        }
示例#2
0
        /// <summary>
        /// Parses the HTML and populates the courses in the database
        /// If {overrideAllSchedules} is true, it will delete all existing course data from the database
        /// </summary>
        /// <param name="overrideAllSchedules">Whether to delete existing course data or not</param>
        public void CreateCourseSchedules(bool overrideAllSchedules)
        {
            db.ObjectTrackingEnabled = true;

            if (overrideAllSchedules)
            {
                RemoveAllCourseSchedules();
            }

            Browser.Initialize();
            TimetablePage.GotoPage();
            TimetablePage.SelectTerm(new string[] { "F", "S", "Y" });
            TimetablePage.SearchForCourses();
            TimetablePage.WaitForContentToLoad();

            CreateCourseSchedule();

            Browser.Close();
            db.Connection.Close();
            db.Dispose();
        }
        /// <summary>
        /// Deletes all building records in the database and repopulates it with new data
        /// </summary>
        public void RedoBuildingList()
        {
            db.ExecuteCommand("DELETE FROM BuildingDistances");
            db.ExecuteCommand("DELETE FROM Building");

            Browser.Initialize();
            Browser.WebInstance.Url = "http://map.utoronto.ca/c/buildings";

            using (UofTDataContext db = new UofTDataContext())
            {
                IWebElement buildingsList = Browser.WebInstance.FindElement(By.ClassName("buildinglist"));
                IReadOnlyList <IWebElement> buildingElements = buildingsList.FindElements(By.TagName("li"));

                foreach (IWebElement building in buildingElements)
                {
                    string[] description  = building.FindElement(By.XPath("./dl/dt")).Text.Split('|');
                    string   address      = building.FindElement(By.XPath("./dl/dd[1]")).Text.Trim();
                    string   buildingName = description[0].Trim();
                    string   buildingCode = description[1].Trim();

                    Building newBuilding = new Building
                    {
                        Address      = address + " Toronto, Canada",
                        BuildingCode = buildingCode,
                        BuildingName = buildingName,
                        Latitude     = null,
                        Longitude    = null
                    };

                    db.Buildings.InsertOnSubmit(newBuilding);
                }
                db.SubmitChanges();
            }

            Browser.Close();
        }