Пример #1
0
        private void ProcessJsonPosts(JToken parsedPostsJObject, List<LepraPost> latestPosts)
        {
            foreach (var postJObject in parsedPostsJObject)
            {
                var jToken = postJObject.First;

                var postBody = jToken["body"].Value<String>();

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

                foreach (Match match in res)
                {
                    if(String.IsNullOrEmpty(img))
                        img = "http://src.sencha.io/80/80/" + match.Groups[1].Value;

                    postBody = postBody.Replace(match.Groups[1].Value, "http://src.sencha.io/" + _screenBiggestMeasure + "/" + match.Groups[1].Value);
                }

                var text = Regex.Replace(postBody, "(<([^>]+)>)", " ");
                if (text.Length > 140)
                {
                    text = text.Substring(0, 140);
                    text += "...";
                }

                var post = new LepraPost();
                post.Id = jToken["id"].Value<String>();
                post.Body = postBody;
                post.Rating = jToken["rating"].Value<String>();
                post.Url = jToken["domain_url"].Value<String>();
                post.Image = img;
                post.Text = text;
                post.User = jToken["login"].Value<String>();
                post.Comments = jToken["comm_count"].Value<String>() + " / " + jToken["unread"].Value<String>();
                post.Wrote = String.Format("{0} {1}{2}", jToken["gender"].Value<int>() == 1 ? "Написал " : "Написала ",
                                           (String.IsNullOrEmpty(jToken["user_rank"].Value<String>()) ? "" : jToken["user_rank"].Value<String>() + " "),
                                           jToken["login"].Value<String>());
                                   
                post.When = jToken["textdate"] + " в " + jToken["posttime"];
                post.Vote = jToken["user_vote"].Value<int>();

                latestPosts.Add(post);
            }
        }
Пример #2
0
        private List<LepraPost> ProcessHtmlPosts(String htmlData)
        {
            var myPosts = new List<LepraPost>();

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

            var postReg ="<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, postReg);
            foreach (Match match in matches)
            {
                var postBody = match.Groups[2].Value;
                var imgReg = "img src=\"(.+?)\"";
                var res = Regex.Matches(postBody, imgReg);
                var img = "";

                foreach (Match imgMatch in res)
                {
                    if (String.IsNullOrEmpty(img))
                        img = "http://src.sencha.io/80/80/" + imgMatch.Groups[1].Value;

                    postBody = postBody.Replace(imgMatch.Groups[1].Value, "http://src.sencha.io/" + _screenBiggestMeasure + "/" + imgMatch.Groups[1].Value);
                }

                var text = Regex.Replace(postBody, "(<([^>]+)>)", " ");
                if (text.Length > 140)
                {
                    text = text.Substring(0, 140);
                    text += "...";
                }

                var userSub = match.Groups[4].Value.Split(new []{"</a> в "}, StringSplitOptions.RemoveEmptyEntries);
                var sub = userSub.Length > 1 ? Regex.Replace(userSub[1], "(<([^>]+)>)", "") : "";

                var user = userSub[0];
                
                var vote = 0;
                if (match.Groups[10].Success && match.Groups[10].Value.Length > 0)
                    vote = 1;
                else if (match.Groups[11].Success && match.Groups[11].Value.Length > 0)
                    vote = -1;

                var post = new LepraPost();
                post.Id = match.Groups[1].Value.Replace("p", "");
                post.Body = postBody;
                post.Rating = match.Groups[9].Value;
                post.User = user;
                post.Type = match.Groups[6].Value;
                post.Url = sub;
                post.When = match.Groups[5].Value;
                post.Wrote = String.Format("{0}{1}", match.Groups[3].Value, user);

                var comments = Regex.Replace(match.Groups[7].Value, "(<([^>]+)>)", "");
                comments = Regex.Replace(comments, "коммента.+?(\\s|$)", "");
                comments = Regex.Replace(comments, " нов.+", "");
                post.Comments = comments;

                post.Image = img;
                post.Text = text;
                post.Vote = vote;

                myPosts.Add(post);
            }

            return myPosts;
        }
Пример #3
0
        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;
        }
Пример #4
0
        public async void VotePost(LepraPost post, 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", "1"),
                                                                new KeyValuePair<string, string>("id", post.Id),
                                                                new KeyValuePair<string, string>("wtf", _postVoteWTF),
                                                                new KeyValuePair<string, string>("value", value),
                                                            });
            var response = await client.SendAsync(message);
        }
Пример #5
0
        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;
        }