public static List<PostModel> ParsePosts(string htmlData) { var posts = new List<PostModel>(); htmlData = CleanupHtml(htmlData); var postRegex = "<div class=\"post.+?id=\"(.+?)\".+?class=\"dt\">(.+?)</div>.+?<div class=\"p\">(Написал|Написала)(.+?)<a href=\".*?/users/.+?\".*?>(.+?)</a>,(.+?)в(.+?)<span>.+?<a href=\".*?/(comments|inbox)/.+?\">(.+?)</span>.+?.+?(<div class=\"vote\".+?><em>(.+?)</em></span>|</div>)(<a href=\"#\".+?class=\"plus(.*?)\">.+?<a href=\"#\".+?class=\"minus(.*?)\">|</div>)"; var matches = Regex.Matches(htmlData, postRegex); foreach (Match match in matches) { var postBody = match.Groups[2].Value; var imageRegex = "img src=\"(.+?)\""; var imageMatches = Regex.Matches(postBody, imageRegex); var img = ""; foreach (Match imageMatch in imageMatches) { if (String.IsNullOrEmpty(img)) img = "http://src.sencha.io/80/80/" + imageMatch.Groups[1].Value; //TODO: Optimize for screen below 720p postBody = postBody.Replace(imageMatch.Groups[1].Value, "http://src.sencha.io/" + 1280 + "/" + imageMatch.Groups[1].Value); } var text = Regex.Replace(postBody, HtmlAnchorRegex, " "); if (text.Length > 140) { text = text.Substring(0, 140); text += "..."; } var userSub = match.Groups[5].Value.Split(new[] { "</a> в " }, StringSplitOptions.RemoveEmptyEntries); var sub = userSub.Length > 1 ? Regex.Replace(userSub[1], HtmlAnchorRegex, "") : ""; var user = userSub[0]; var vote = 0; if (match.Groups[12].Success && match.Groups[12].Value.Length > 0) vote = 1; else if (match.Groups[13].Success && match.Groups[13].Value.Length > 0) vote = -1; var post = new PostModel(); post.Id = match.Groups[1].Value.Replace("p", ""); post.Body = postBody; post.Rating = match.Groups[11].Value; post.Author = new UserModel {Username = user, Gender = match.Groups[3].Value == "Написал" ? UserGender.Male : UserGender.Female, CustomRank = match.Groups[4].Value}; post.Type = match.Groups[8].Value; post.Url = sub; post.Date = match.Groups[6].Value; post.Time = match.Groups[7].Value; var comments = Regex.Replace(match.Groups[9].Value, HtmlAnchorRegex, ""); comments = Regex.Replace(comments, "коммента.+?(\\s|$)", ""); comments = Regex.Replace(comments, " нов.+", ""); var commentsSplit = comments.Split('/'); post.TotalCommentsCount = commentsSplit[0].Trim(); if (commentsSplit.Length == 2) post.UnreadCommentsCount = commentsSplit[1].Trim(); post.HeaderImageUrl = img; post.HeaderText = text; post.Vote = vote; posts.Add(post); } return posts; }
public async Task<List<CommentModel>> GetComments(PostModel post) { var url = String.Empty; if (post.Type == "inbox") url = "http://leprosorium.ru/my/inbox/" + post.Id; else { var server = "leprosorium.ru"; url = String.Format("http://{0}/comments/{1}", server, post.Id); } var response = await PerformAuthenticatedGetRequest(url); if (response == null) return null; var responseContent = await response.Content.ReadAsStringAsync(); post.AddCommentCode = HtmlParser.ParseAddCommentCode(responseContent); var comments = HtmlParser.ParseComments(responseContent); return comments; }
public async Task<bool> VoteComment(PostModel post, CommentModel comment, string voteValue, string votingCode) { var url = "http://"; if (!String.IsNullOrEmpty(post.Url)) url += post.Url; else url += "leprosorium.ru"; url += "/rate/"; var message = new HttpRequestMessage(HttpMethod.Post, new Uri(url)); message.Content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("type", "0"), new KeyValuePair<string, string>("post_id", post.Id), new KeyValuePair<string, string>("wtf", votingCode), new KeyValuePair<string, string>("value", voteValue), new KeyValuePair<string, string>("id", comment.Id), }); var response = await PerformAuthenticatedPostRequest(message); return response != null; }
public PostViewModel(PostModel postModel) { Model = postModel; }