Esempio n. 1
0
        public async Task <ForumThreadEntity> GetThread(string url)
        {
            var forumThread = new ForumThreadEntity();

            WebManager.Result result = await _webManager.GetData(url);

            HtmlDocument doc = result.Document;

            try
            {
                forumThread.ParseFromThread(doc);
            }
            catch (Exception)
            {
                return(null);
            }
            var query = Extensions.ParseQueryString(url);

            if (!query.ContainsKey("postid"))
            {
                return(forumThread);
            }

            // If we are going to a post, it won't use #pti but instead uses the post id.

            forumThread.ScrollToPost       = Convert.ToInt32(query["postid"]);
            forumThread.ScrollToPostString = "#post" + query["postid"];
            return(forumThread);
        }
Esempio n. 2
0
        public async Task <IEnumerable <PostIconEntity> > GetPostIconList(ForumEntity forum)
        {
            string url = string.Format(Constants.NEW_THREAD, forum.ForumId);

            WebManager.Result result = await _webManager.GetData(url);

            HtmlDocument doc = result.Document;

            HtmlNode[] pageNodes          = doc.DocumentNode.Descendants("div").Where(node => node.GetAttributeValue("class", string.Empty).Equals("posticon")).ToArray();
            var        postIconEntityList = new List <PostIconEntity>();

            foreach (var pageNode in pageNodes)
            {
                var postIconEntity = new PostIconEntity();
                postIconEntity.Parse(pageNode);
                postIconEntityList.Add(postIconEntity);
            }
            return(postIconEntityList);
        }
Esempio n. 3
0
        public async Task <HtmlDocument> GetThread(ForumThreadEntity forumThread, string url)
        {
            WebManager.Result result = await _webManager.GetData(url);

            HtmlDocument doc = result.Document;

            try
            {
                forumThread.ParseFromThread(doc);
            }
            catch (Exception)
            {
                return(null);
            }
            string responseUri = result.AbsoluteUri;

            string[] test = responseUri.Split('#');
            if (test.Length > 1 && test[1].Contains("pti"))
            {
                forumThread.ScrollToPost       = Int32.Parse(Regex.Match(responseUri.Split('#')[1], @"\d+").Value) - 1;
                forumThread.ScrollToPostString = string.Concat("#", responseUri.Split('#')[1]);
            }

            var query = Extensions.ParseQueryString(url);

            if (query.ContainsKey("pagenumber"))
            {
                forumThread.CurrentPage = Convert.ToInt32(query["pagenumber"]);
            }

            if (!query.ContainsKey("postid"))
            {
                return(doc);
            }

            // If we are going to a post, it won't use #pti but instead uses the post id.

            forumThread.ScrollToPost       = Convert.ToInt32(query["postid"]);
            forumThread.ScrollToPostString = "#postId" + query["postid"];

            return(doc);
        }
Esempio n. 4
0
        public async Task <string> GetQuoteString(long postId)
        {
            string url = string.Format(Constants.QUOTE_BASE, postId);

            WebManager.Result result = await _webManager.GetData(url);

            HtmlDocument doc = result.Document;

            HtmlNode[] textAreaNodes = doc.DocumentNode.Descendants("textarea").ToArray();

            HtmlNode textNode =
                textAreaNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("message"));

            try
            {
                // TODO: Figure out why in the hell we have to decode the HTML twice for Unicode to render properly.
                return(WebUtility.HtmlDecode(WebUtility.HtmlDecode(textNode.InnerText)));
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Could not parse newReply form data.");
            }
        }
Esempio n. 5
0
        public async Task <IEnumerable <PostIconCategoryEntity> > GetPmPostIcons()
        {
            string url = Constants.NEW_PRIVATE_MESSAGE;

            WebManager.Result result = await _webManager.GetData(url);

            HtmlDocument doc = result.Document;

            HtmlNode[] pageNodes          = doc.DocumentNode.Descendants("div").Where(node => node.GetAttributeValue("class", string.Empty).Equals("posticon")).ToArray();
            var        postIconEntityList = new List <PostIconEntity>();

            foreach (var pageNode in pageNodes)
            {
                var postIconEntity = new PostIconEntity();
                postIconEntity.Parse(pageNode);
                postIconEntityList.Add(postIconEntity);
            }
            var postIconCategoryEntity = new PostIconCategoryEntity("Post Icon", postIconEntityList);
            var postIconCategoryList   = new List <PostIconCategoryEntity> {
                postIconCategoryEntity
            };

            return(postIconCategoryList);
        }
Esempio n. 6
0
        public async Task <NewThreadEntity> GetThreadCookies(long forumId)
        {
            try
            {
                string            url    = string.Format(Constants.NEW_THREAD, forumId);
                WebManager.Result result = await _webManager.GetData(url);

                HtmlDocument doc = result.Document;

                HtmlNode[] formNodes = doc.DocumentNode.Descendants("input").ToArray();

                HtmlNode formKeyNode =
                    formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("formkey"));

                HtmlNode formCookieNode =
                    formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("form_cookie"));

                var newForumEntity = new NewThreadEntity();
                try
                {
                    string formKey    = formKeyNode.GetAttributeValue("value", "");
                    string formCookie = formCookieNode.GetAttributeValue("value", "");
                    newForumEntity.FormKey    = formKey;
                    newForumEntity.FormCookie = formCookie;
                    return(newForumEntity);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Could not parse new thread form data.");
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Esempio n. 7
0
        public async Task <ForumReplyEntity> GetReplyCookies(ForumThreadEntity forumThread)
        {
            try
            {
                string            url    = string.Format(Constants.REPLY_BASE, forumThread.ThreadId);
                WebManager.Result result = await _webManager.GetData(url);

                HtmlDocument doc = result.Document;

                HtmlNode[] formNodes = doc.DocumentNode.Descendants("input").ToArray();

                HtmlNode formKeyNode =
                    formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("formkey"));

                HtmlNode formCookieNode =
                    formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("form_cookie"));

                HtmlNode bookmarkNode =
                    formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("bookmark"));

                HtmlNode[] textAreaNodes = doc.DocumentNode.Descendants("textarea").ToArray();

                HtmlNode textNode =
                    textAreaNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("message"));

                HtmlNode threadIdNode =
                    formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("threadid"));

                var threadManager = new ThreadManager();

                var forumThreadPosts = new ObservableCollection <ForumPostEntity>();

                HtmlNode threadNode =
                    doc.DocumentNode.Descendants("div")
                    .FirstOrDefault(node => node.GetAttributeValue("id", string.Empty).Contains("thread"));


                foreach (
                    HtmlNode postNode in
                    threadNode.Descendants("table")
                    .Where(node => node.GetAttributeValue("class", string.Empty).Contains("post")))
                {
                    var post = new ForumPostEntity();
                    post.Parse(postNode);
                    forumThreadPosts.Add(post);
                }

                forumThread.ForumPosts = forumThreadPosts;

                string htmlThread = await HtmlFormater.FormatThreadHtml(forumThread);

                var forumReplyEntity = new ForumReplyEntity();
                try
                {
                    string formKey    = formKeyNode.GetAttributeValue("value", "");
                    string formCookie = formCookieNode.GetAttributeValue("value", "");
                    string quote      = WebUtility.HtmlDecode(textNode.InnerText);
                    string threadId   = threadIdNode.GetAttributeValue("value", "");
                    forumReplyEntity.MapThreadInformation(formKey, formCookie, quote, threadId);
                    forumReplyEntity.PreviousPostsRaw = htmlThread;
                    return(forumReplyEntity);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Could not parse newReply form data.");
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Esempio n. 8
0
        public async Task <ForumReplyEntity> GetReplyCookiesForEdit(long postId)
        {
            try
            {
                string            url    = string.Format(Constants.EDIT_BASE, postId);
                WebManager.Result result = await _webManager.GetData(url);

                HtmlDocument doc = result.Document;

                HtmlNode[] formNodes = doc.DocumentNode.Descendants("input").ToArray();

                HtmlNode bookmarkNode =
                    formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("bookmark"));

                HtmlNode[] textAreaNodes = doc.DocumentNode.Descendants("textarea").ToArray();

                HtmlNode textNode =
                    textAreaNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("message"));

                var threadManager = new ThreadManager();

                //Get previous posts from quote page.
                string            url2    = string.Format(Constants.QUOTE_BASE, postId);
                WebManager.Result result2 = await _webManager.GetData(url2);

                HtmlDocument doc2 = result2.Document;

                var forumThreadPosts = new ObservableCollection <ForumPostEntity>();

                HtmlNode threadNode =
                    doc2.DocumentNode.Descendants("div")
                    .FirstOrDefault(node => node.GetAttributeValue("id", string.Empty).Contains("thread"));


                foreach (
                    HtmlNode postNode in
                    threadNode.Descendants("table")
                    .Where(node => node.GetAttributeValue("class", string.Empty).Contains("post")))
                {
                    var post = new ForumPostEntity();
                    post.Parse(postNode);
                    forumThreadPosts.Add(post);
                }

                ForumThreadEntity threadEntity = new ForumThreadEntity()
                {
                    ForumPosts = forumThreadPosts
                };

                string htmlThread = await HtmlFormater.FormatThreadHtml(threadEntity);

                var forumReplyEntity = new ForumReplyEntity();
                try
                {
                    string quote = WebUtility.HtmlDecode(textNode.InnerText);
                    forumReplyEntity.PreviousPostsRaw = htmlThread;
                    string bookmark = bookmarkNode.OuterHtml.Contains("checked") ? "yes" : "no";
                    forumReplyEntity.MapEditPostInformation(quote, postId, bookmark);
                    return(forumReplyEntity);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Could not parse newReply form data.");
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }