internal static CommentListing FromJson(JToken token) { var list = new CommentListing(); list.ModHash = token["data"]["modhash"].ToString(); var comments = token["data"]["children"]; //.Select(t => t["data"]); foreach (var comment in comments) { switch (comment["kind"].ToString()) { case "t1": list.Add(new Comment(comment["data"])); break; case "more": var test = comment["data"]["children"].Values().Select(t => t.ToString()).ToList(); list.More.AddRange(test); break; } } return(list); }
public static CommentListing GetCommentsForPost(Session session, string id, CommentSortBy sort, string more) { var url = "http://www.reddit.com/comments/" + id + ".json"; var request = new Request { Url = url, Method = "GET", Cookie = session.Cookie }; var json = string.Empty; if (request.Execute(out json) != System.Net.HttpStatusCode.OK) { throw new RedditException(json); } // use some tricky recursive linq to get the comment structure, // understanding this required the use of caffeine. I'm tired. var o = JArray.Parse(json); // skip / ignore the post // o[0] // loop over the top level comments var list = new CommentListing(); var comments = o[1]["data"]["children"]; //.Select(t => t["data"]); list.ModHash = o[1]["data"]["modhash"].ToString(); foreach (var comment in comments) { switch (comment["kind"].ToString()) { case "t1": list.Add(new Comment(comment["data"])); break; case "more": var test = comment["data"]["children"].Values().Select(t => t.ToString()).ToList(); list.More.AddRange(test); break; } } return(list); }
internal Comment(JToken token) { Replies = new CommentListing(); // convert from json Body = token["body"].ToString(); SubID = token["subreddit_id"].ToString(); AuthorFlairCssClass = token["author_flair_css_class"].ToString(); Created = Convert.ToInt32(token["created"].ToString()).ToDateTime(); AuthorFlairText = token["author_flair_text"].ToString(); Downs = Convert.ToInt32(token["downs"].ToString()); Author = token["author"].ToString(); CreatedUtc = Convert.ToInt32(token["created_utc"].ToString()).ToDateTime(); BodyHtml = token["body_html"].ToString(); LinkID = token["link_id"].ToString(); ParentID = token["parent_id"].ToString(); Likes = token["likes"].ToString(); ID = token["id"].ToString(); SubReddit = token["subreddit"].ToString(); Ups = Convert.ToInt32(token["ups"].ToString()); Name = token["name"].ToString(); // parse any child comments in if (token["replies"].HasValues) { // set the modhash etc Replies.ModHash = token["replies"]["data"]["modhash"].ToString(); // recursive reply logic foreach (var child in token["replies"]["data"]["children"].Children()) { // find any 'more' recrods and attach here switch (child["kind"].ToString()) { case "t1": Replies.Add(new Comment(child["data"])); break; case "more": var test = child["data"]["children"].Values().Select(t => t.ToString()).ToList(); Replies.More.AddRange(test); break; } } } }