示例#1
0
文件: Lepra.cs 项目: ncksol/Testing
        public async Task<List<LepraComment>> GetComments(LepraPost post)
        {
            var comments = new List<LepraComment>();
            var url = String.Empty;

            if (post.Type == "inbox")
                url = "http://leprosorium.ru/my/inbox/" + post.Id;
            else
            {
                var server = "leprosorium.ru";
                /*if (!String.IsNullOrEmpty(post.Url))
                    server = post.Url;*/

                url = String.Format("http://{0}/comments/{1}", server, post.Id);
            }
            
            if (_cookieContainer == null)
                FillCookies();

            var handler = new HttpClientHandler { CookieContainer = _cookieContainer };
            var client = new HttpClient(handler);
            var htmlData = await client.GetStringAsync(url);

            ProcessMain(htmlData);
            
            htmlData = Regex.Replace(htmlData, "\n+", "");
            htmlData = Regex.Replace(htmlData, "\r+", "");
            htmlData = Regex.Replace(htmlData, "\t+", "");

            var voteResMatch = Regex.Match(htmlData, "wtf_vote = '(.+?)'");
            //post.Vote = voteResMatch.Success ? Convert.ToInt32(voteResMatch.Groups[1].Value) : (int?)null;
            post.CommentWtf = Regex.Match(htmlData, "commentsHandler.wtf = '(.+?)'").Groups[1].Value;

            var commentsReg = "<div id=\"(.+?)\" class=\"post tree(.+?)\"><div class=\"dt\">(.+?)</div>.+?<a href=\".*?/users/.+?\">(.+?)</a>,(.+?)<span>.+?(<div class=\"vote\".+?><em>(.+?)</em></span>|</div>)(<a href=\"#\".+?class=\"plus(.*?)\">.+?<a href=\"#\".+?class=\"minus(.*?)\">|</div>)";

            htmlData = htmlData.Substring(htmlData.IndexOf("id=\"js-commentsHolder\""));
            var commentsMatches = Regex.Matches(htmlData, commentsReg);

            foreach (Match match in commentsMatches)
            {
                var text = match.Groups[3].Value;

                var imgReg = "img src=\"(.+?)\"";
                var res = Regex.Matches(text, imgReg);

                foreach (Match imgMatch in res)
                {
                    text = text.Replace(imgMatch.Groups[1].Value, "http://src.sencha.io/" + _screenBiggestMeasure + "/" + imgMatch.Groups[1].Value);
                }

                var vote = 0;
                if (!String.IsNullOrEmpty(match.Groups[9].Value))
                    vote = 1;
                else if (!String.IsNullOrEmpty(match.Groups[10].Value))
                    vote = -1;

                var indent = 0;
                var resImgMatch = Regex.Match(match.Groups[2].Value, "indent_(.+?) ");

                if (resImgMatch.Success)
                    indent = Convert.ToInt32(resImgMatch.Groups[1].Value);
                if (indent > 15)
                    indent = 15;

                text = Regex.Replace(text, "<p.*?>", "");
                text = Regex.Replace(text, "</p>", "");
                text = Regex.Replace(text, "<nonimg", "<img");

                var isNew = match.Groups[2].Value.IndexOf("new") != -1;

                var comment = new LepraComment();
                comment.Id = match.Groups[1].Value;
                comment.IsNew = isNew;
                comment.Indent = indent;
                comment.Text = text;
                comment.Rating = match.Groups[7].Value;
                comment.User = match.Groups[4].Value;
                comment.When = match.Groups[5].Value;
                comment.Vote = vote;

                comments.Add(comment);
            }

            return comments;
        }
示例#2
0
文件: Lepra.cs 项目: ncksol/Testing
        public async void VoteComment(LepraPost post, LepraComment comment, string value)
        {
            if (_cookieContainer == null)
                FillCookies();

            var url = "http://";
            if (!String.IsNullOrEmpty(post.Url))
                url += post.Url;
            else
                url += "leprosorium.ru";

            url += "/rate/";

            var handler = new HttpClientHandler { CookieContainer = _cookieContainer };
            var client = new HttpClient(handler);
            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", _postVoteWTF),
                                                                new KeyValuePair<string, string>("value", value),
                                                                new KeyValuePair<string, string>("id", comment.Id),
                                                            });
            var response = await client.SendAsync(message);
        }
示例#3
0
文件: Lepra.cs 项目: ncksol/Testing
        public async Task<LepraComment> AddComment(LepraPost post, LepraComment inReplyTo, string comment)
        {
            var url = "http://leprosorium.ru/commctl/";
            /*
            if(!String.IsNullOrEmpty(post.Url))
                url += post.Url;
            else
                url += "leprosorium.ru";

            url += "/commctl/";*/

            if (_cookieContainer == null)
                FillCookies();

            var handler = new HttpClientHandler { CookieContainer = _cookieContainer };
            var client = new HttpClient(handler);
            var message = new HttpRequestMessage(HttpMethod.Post, new Uri(url));
            message.Content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
                                                            {
                                                                new KeyValuePair<string, string>("pid", post.Id),
                                                                new KeyValuePair<string, string>("wtf", post.CommentWtf),
                                                                new KeyValuePair<string, string>("comment", comment),
                                                                new KeyValuePair<string, string>("replyto", inReplyTo != null ? inReplyTo.Id : ""),
                                                            });

            var response = await client.SendAsync(message);
            var responseContent = await response.Content.ReadAsStringAsync();

            var newCommentJObject = JObject.Parse(responseContent);

            if(newCommentJObject["status"].Value<String>() == "ERR")
                return null;

            var newComment = new LepraComment();
            newComment.Id = newCommentJObject["new_comment"]["comment_id"].Value<String>();
            newComment.IsNew = true;
            newComment.Text = comment;
            newComment.Rating = "0";
            newComment.User = newCommentJObject["new_comment"]["user_login"].Value<String>();
            var date = newCommentJObject["new_comment"]["date"].Value<String>();
            var time = newCommentJObject["new_comment"]["time"].Value<String>();
            newComment.When = date + " в " + time;
            newComment.Vote = 0;
            if(inReplyTo == null)
                newComment.Indent = 0;
            else
                newComment.Indent = inReplyTo.Indent == 15 ? 15 : inReplyTo.Indent + 1;

            return newComment;
        }