Пример #1
0
        public static async Task <List <ICourseLink> > ExtractCourses()
        {
            await CoursesClient.LazyRefresh();

            var coursesPageText = await CoursesClient.SessionClient.GetStringAsyncHttp(CoursesProfileAllCoursesUrl);

            CoursesClient.FindSessKey(coursesPageText);
            var doc = new HtmlDocument();

            doc.LoadHtml(coursesPageText);
            var coursesLinksNodes = doc.DocumentNode.SelectNodes(XPathFilterProfileCoursesLinks);

            var coursesLinksTasks = coursesLinksNodes
                                    .Where(l => Regex.IsMatch(l.InnerText, CurrentSemesterCourseLongFormNameRegex))
                                    .Select(async(l, _) =>
            {
                var longName = ExtractName(l);
                var url      = ExtractHref(l);

                string shortName;
                using (var courseHtml = await CoursesClient.SessionClient.GetStreamAsyncHttp(url))
                {
                    shortName = LazyHtmlParser.FindShortNameInHtml(courseHtml);
                    shortName = CleanName(shortName, true);
                }

                return(new CourseLink($"{shortName,-8}{longName}", url));
            });

            var coursesLinks = await Task.WhenAll(coursesLinksTasks);

            SharedVars.Courses = coursesLinks.ToList <ICourseLink>();

            return(SharedVars.Courses);
        }
        private async Task ExtractExternalUrlAndTitle()
        {
            await CoursesClient.LazyRefresh();

            using (var html = await CoursesClient.SessionClient.GetStreamAsyncHttp(Url))
            {
                var title = LazyHtmlParser.FindTitleInHtml(html);
                Title = title;

                IsTitleExtracted = true;
            }
        }
Пример #3
0
        private async Task ExtractExternalUrlAndTitle()
        {
            await CoursesClient.LazyRefresh();

            // Go to url as clicked on courses
            using (var coursesResponse = await CoursesClient.SessionClient.GetHeadersAsyncHttp(Url))
            {
                var externalResponse = coursesResponse;

                // if redirected to courses, grab url workaround, get title from there
                if (coursesResponse.RequestMessage.RequestUri.Host == CoursesClient.SessionClient.BaseAddress.Host)
                {
                    using (var coursesHtml = await coursesResponse.Content.ReadAsStreamAsync())
                    {
                        var urlWorkaround = LazyHtmlParser.FindUrlWorkaroundInHtml(coursesHtml);
                        ExternalUrl = urlWorkaround;

                        // Go to url as clicked on workaround url
                        externalResponse = await CoursesClient.SessionClient.GetHeadersAsyncHttp(urlWorkaround);
                    }
                }
                // else redirected to external link, just save it
                else
                {
                    var url = coursesResponse.RequestMessage.RequestUri.ToString();
                    ExternalUrl = url;
                }

                using (externalResponse)
                {
                    using (var externalHtml = await externalResponse.Content.ReadAsStreamAsync())
                    {
                        var title = LazyHtmlParser.FindTitleInHtml(externalHtml);
                        Title = title;

                        AreExternalUrlAndTitleExtracted = true;
                    }
                }
            }
        }