예제 #1
0
        public async Task <Post> getPost()
        {
            Post resultPost = new Post();

            if (rootNode == null)
            {
                return(new Post());
            }

            var article       = rootNode.SelectSingleNode("//article");
            var articleHeader = rootNode.SelectSingleNode("//header");
            var articleFooter = rootNode.SelectSingleNode("//footer");

            resultPost.id = postId;

            resultPost.title = article.SelectSingleNode(".//h1[@class='topic-title word-wrap']").InnerText;

            resultPost.author = article.SelectSingleNode(".//a[@rel='author']").InnerText;

            await resultPost.author_image.SetBitmapAsync(
                await webManager.getCachedImageAsync(
                    normalizeImageUriDebug(
                        article.SelectSingleNode(".//img[@class='avatar']")
                        .Attributes["src"].Value)));

            var blogNode = article.SelectSingleNode(".//a[@class='topic-blog'] | .//a[@class='topic-blog private-blog']");

            resultPost.blog = blogNode.InnerText;

            resultPost.blog_id = UriParser.getLastPart(blogNode
                                                       .Attributes["href"].Value);

            resultPost.rating     = article.SelectSingleNode(".//div[@class='vote-item vote-count']").InnerText;
            resultPost.votesTotal = article.SelectSingleNode(".//div[@class='vote-item vote-count']")
                                    .Attributes["title"].Value;

            resultPost.text = await htmlParser.convertNodeToParagraph(
                article.SelectSingleNode(".//div[@class='topic-content text']"));

            resultPost.datatime = article.SelectSingleNode(".//time").InnerText.Replace("\n", String.Empty).Replace("\t", String.Empty);

            foreach (HtmlNode node in article.SelectNodes(".//a[@rel='tag']"))
            {
                resultPost.tags += HtmlEntity.DeEntitize(node.InnerText) + " ";
            }

            resultPost.commentsCount = rootNode.SelectSingleNode(".//span[@id='count-comments']").InnerText;

            var new_comments_counter = rootNode.SelectSingleNode(".//div[@id='new_comments_counter']");

            if (new_comments_counter != null)
            {
                int.TryParse(new_comments_counter.Attributes["data-id-comment-last"].Value, out lastComment);
                resultPost.lastComment = lastComment;
            }

            return(resultPost);
        }
예제 #2
0
        public async Task <List <Post> > getPosts()
        {
            List <Post> resultPosts = new List <Post>();

            if (rootNode == null)
            {
                return(null);
            }

            var articles = rootNode.SelectNodes("//article");

            foreach (HtmlNode article in articles)
            {
                var articleHeader = article.SelectSingleNode(".//header");

                var articleTitle = articleHeader.SelectSingleNode(".//h1/a").InnerText;

                var articleUri = new Uri(articleHeader.SelectSingleNode(".//h1/a")
                                         .Attributes["href"].Value);

                var articleId = Int32.Parse(articleUri
                                            .Segments.Last()
                                            .Replace(".html", String.Empty));

                var articleRating = articleHeader.SelectSingleNode(".//span/i").InnerText;

                var articleAuthor = articleHeader.SelectSingleNode(".//a[@rel]").InnerText;

                var articleAuthorImageUri = articleHeader.SelectSingleNode(".//img")
                                            .Attributes["src"].Value;

                var articleBlog = articleHeader.SelectSingleNode(".//a[@class]").InnerText;

                var articleBlogId = articleHeader.SelectSingleNode(".//*[@class='topic-blog']")
                                    .Attributes["href"].Value;
                articleBlogId = UriParser.getLastPart(articleBlogId);

                var articleBody = await htmlParser.convertNodeToParagraph(
                    article.SelectSingleNode(".//div[@class='topic-content text']"));

                var articleFooter = article.SelectSingleNode(".//footer[@class='topic-footer']");

                var    articleTags_tmp = articleFooter.SelectNodes(".//a[@rel]");
                string articleTags     = "";
                foreach (HtmlNode node in articleTags_tmp)
                {
                    articleTags += node.InnerText + " ";
                }

                var articleDatatime = articleFooter.SelectSingleNode(".//time").InnerText;

                var articleCommentsCount = articleFooter.SelectSingleNode(".//li[@class='topic-info-comments']").InnerText.Trim();

                SoftwareBitmapSource source = new SoftwareBitmapSource();
                await source.SetBitmapAsync(
                    await webManager.getCachedImageAsync(normalizeImageUriDebug(articleAuthorImageUri)));

                resultPosts.Add(new Post
                {
                    id           = articleId,
                    title        = articleTitle,
                    author       = articleAuthor,
                    author_image = source,
                    blog         = " " + articleBlog, // Чтобы не сливался с "в блоге"
                    blog_id      = articleBlogId,
                    rating       = articleRating,
                    text         = articleBody,
                    //text = HtmlEntity.DeEntitize(articleBody).Trim(),
                    tags          = articleTags,
                    datatime      = articleDatatime,
                    commentsCount = articleCommentsCount,
                });
            }

            return(resultPosts);
        }
예제 #3
0
        public async Task <Profile> getProfile()
        {
            var profile      = rootNode.SelectSingleNode(".//div[@class='profile']");
            var profileLeft  = rootNode.SelectSingleNode(".//div[@class='profile-left']");
            var profileRight = rootNode.SelectSingleNode(".//div[@class='profile-right']");

            var profileNickname = profile.SelectSingleNode(".//h2").InnerText;

            var profileNameNode = profile.SelectSingleNode(".//p");
            var profileName     = "";

            if (profileNameNode != null)
            {
                profileName = profileNameNode.InnerText;
            }

            var profileForce  = profile.SelectSingleNode(".//div[@class='count']").InnerText.Trim();
            var profileRating = profile.SelectSingleNode(".//div[@class='vote-item vote-count']").InnerText.Trim();

            var profileVotes = profile.SelectSingleNode(".//div[@class='vote-label']").InnerText;


            // Жестокий парсинг голого HTML
            var profileAbout = new Paragraph();

            try {
                var profileAboutNodes = rootNode.SelectSingleNode(".//div[@class='profile-info-about']")
                                        .SelectSingleNode(".//div[@class='text']");
                profileAbout = await htmlParser.convertNodeToParagraph(profileAboutNodes);
            } catch (Exception)
            {
            }

            var profileDotLists = profileLeft.SelectNodes(".//ul[@class='profile-dotted-list']");

            string   profileSex                = "";
            string   profileDateOfBirdth       = "";
            string   profilePlace              = "";
            HtmlNode profileCreated            = null;
            HtmlNode profileAdministrated      = null;
            HtmlNode profileConsistsIn         = null;
            string   profileDateOfRegistration = "";
            string   profileLastVisite         = "";

            foreach (HtmlNode dottedList in profileDotLists)
            {
                var list = dottedList.SelectNodes(".//li");
                foreach (HtmlNode node in list)
                {
                    var span = node.SelectSingleNode(".//span").InnerText;
                    if (span.Contains("Пол:"))
                    {
                        profileSex = getInnerTextFromFirstDescendant(node, "strong");
                    }
                    if (span.Contains("Дата рождения:"))
                    {
                        profileDateOfBirdth = getInnerTextFromFirstDescendant(node, "strong");
                    }
                    if (span.Contains("Местоположение:"))
                    {
                        profilePlace = getInnerTextFromFirstDescendant(node, "strong").Replace("\n", String.Empty).Replace("\t", String.Empty);
                    }
                    if (span.Contains("Создал:"))
                    {
                        profileCreated = getFirstDescendant(node, "strong");
                    }
                    if (span.Contains("Администрирует:"))
                    {
                        profileAdministrated = getFirstDescendant(node, "strong");
                    }
                    if (span.Contains("Состоит в:"))
                    {
                        profileConsistsIn = getFirstDescendant(node, "strong");
                    }
                    if (span.Contains("Зарегистрирован:"))
                    {
                        profileDateOfRegistration = getInnerTextFromFirstDescendant(node, "strong");
                    }
                    if (span.Contains("Последний визит:"))
                    {
                        profileLastVisite = getInnerTextFromFirstDescendant(node, "strong");
                    }
                }
            }

            var profileBlogConsistIn = await htmlParser.convertNodeToParagraph(profileConsistsIn);

            HtmlNode      profileFriendsNode = profileLeft.SelectSingleNode(".//ul[@class='user-list-avatar']");
            List <Friend> profileFriends     = new List <Friend>();

            try
            {
                foreach (HtmlNode node in profileFriendsNode.SelectNodes(".//a"))
                {
                    var friendAvatarPath = node.SelectSingleNode(".//img")
                                           .Attributes["src"].Value;
                    var friendName = node.InnerText.Trim();
                    SoftwareBitmapSource source = new SoftwareBitmapSource();
                    await source.SetBitmapAsync(
                        await webManager.getCachedImageAsync(normalizeImageUriDebug(friendAvatarPath)));

                    profileFriends.Add(new Friend
                    {
                        avatar_100x100 = source,
                        name           = friendName,
                    });
                }
            } catch (System.NullReferenceException)
            {
            }

            var           profileContactLists = profileRight.SelectNodes(".//ul[@class='profile-contact-list']");
            List <string> profileContacts     = new List <string>();

            if (profileContactLists != null)
            {
                foreach (HtmlNode list in profileContactLists)
                {
                    foreach (HtmlNode node in list.SelectNodes(".//a"))
                    {
                        profileContacts.Add(node.InnerText);
                    }
                }
            }

            SoftwareBitmapSource profileBigPhoto = new SoftwareBitmapSource();
            await profileBigPhoto.SetBitmapAsync(
                await webManager.getCachedImageAsync(
                    normalizeImageUriDebug(
                        rootNode.SelectSingleNode(".//img[@class='profile-photo']")
                        .Attributes["src"].Value)));

            SoftwareBitmapSource profileAvatar_100x100 = new SoftwareBitmapSource();
            await profileAvatar_100x100.SetBitmapAsync(
                await webManager.getCachedImageAsync(
                    normalizeImageUriDebug(
                        rootNode.SelectSingleNode(".//img[@itemprop='photo']")
                        .Attributes["src"].Value)));

            Profile resultProfile = new Profile
            {
                nickname           = profileNickname,
                name               = profileName,
                force              = profileForce,
                rating             = profileRating,
                votes              = profileVotes,
                about              = profileAbout,
                sex                = profileSex,
                dateOfBirdth       = profileDateOfBirdth,
                place              = profilePlace,
                profile_photo      = profileBigPhoto,
                avatar_100x100     = profileAvatar_100x100,
                dateOfRegistration = profileDateOfRegistration,
                dateOfLastVisite   = profileLastVisite,
                friends            = profileFriends,
                contacts           = profileContacts,
                blogsConsistIn     = profileBlogConsistIn,
            };

            return(resultProfile);
        }