private ListEntry GenerateCourseRow(Course course)
 {
     ListEntry row = new ListEntry();
     row.Elements.Add(new ListEntry.Custom() { LocalName = HeaderTitle, Value = course.Title });
     row.Elements.Add(new ListEntry.Custom() { LocalName = HeaderAuthor, Value = course.Author });
     row.Elements.Add(new ListEntry.Custom() { LocalName = HeaderLevel, Value = course.Level });
     row.Elements.Add(new ListEntry.Custom() { LocalName = HeaderDuration, Value = course.Duration.ToString() });
     row.Elements.Add(new ListEntry.Custom() { LocalName = HeaderRelease, Value = course.ReleaseDate.ToShortDateString() });
     row.Elements.Add(new ListEntry.Custom() { LocalName = HeaderStatus, Value = course.LearningStatus.ToString() });
     row.Elements.Add(new ListEntry.Custom() { LocalName = HeaderCommment, Value = course.Comment });
     return row;
 }
        public List<Course> FetchOldCourseSheet()
        {
            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 null;

            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 null;

            // 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 courses = new List<Course>();
            var currentCategoryName = "";

            // Iterate through each row, printing its cell values.
            foreach (ListEntry row in listFeed.Entries)
            {
                var course = new Course();
                // Iterate over the remaining columns, and print each cell value
                foreach (ListEntry.Custom element in row.Elements)
                {
                    if (element.LocalName == HeaderTitle)
                    {
                        course.Title = element.Value;
                    }
                    else if (element.LocalName == HeaderAuthor)
                    {
                        course.Author = element.Value;
                    }
                    else if (element.LocalName == HeaderLevel)
                    {
                        course.Level = element.Value;
                    }
                    else if (element.LocalName == HeaderDuration)
                    {
                        if (string.IsNullOrEmpty(element.Value) == false)
                            course.Duration = TimeSpan.Parse(element.Value);
                    }
                    else if (element.LocalName == HeaderRelease)
                    {
                        if (string.IsNullOrEmpty(element.Value) == false)
                            course.ReleaseDate = System.DateTime.Parse(element.Value);
                    }
                    else if (element.LocalName == HeaderStatus)
                    {
                        if (string.IsNullOrEmpty(element.Value) == false)
                            course.LearningStatus = (LearningStatus)Enum.Parse(typeof(LearningStatus), element.Value);
                    }
                    else if (element.LocalName == HeaderCommment)
                    {
                        course.Comment = element.Value;
                    }
                }

                if (!string.IsNullOrEmpty(course.Title.Trim()) && !string.IsNullOrEmpty(course.Author.Trim()) && string.IsNullOrEmpty(course.Level.Trim()))
                {
                    currentCategoryName = course.Title;
                }

                if (string.IsNullOrEmpty(course.Level.Trim()) == false) //Determine whether this is a actual course
                {
                    course.ParentCateogry = currentCategoryName;
                    courses.Add(course);
                }
            }

            return courses;
        }
        private List<Course> GetCourses(HtmlNode node, string categoryName = "")
        {
            if (node == null)
                return null;

            var courses = new List<Course>();

            node = node.SelectSingleNode("div[@class='wrap']");
            //var courseTable = node.SelectSingleNode("table/tbody"); //node.Descendants("table").First();
            var courseTable = node.Descendants("table").First();

            foreach (var row in courseTable.ChildNodes)
            {
                if (row.Name == "tr")
                {
                    var course = new Course();
                    foreach (var td in row.ChildNodes)
                    {
                        if (td.Attributes["class"] != null)
                        {
                            if (td.Attributes["class"].Value.Contains("title"))
                            {
                                var aNode = td.SelectSingleNode("./a");
                                if (aNode != null)
                                {
                                    course.Title = aNode.InnerText.Trim();
                                    course.Url = PSMainUrl + aNode.Attributes["href"].Value;
                                }
                            }
                            else if (td.Attributes["class"].Value.Contains("author"))
                            {
                                var aNode = td.SelectNodes("./a");
                                if (aNode != null)
                                {
                                    var addComma = false;
                                    foreach (var item in aNode)
                                    {
                                        if (addComma == false)
                                        {
                                            course.Author += HttpUtility.HtmlDecode(item.InnerText.Trim());
                                            addComma = true;
                                        }
                                        else
                                        {
                                            course.Author += ", " + HttpUtility.HtmlDecode(item.InnerText.Trim());
                                        }
                                    }
                                }
                            }
                            else if (td.Attributes["class"].Value.Contains("level"))
                            {
                                course.Level = td.InnerText.Trim();
                            }
                            else if (td.Attributes["class"].Value.Contains("duration"))
                            {
                                var dur = td.InnerText.Trim();
                                dur = dur.Substring(1, dur.Length - 2);
                                var durComponents = dur.Split(':');
                                course.Duration = new TimeSpan(int.Parse(durComponents[0]), int.Parse(durComponents[1]), int.Parse(durComponents[2]));
                            }
                            else if (td.Attributes["class"].Value.Contains("releaseDate"))
                            {
                                var releaseStr = td.InnerText.Trim();
                                var releaseComponents = releaseStr.Split(' ');
                                course.ReleaseDate = new DateTime(int.Parse(releaseComponents[2]), MapMonth(releaseComponents[1]), int.Parse(releaseComponents[0]));
                            }
                        }
                    }

                    course.ParentCateogry = categoryName;

                    if (_startDate.HasValue)
                    {
                        if (course.ReleaseDate >= _startDate.Value)
                        {
                            courses.Add(course);
                        }
                    }
                    else
                    {
                        //Add all courses
                        courses.Add(course);
                    }

                }
            }

            var orderedList = courses.OrderBy(c => c.ReleaseDate).ToList();
            return orderedList;
        }