public async Task <string> CreatePreviewEditPost(ForumReplyEntity forumReplyEntity) { if (forumReplyEntity == null) { return(string.Empty); } var form = new MultipartFormDataContent { { new StringContent("updatepost"), "action" }, { new StringContent(forumReplyEntity.PostId.ToString()), "postid" }, { new StringContent(HtmlEncode(forumReplyEntity.Message)), "message" }, { new StringContent(forumReplyEntity.ParseUrl.ToString()), "parseurl" }, { new StringContent("2097152"), "MAX_FILE_SIZE" }, { new StringContent("Preview Post"), "preview" } }; HttpResponseMessage response = await _webManager.PostFormData(Constants.EDIT_POST, form); Stream stream = await response.Content.ReadAsStreamAsync(); using (var reader = new StreamReader(stream)) { string html = reader.ReadToEnd(); var doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNode[] replyNodes = doc.DocumentNode.Descendants("div").ToArray(); HtmlNode previewNode = replyNodes.FirstOrDefault(node => node.GetAttributeValue("class", "").Equals("inner postbody")); return(previewNode == null ? string.Empty : FixPostHtml(previewNode.OuterHtml)); } }
public async Task CreateReplyPreview(ForumReplyEntity forumReplyEntity, bool isEdit) { IsLoading = true; var replyManager = new ReplyManager(); string result; if (isEdit) { result = await replyManager.CreatePreviewEditPost(forumReplyEntity); } else { result = await replyManager.CreatePreviewPost(forumReplyEntity); } try { if (!string.IsNullOrEmpty(result)) { Html = result; } else { string messageText = string.Format( "No text?! What good is showing you a preview then! Type something in and try again!{0}{1}", Environment.NewLine, Constants.Ascii2); var msgDlg = new MessageDialog(messageText); await msgDlg.ShowAsync(); } } catch (Exception ex) { AwfulDebugger.SendMessageDialogAsync("Failed to get the preview html", ex); } IsLoading = false; }
public async Task <bool> Initialize(string jsonObjectString) { IsLoading = true; long threadId = 0; var replyManager = new ReplyManager(); try { _forumThread = JsonConvert.DeserializeObject <ForumThreadEntity>(jsonObjectString); } catch (Exception) { threadId = Convert.ToInt64(jsonObjectString); } try { if (_forumThread != null) { ForumReplyEntity = await replyManager.GetReplyCookies(_forumThread); } else { ForumReplyEntity = await replyManager.GetReplyCookies(threadId); } } catch (Exception ex) { ForumReplyEntity = null; } IsLoading = false; return(ForumReplyEntity != null); }
public async Task <bool> InitializeEdit(string jsonObjectString) { IsLoading = true; long threadId = Convert.ToInt64(jsonObjectString);; try { var replyManager = new ReplyManager(); ForumReplyEntity = await replyManager.GetReplyCookiesForEdit(threadId); } catch (Exception ex) { AwfulDebugger.SendMessageDialogAsync("You can't edit this post!", ex); } IsLoading = false; return(ForumReplyEntity != null); }
/// <summary> /// Populates the page with content passed during navigation. Any saved state is also /// provided when recreating a page from a prior session. /// </summary> /// <param name="sender"> /// The source of the event; typically <see cref="NavigationHelper" /> /// </param> /// <param name="e"> /// Event data that provides both the navigation parameter passed to /// <see cref="Frame.Navigate(Type, Object)" /> when this page was initially requested and /// a dictionary of state preserved by this page during an earlier /// session. The state will be null the first time a page is visited. /// </param> private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e) { LoadingProgressBar.Visibility = Visibility.Visible; var jsonObjectString = (string)e.NavigationParameter; long threadId = Convert.ToInt64(jsonObjectString); _forumReply = await _replyManager.GetReplyCookiesForEdit(threadId); if (_forumReply == null) { var msgDlg = new MessageDialog("You can't edit this post!"); await msgDlg.ShowAsync(); Frame.GoBack(); return; } ReplyText.Text = _forumReply.Quote; PreviewLastPostWebView.NavigateToString(_forumReply.PreviousPostsRaw); LoadingProgressBar.Visibility = Visibility.Collapsed; }
public async Task <bool> SendUpdatePost(ForumReplyEntity forumReplyEntity) { if (forumReplyEntity == null) { return(false); } var form = new MultipartFormDataContent { { new StringContent("updatepost"), "action" }, { new StringContent(forumReplyEntity.PostId.ToString()), "postid" }, { new StringContent(HtmlEncode(forumReplyEntity.Message)), "message" }, { new StringContent(forumReplyEntity.ParseUrl.ToString()), "parseurl" }, { new StringContent(forumReplyEntity.Bookmark), "bookmark" }, { new StringContent("2097152"), "MAX_FILE_SIZE" }, { new StringContent("Save Changes"), "submit" } }; HttpResponseMessage response = await _webManager.PostFormData(Constants.EDIT_POST, form); return(response.IsSuccessStatusCode); }
public async Task <bool> SendPost(ForumReplyEntity forumReplyEntity) { if (forumReplyEntity == null) { return(false); } var form = new MultipartFormDataContent { { new StringContent("postreply"), "action" }, { new StringContent(forumReplyEntity.ThreadId), "threadid" }, { new StringContent(forumReplyEntity.FormKey), "formkey" }, { new StringContent(forumReplyEntity.FormCookie), "form_cookie" }, { new StringContent(HtmlEncode(forumReplyEntity.Message)), "message" }, { new StringContent(forumReplyEntity.ParseUrl.ToString()), "parseurl" }, { new StringContent("2097152"), "MAX_FILE_SIZE" }, { new StringContent("Submit Reply"), "submit" } }; HttpResponseMessage response = await _webManager.PostFormData(Constants.NEW_REPLY, form); return(response.IsSuccessStatusCode); }
public async Task <string> CreatePreviewPost(ForumReplyEntity forumReplyEntity) { if (forumReplyEntity == null) { return(string.Empty); } var form = new MultipartFormDataContent { { new StringContent("postreply"), "action" }, { new StringContent(forumReplyEntity.ThreadId), "threadid" }, { new StringContent(forumReplyEntity.FormKey), "formkey" }, { new StringContent(forumReplyEntity.FormCookie), "form_cookie" }, { new StringContent(HtmlEncode(forumReplyEntity.Message)), "message" }, { new StringContent(forumReplyEntity.ParseUrl.ToString()), "parseurl" }, { new StringContent("2097152"), "MAX_FILE_SIZE" }, { new StringContent("Submit Reply"), "submit" }, { new StringContent("Preview Reply"), "preview" } }; // We post to SA the same way we would for a normal reply, but instead of getting a redirect back to the // thread, we'll get redirected to back to the reply screen with the preview message on it. // From here we can parse that preview and return it to the user. HttpResponseMessage response = await _webManager.PostFormData(Constants.NEW_REPLY, form); Stream stream = await response.Content.ReadAsStreamAsync(); using (var reader = new StreamReader(stream)) { string html = reader.ReadToEnd(); var doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNode[] replyNodes = doc.DocumentNode.Descendants("div").ToArray(); HtmlNode previewNode = replyNodes.FirstOrDefault(node => node.GetAttributeValue("class", "").Equals("inner postbody")); return(previewNode == null ? string.Empty : FixPostHtml(previewNode.OuterHtml)); } }
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); } }
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); } }