public static AwfulPost Build(HtmlNode node, AwfulThreadPage page) { AwfulPost result = null; Logger.AddEntry("AwfulPost - Parsing postNode for html..."); int id = Factory.ParsePostID(node); try { result = new AwfulPost(); result.ID = id; result.ThreadPageID = page.ID; Factory.ParsePostThreadIndex(result, node); Factory.ParseAuthor(result, node); Factory.ParsePostDate(result, node); Factory.ParseUserID(result, node); result.ContentNode = Factory.ParseContent(node); Factory.ParseHasSeen(result, node); Factory.ParseIcon(result, node); } catch (Exception ex) { string error = string.Format("An error occured while processing the post to the database. [{0}] {1}", ex.Message, ex.StackTrace); Logger.AddEntry(error); } return result; }
private string BuildPreviewHtml(HtmlNode node) { AwfulPost post = new AwfulPost(); post.PostAuthor = "Post Preview"; post.PostDate = DateTime.Now; post.ShowPostIcon = false; post.PostIndex = 1; var previewContentNode = node.Descendants("div") .Where(n => n.GetAttributeValue("class", "").Contains("postbody")) .FirstOrDefault(); if (previewContentNode != null) { post.ContentNode = previewContentNode; this.Posts.Add(post); } string content = AwfulThreadPageHtmlFactory.Metrofy(this); return content; }
private void ParseAuthor(AwfulPost post, HtmlNode postNode) { var authorNode = postNode.Descendants() .Where(node => (node.GetAttributeValue("class", "").Equals("author")) || (node.GetAttributeValue("title", "").Equals("Administrator")) || (node.GetAttributeValue("title", "").Equals("Moderator"))) .FirstOrDefault(); if (authorNode != null) { var type = authorNode.GetAttributeValue("title", ""); switch (type) { case "Administrator": post.AccountType = Models.AccountType.ADMIN; break; case "Moderator": post.AccountType = Models.AccountType.MODERATOR; break; default: post.AccountType = Models.AccountType.NORMAL; break; } post.PostAuthor = authorNode.InnerText; } else { post.PostAuthor = "AwfulPoster"; post.AccountType = Models.AccountType.NORMAL; } }
private static string AppendPostAuthor(AwfulPost post) { string style = string.Empty; switch (post.AccountType) { case AccountType.ADMIN: style = "admin_post"; break; case AccountType.MODERATOR: style = "mod_post"; break; case AccountType.NORMAL: style = "user_post"; break; } return string.Format("<span class='text_title3style'><span class='{0}'>{1}</span></span><br/>", style, post.PostAuthor); }
private void ParseUserID(AwfulPost post, HtmlNode postNode) { var userIDNode = postNode.Descendants() .Where(node => node.GetAttributeValue("class", "").Contains("userid")) .FirstOrDefault(); if (userIDNode != null) { string value = userIDNode.GetAttributeValue("class", ""); value = value.Replace("userinfo userid-", ""); int userid = 0; int.TryParse(value, out userid); post.UserID = userid; } }
private void ParsePostThreadIndex(AwfulPost post, HtmlNode postNode) { var seenUrlNode = postNode.Descendants("a") .Where(node => node.GetAttributeValue("title", "").Contains("Mark thread")) .FirstOrDefault(); if (seenUrlNode == null) { post.PostIndex = AwfulPost.UNKNOWN_POST_INDEX; } else { // make sure the string is in the right format so the uri class can parse correctly. var nodeValue = seenUrlNode.GetAttributeValue("href", ""); int index = -1; string indexValue = nodeValue.Split('&').LastOrDefault(); if (indexValue != null) { indexValue = indexValue.Split('=').Last(); post.PostIndex = int.TryParse(indexValue, out index) ? index : AwfulPost.UNKNOWN_POST_INDEX; } } }
private void ParsePostDate(AwfulPost post, HtmlNode postNode) { var postDateNode = postNode.Descendants() .Where(node => node.GetAttributeValue("class", "").Equals("postdate")) .FirstOrDefault(); var postDateString = postDateNode == null ? string.Empty : postDateNode.InnerText; post.PostDate = postDateNode == null ? default(DateTime) : Convert.ToDateTime(postDateString.SanitizeDateTimeHTML()); }
private void ParseIcon(AwfulPost post, HtmlNode postNode) { try { post.PostIconUri = postNode.Descendants() .Where(node => node.GetAttributeValue("class", "").Equals("title")) .First() .Descendants("img") .First() .GetAttributeValue("src", ""); post.ShowPostIcon = true; } catch (Exception) { post.PostIconUri = null; post.ShowPostIcon = false; } }
private void ParseHasSeen(AwfulPost post, HtmlNode postNode) { var hasSeenMarker = postNode.Descendants("tr") .Where(node => node.GetAttributeValue("class", "").Contains(Constants.LASTREAD_FLAG)) .FirstOrDefault(); var hasNotSeenMarker = postNode.Descendants("img") .Where(node => node.GetAttributeValue("src", "") .Equals(Constants.NEWPOST_GIF_URL)).FirstOrDefault(); bool firstGuess = hasSeenMarker != null; bool secondGuess = hasNotSeenMarker == null; post.HasSeen = firstGuess || secondGuess; }
public void TestGeneralThreadIntegration() { // make assertions based on test initialization var context = AwfulDataContext.CreateDataContext(AwfulTestService.TEST_FILENAME); Assert.IsTrue(context.Threads.Count() == 2); Assert.IsTrue(context.ThreadPages.Count() == 6); var thread = context.Threads.Where(t => t.ID == thread1.ID).SingleOrDefault(); Assert.IsNotNull(thread); Assert.AreEqual("test forum", thread.Forum.ForumName); Assert.AreEqual(5, thread.Pages.Count); Assert.AreEqual("author", thread.Author); // let's add a post var page = thread.Pages[0]; var pageID = page.ID; // in order to relate thread page : post, create post, add post to page, // insert post to page on submit, then submit changes var post = new AwfulPost() { ID = 1, PostAuthor = "post author" }; page.Posts.Add(post); Assert.IsTrue(page.Posts.Count > 0); context.Posts.InsertOnSubmit(post); context.SubmitChanges(); context.Dispose(); context = AwfulDataContext.CreateDataContext(AwfulTestService.TEST_FILENAME); // did the post persist? int actualPostCount = context.Posts.Count(); Assert.AreEqual(1, actualPostCount); // is the thread persisted? thread = context.Threads.Where(t => t.ID == thread.ID).SingleOrDefault(); Assert.IsNotNull(thread); // is the page releated to the thread? page = thread.Pages.Where(p => p.ID == pageID).SingleOrDefault(); post = context.Posts.FirstOrDefault(); Assert.IsNotNull(post); Assert.IsNotNull(page); Assert.IsTrue(page.Posts.Count > 0); // is the post releated to the page? post = page.Posts[0]; Assert.IsNotNull(post); Assert.AreEqual(page, post.ThreadPage); }
private void OnPostRemoved(AwfulPost post) { post.ThreadPage = null; }
private void OnPostAdded(AwfulPost post) { post.ThreadPage = this; }
public void MarkThreadToPostAsync(AwfulPost post, Action<ActionResult> result) { if (Logger.IsEnabled) { Logger.AddEntry(string.Format("MarkThreadToPost - PostID: {0}", post.ID)); } var markUrl = post.MarkSeenUrl; RunURLTaskAsync(markUrl, result); }