Пример #1
0
        public bool UpdateCourseStatus(PSCoursesModel currentCoursesModel, List<Course> oldCourses)
        {
            if (oldCourses == null || oldCourses.Count == 0)
            {
                return true;
            }

            foreach (var category in currentCoursesModel.Categories)
            {
                for (int i = 0; i < category.Courses.Count; i++)
                {
                    var currCourse = category.Courses[i];

                    var oldCourse =
                        oldCourses.Find(c => c.Title == currCourse.Title && c.ParentCateogry == currCourse.ParentCateogry);

                    if (oldCourse != null)
                    {
                        currCourse.LearningStatus = oldCourse.LearningStatus;
                        currCourse.Comment = oldCourse.Comment;
                    }
                }
            }

            return true;
        }
        //
        // GET: /PSCourse/
        // Default parse courses from the web
        public ActionResult Index(string courseUrl, DateTime? startDate)
        {
            if (HttpContext.Session[GlobalVariables.OAuthCodeKey] == null)
            {
                var model = new PSCoursesModel();
                return View(model);
            }
            else
            {
                _startDate = startDate;

                var startTime = System.DateTime.Now;
                PSCoursesModel currentCoursesModel;

                if (_startDate.HasValue && _startDate.Value < defaultStartTime)
                {
                    _startDate = defaultStartTime;
                }

                //Parsing
                var readCourseService = new ReadCourseFromWeb(courseUrl);
                var parser = new PSCourseParser(readCourseService, _startDate);
                currentCoursesModel = parser.ParseCourses();

                //Write Output
                WriteToGoogleDrive(parser, currentCoursesModel);

                var endTime = System.DateTime.Now;
                ViewData["StartTime"] = startTime;
                ViewData["EndTime"] = endTime;
                ViewData["ProcessTime"] = endTime - startTime;

                return View(currentCoursesModel);
            }
        }
Пример #3
0
        //public PSCoursesModel ParseCourses(string psFilePath)
        public PSCoursesModel ParseCourses()
        {
            //var html = DownloadPSHtml();
            //var html = new StreamReader(@"C:\Users\Weili\Desktop\PSLibrary.html").ReadToEnd();
            //var html = LoadPSHtmlFile(@"C:\Users\Weili\Desktop\Pluralsight Course Library.htm");
            //var html = LoadPSHtmlFile(psFilePath);
            //var html = LoadPSHtmlByInputStream(file.InputStream);

            var html = _readCourseService.ReadCourses();
            var listCategories = ParsePsHtml(html);

            var model = new PSCoursesModel();
            model.Categories = listCategories;
            model.ParsedTime = System.DateTime.Now;

            return model;
        }
        public ActionResult ParseFile(string fileName)
        {
            if (HttpContext.Session[GlobalVariables.OAuthCodeKey] == null)
            {
                var model = new PSCoursesModel();
                return View(model);
            }
            else
            {
                var code = HttpContext.Session[GlobalVariables.OAuthCodeKey].ToString();

                var parameters = OAuthHelper.GetOAuthParameter();
                parameters.AccessCode = code;
                OAuthUtil.GetAccessToken(parameters);

                GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, GlobalVariables.GoogleSpreadSheetAppName, parameters);
                SpreadsheetsService service = new SpreadsheetsService(GlobalVariables.GoogleSpreadSheetAppName);
                service.RequestFactory = requestFactory;

                //Parsing
                var startTime = System.DateTime.Now;
                var readCourseService = new ReadCourseFromFile(fileName);
                var parser = new PSCourseParser(readCourseService, defaultStartTime);
                var currentCoursesModel = parser.ParseCourses();

                var googleHelper = new GoogleDriveHelper(defaultStartTime);
                googleHelper.Service = service;
                var oldCourses = googleHelper.FetchOldCourseSheet();
                parser.UpdateCourseStatus(currentCoursesModel, oldCourses);

                var success = googleHelper.AddDriveSpreadsheet(currentCoursesModel);

                var endTime = System.DateTime.Now;
                var processingTime = endTime - startTime;

                ViewData["StartTime"] = startTime;
                ViewData["EndTime"] = endTime;
                ViewData["ProcessTime"] = processingTime;

                return View(currentCoursesModel);
            }
        }
Пример #5
0
        public bool AddDriveSpreadsheet(PSCoursesModel model)
        {
            //Delete an existing worksheet
            DeleteExistingWorkSheet();

            //Create a new worksheet
            if (CreateNewSpreadsheet(model) == false)
                return false;

            //Add Header
            if (AddHeader() == false)
                return false;

            //Add Content
            if (AddContent(model) == false)
                return false;

            //Add Summary
            if (AddSummary(model) == false)
                return false;

            return true;
        }
        private bool WriteToGoogleDrive(PSCourseParser parser, PSCoursesModel currentCoursesModel)
        {
            var code = HttpContext.Session[GlobalVariables.OAuthCodeKey].ToString();

            var parameters = OAuthHelper.GetOAuthParameter();
            parameters.AccessCode = code;
            OAuthUtil.GetAccessToken(parameters);

            GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, GlobalVariables.GoogleSpreadSheetAppName, parameters);
            SpreadsheetsService service = new SpreadsheetsService(GlobalVariables.GoogleSpreadSheetAppName);
            service.RequestFactory = requestFactory;

            var googleHelper = new GoogleDriveHelper(_startDate);
            googleHelper.Service = service;
            var oldCourses = googleHelper.FetchOldCourseSheet();
            parser.UpdateCourseStatus(currentCoursesModel, oldCourses);

            return googleHelper.AddDriveSpreadsheet(currentCoursesModel);
        }
Пример #7
0
        private bool CreateNewSpreadsheet(PSCoursesModel model)
        {
            var service = InitiateService();

            var query = new SpreadsheetQuery();
            var feed = service.Query(query);

            if (feed.Entries.Count == 0) return false;

            SpreadsheetEntry ss = null;
            foreach (SpreadsheetEntry spreadsheetEntry in feed.Entries)
            {
                if (spreadsheetEntry.Title.Text == SpreadSheetTitle)
                {
                    ss = spreadsheetEntry;
                    break;
                }
            }
            if (ss == null) return false;

            WorksheetEntry worksheet = new WorksheetEntry();
            worksheet.Title.Text = WorkSheetTitle;
            worksheet.Rows = (uint)ChooseRowNum(model); ;
            worksheet.Cols = 20;

            //AddContent(worksheet, model);
            WorksheetFeed wsFeed = ss.Worksheets;
            service.Insert(wsFeed, worksheet);

            return true;
        }
Пример #8
0
        private int ChooseRowNum(PSCoursesModel model)
        {
            int totalCategories = model.Categories.Count();
            int totalCourses = model.Categories.Sum(c => c.Courses.Count());
            const int headRoom = 30;

            return totalCategories * 2 + totalCourses + headRoom;
        }
Пример #9
0
        private bool AddSummary(PSCoursesModel model)
        {
            var service = InitiateService();

            SpreadsheetQuery query = new SpreadsheetQuery();
            SpreadsheetFeed feed = service.Query(query);
            SpreadsheetEntry ss = null;
            foreach (SpreadsheetEntry spreadsheetEntry in feed.Entries)
            {
                if (spreadsheetEntry.Title.Text == SpreadSheetTitle)
                {
                    ss = spreadsheetEntry;
                    break;
                }
            }
            if (ss == null) return false;

            WorksheetFeed wsFeed = ss.Worksheets;
            WorksheetEntry worksheet = null;
            foreach (WorksheetEntry worksheetEntry in wsFeed.Entries)
            {
                if (worksheetEntry.Title.Text == WorkSheetTitle)
                {
                    worksheet = worksheetEntry;
                    break;
                }
            }
            if (worksheet == null) return false;

            uint rowCount = worksheet.Rows;

            CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);
            CellFeed cellFeed = service.Query(cellQuery);

            uint nextRow = 2;

            cellFeed[nextRow, 9].InputValue = "Summary";
            cellFeed[nextRow, 9].Update();
            nextRow++;

            cellFeed[nextRow, 9].InputValue = "Total Categories";
            cellFeed[nextRow, 9].Update();

            cellFeed[nextRow, 10].InputValue = model.Categories.Count().ToString();
            cellFeed[nextRow, 10].Update();
            nextRow++;

            cellFeed[nextRow, 9].InputValue = "Total Courses";
            cellFeed[nextRow, 9].Update();

            cellFeed[nextRow, 10].InputValue = model.Categories.Sum(c => c.Courses.Count()).ToString();
            cellFeed[nextRow, 10].Update();
            nextRow++;

            cellFeed[nextRow, 9].InputValue = "New";
            cellFeed[nextRow, 9].Update();

            var newFormula = string.Format("=COUNTIF(F1:F{0}, \"{1}\")", rowCount.ToString(), LearningStatus.New.ToString());
            cellFeed[nextRow, 10].InputValue = newFormula;
            cellFeed[nextRow, 10].Update();
            nextRow++;

            cellFeed[nextRow, 9].InputValue = "Finished";
            cellFeed[nextRow, 9].Update();

            var finishedFormular = string.Format("=COUNTIF(F1:F{0}, \"{1}\")", rowCount.ToString(), LearningStatus.Finished.ToString());
            cellFeed[nextRow, 10].InputValue = finishedFormular;
            cellFeed[nextRow, 10].Update();
            nextRow++;

            cellFeed[nextRow, 9].InputValue = "Partial";
            cellFeed[nextRow, 9].Update();

            var partialFormula = string.Format("=COUNTIF(F1:F{0}, \"{1}\")", rowCount.ToString(), LearningStatus.Partial.ToString());
            cellFeed[nextRow, 10].InputValue = partialFormula;
            cellFeed[nextRow, 10].Update();
            nextRow++;

            cellFeed[nextRow, 9].InputValue = "Irrelevant";
            cellFeed[nextRow, 9].Update();

            var irrelevantFormula = string.Format("=COUNTIF(F1:F{0}, \"{1}\")", rowCount.ToString(), LearningStatus.Irrelevant.ToString());
            cellFeed[nextRow, 10].InputValue = irrelevantFormula;
            cellFeed[nextRow, 10].Update();
            nextRow++;

            return true;
        }
Пример #10
0
        private bool AddContent(PSCoursesModel model)
        {
            var service = InitiateService();

            SpreadsheetQuery query = new SpreadsheetQuery();
            SpreadsheetFeed feed = service.Query(query);
            SpreadsheetEntry ss = null;
            foreach (SpreadsheetEntry spreadsheetEntry in feed.Entries)
            {
                if (spreadsheetEntry.Title.Text == SpreadSheetTitle)
                {
                    ss = spreadsheetEntry;
                    break;
                }
            }
            if (ss == null) return false;

            WorksheetFeed wsFeed = ss.Worksheets;
            WorksheetEntry worksheet = null;
            foreach (WorksheetEntry worksheetEntry in wsFeed.Entries)
            {
                if (worksheetEntry.Title.Text == WorkSheetTitle)
                {
                    worksheet = worksheetEntry;
                    break;
                }
            }
            if (worksheet == null) return false;

            // Define the URL to request the list feed of the worksheet.
            AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);

            // Fetch the list feed of the worksheet.
            ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString());
            ListFeed listFeed = service.Query(listQuery);

            var titleMsg = "All Courses as of " + System.DateTime.Now.ToShortDateString();
            if (_startDate.HasValue)
            {
                titleMsg = "Courses released after " + _startDate.Value.ToShortDateString() + " - " + "Parsed on " + System.DateTime.Now.ToShortDateString();
            }

            ListEntry parseTimeRow = GenerateRow(titleField: titleMsg);
            service.Insert(listFeed, parseTimeRow);

            foreach (var category in model.Categories)
            {
                ListEntry emptyRow = GenerateEmptyRow();
                service.Insert(listFeed, emptyRow);

                ListEntry categoryRow = GenerateCategoryRow(category.CategoryName, category.Courses.Count());
                service.Insert(listFeed, categoryRow);

                foreach (var course in category.Courses)
                {
                    ListEntry courseRow = GenerateCourseRow(course);
                    service.Insert(listFeed, courseRow);
                }
            }

            return true;
        }