public async Task <User> GetUserFromProfilePage(long userId, bool parseToJson = true) { string url = EndPoints.BaseUrl + string.Format(EndPoints.UserProfile, userId); var result = await _webManager.GetData(url); if (!result.IsSuccess || !parseToJson) { throw new Exception($"Failed to parse user: {result.ResultHtml}"); } try { var doc = new HtmlDocument(); doc.LoadHtml(result.ResultHtml); /*Get the user profile HTML from the user profile page, * once we get it, get the nodes for each section of the page and parse them.*/ HtmlNode profileNode = doc.DocumentNode.Descendants("td") .FirstOrDefault(node => node.GetAttributeValue("class", string.Empty).Contains("info")); HtmlNode threadNode = doc.DocumentNode.Descendants("td") .FirstOrDefault(node => node.GetAttributeValue("id", string.Empty).Contains("thread")); return(ParseFromUserProfile(profileNode, threadNode)); } catch (Exception ex) { throw new Exception("Failed to parse user", ex); } }
public async Task <Result> GetPrivateMessagesAsync(int page) { var privateMessageEntities = new List <PrivateMessage>(); var url = EndPoints.PrivateMessages; if (page > 0) { url = EndPoints.PrivateMessages + string.Format(EndPoints.PageNumber, page); } var result = await _webManager.GetData(url); try { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(result.ResultHtml); HtmlNode forumNode = doc.DocumentNode.Descendants("tbody").FirstOrDefault(); foreach ( HtmlNode threadNode in forumNode.Descendants("tr")) { var threadEntity = new PrivateMessage(); try { Parse(threadEntity, threadNode); } catch (Exception ex) { throw new Exception("Failed to parse private message list", ex); } privateMessageEntities.Add(threadEntity); } result.ResultJson = JsonConvert.SerializeObject(privateMessageEntities); return(result); } catch (Exception ex) { ErrorHandler.CreateErrorObject(result, ex.Message, ex.StackTrace); return(result); } }
public async Task <HtmlDocument> GetThreadInfo(Thread forumThread, string url) { var result = await _webManager.GetData(url); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(result.ResultHtml); try { ParseFromThread(forumThread, doc); } catch (Exception exception) { return(doc); } try { var usernameNode = doc.DocumentNode.Descendants("div").FirstOrDefault(node => node.GetAttributeValue("id", string.Empty).Equals("loggedinusername")); forumThread.LoggedInUserName = usernameNode != null ? usernameNode.InnerText : string.Empty; 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(new Uri(url).Query); if (query.ContainsKey("pagenumber")) { forumThread.CurrentPage = Convert.ToInt32(query["pagenumber"]); } } catch (Exception exception) { return(doc); } return(doc); }
public async Task <Result> GetBookmarksAsync(int page) { var forumThreadList = new List <Thread>(); var forum = new Forum() { Name = "Bookmarks", IsSubforum = false, Location = EndPoints.UserCp }; String url = EndPoints.BookmarksUrl; if (page >= 0) { url = EndPoints.BookmarksUrl + string.Format(EndPoints.PageNumber, page); } var result = (await _webManager.GetData(url)); var doc = new HtmlDocument(); doc.LoadHtml(result.ResultHtml); HtmlNode forumNode = doc.DocumentNode.Descendants() .FirstOrDefault(node => node.GetAttributeValue("class", string.Empty).Contains("threadlist")); foreach ( HtmlNode threadNode in forumNode.Descendants("tr") .Where(node => node.GetAttributeValue("class", string.Empty).StartsWith("thread"))) { var threadEntity = new Thread { ForumId = 0, IsBookmark = true }; ParseThreadHtml(threadEntity, threadNode); forumThreadList.Add(threadEntity); } result.ResultJson = JsonConvert.SerializeObject(forumThreadList); return(result); }
public async Task <IEnumerable <PostIconCategory> > GetPostIcons(int forumId) { string url = string.Format(EndPoints.NewThread, forumId); var result = await _webManager.GetData(url); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(result.ResultHtml); HtmlNode[] pageNodes = doc.DocumentNode.Descendants("div").Where(node => node.GetAttributeValue("class", string.Empty).Equals("posticon")).ToArray(); var postIconEntityList = new List <PostIcon>(); foreach (var pageNode in pageNodes) { var postIconEntity = new PostIcon(); Parse(postIconEntity, pageNode); postIconEntityList.Add(postIconEntity); } var postIconCategoryEntity = new PostIconCategory("Post Icon", postIconEntityList); var postIconCategoryList = new List <PostIconCategory> { postIconCategoryEntity }; return(postIconCategoryList); }
public async Task <List <SmileCategory> > GetSmileList() { var smileCategoryList = new List <SmileCategory>(); try { //inject this var result = await _webManager.GetData(EndPoints.SmileUrl); var doc = new HtmlDocument(); doc.LoadHtml(result.ResultHtml); IEnumerable <HtmlNode> smileCategoryTitles = doc.DocumentNode.Descendants("div") .FirstOrDefault(node => node.GetAttributeValue("class", string.Empty).Contains("inner")) .Descendants("h3"); List <string> categoryTitles = smileCategoryTitles.Select(smileCategoryTitle => WebUtility.HtmlDecode(smileCategoryTitle.InnerText)) .ToList(); IEnumerable <HtmlNode> smileNodes = doc.DocumentNode.Descendants("ul") .Where(node => node.GetAttributeValue("class", string.Empty).Contains("smilie_group")); int smileCount = 0; foreach (HtmlNode smileNode in smileNodes) { var smileList = new List <Smile>(); IEnumerable <HtmlNode> smileIcons = smileNode.Descendants("li"); foreach (HtmlNode smileIcon in smileIcons) { var smileEntity = new Smile(); smileEntity.Parse(smileIcon); smileList.Add(smileEntity); } smileCategoryList.Add(new SmileCategory() { Name = categoryTitles[smileCount], SmileList = smileList }); smileCount++; } } catch (Exception) { // TODO: Add error handling. For whatever reason serializing the list returns null? } return(smileCategoryList); }
public async Task <Result> GetForumCategoriesAsync(bool parseToJson = true) { var result = new Result(); try { result = await _webManager.GetData(EndPoints.ForumListPage); } catch (Exception ex) { ErrorHandler.CreateErrorObject(result, "Failed to download forum list", ex.StackTrace, ex.GetType().FullName); } if (!result.IsSuccess) { return(result); } // Got the forum list HTML! result.Type = typeof(Category).ToString(); if (!parseToJson) { return(result); } try { result.ResultJson = ParseCategoryList(result.ResultHtml); } catch (Exception ex) { ErrorHandler.CreateErrorObject(result, "Failed to parse forum list", ex.StackTrace, ex.GetType().FullName); } return(result); }
public async Task <ForumReply> GetReplyCookiesForEdit(long postId) { try { string url = string.Format(EndPoints.EditBase, postId); var result = await _webManager.GetData(url); var doc = new HtmlDocument(); doc.LoadHtml(result.ResultHtml); 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(_webManager); //Get previous posts from quote page. string url2 = string.Format(EndPoints.QuoteBase, postId); var result2 = await _webManager.GetData(url2); HtmlDocument doc2 = new HtmlDocument(); doc2.LoadHtml(result2.ResultHtml); var forumThreadPosts = new List <Post>(); HtmlNode threadNode = doc2.DocumentNode.Descendants("div") .FirstOrDefault(node => node.GetAttributeValue("id", string.Empty).Contains("thread")); var postManager = new PostManager(_webManager); foreach ( HtmlNode postNode in threadNode.Descendants("table") .Where(node => node.GetAttributeValue("class", string.Empty).Contains("post"))) { var post = new Post(); postManager.ParsePost(post, postNode); forumThreadPosts.Add(post); } var forumReplyEntity = new ForumReply(); try { string quote = WebUtility.HtmlDecode(textNode.InnerText); forumReplyEntity.ForumPosts = forumThreadPosts; string bookmark = bookmarkNode.OuterHtml.Contains("checked") ? "yes" : "no"; forumReplyEntity.MapEditPostInformation(quote, postId, bookmark); return(forumReplyEntity); } catch (Exception) { return(null); } } catch (Exception) { return(null); } }