Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "More" to "Continue this thread" #219

Open
Slord6 opened this issue Nov 27, 2019 · 6 comments
Open

Use "More" to "Continue this thread" #219

Slord6 opened this issue Nov 27, 2019 · 6 comments
Labels

Comments

@Slord6
Copy link

Slord6 commented Nov 27, 2019

Hi, I'm struggling to iterate down a comment tree. Based on what I've seen of the RedditSharp code and the Reddit API, I think I should get back a More object on comments that on Reddit show with a Continue this thread link. I'm not seeing this and I'm not sure if it's not implemented or I'm doing something wrong...

The post I'm looking at specifically in trying to solve this is this one, specifically this comment thread. And my current implementation looks something like this, where I've retrieved the above linked post, and I'm traversing down the above comment thread.

Post post = GetPost(xyz);
List<Comment> authorComments = GetAllComments(post); // Gets top level comments by the post author
while (authorComments.Count > 0)
            {
                // Grab highest voted comment
                Comment topComment = authorComments.OrderByDescending(comment => comment.Upvotes).First();
               // Add contents to thread text
                threadContents += Environment.NewLine + Environment.NewLine + topComment.Body;
                
                if (topComment.More != null)
                {
                    Console.WriteLine("MORE!"); // This is never hit
                    // Go fetch the 'more' child comments (here lies the problem) + assign to authorComments
                } else {
                    // Otherwise, get the child comments by the same author
                    authorComments = topComment.Comments.Where(authorCommentsFilter).ToList();
                }
            }
@CrustyJew
Copy link
Owner

Whats the implementation of GetAllComments?

If you call EnumerateCommentTreeAsync on the Post this will give you an enumerator that does all the "more" parsing for you if that solves your problem.

@Slord6
Copy link
Author

Slord6 commented Dec 10, 2019

Hi, I'm on mobile so apologies in advance for dodgy formatting.

GetAllComments look like:

private static List<Comment> GetAllComments(Post post, CommentSort sorting)
{
     Task<List<Thing>> getCommentsTask = post.GetCommentsWithMoresAsync(0, sorting);
     getCommentsTask.Wait();
     List<Comment> comments = ResolveToComments(getCommentsTask.Result);
     getCommentsTask.Dispose();

    return comments;
}

private static List<Comment> ResolveToComments(List<Thing> things)
{
        List<Comment> comments = new List<Comment>();
        foreach (Thing item in things)
        {
            if (item is Comment)
            {
                comments.Add((Comment)item);
            }
            else if(item is More)
            {
                More more = (More)item;
                Task<List<Thing>> getThingsTask = more.GetThingsAsync();
                getThingsTask.Wait();
                List<Thing> moreItems = getThingsTask.Result;
                getThingsTask.Dispose();
                comments.AddRange(ResolveToComments(moreItems));
            }
            else
            {
                Console.WriteLine("Could not resolve thing to comment - " + item.ToString() + " - " + item.GetType().ToString());
            }
        }
        return comments;
}

Call signature may not match as I simplified the original code I posted a bit for clarity.
Off the top of my head the else if(item is More) block above does resolve the Mores it sees, which are top-level expansions for more comments on the Post, but I don't see More objects in specific comment threads.
I believed I tried that, with similar results but will try and have another look this week.

@Slord6
Copy link
Author

Slord6 commented Dec 18, 2019

Ok, think I've tracked down the issue.

The initial issue I raised I think was a problem in my code, not the lib. However, there is a closely related issue that has been mentioned in a couple of places elsewhere. The second thread there link through to the praw solution.

But to summarise - on some deeply nested comments, like those I linked in the original issue, the "Continue this thread" object looks like this:

{{
  "count": 0,
  "name": "t1__",
  "id": "_",
  "parent_id": "t1_cydnx3d",	
  "depth": 10,
  "children": []
}}

Which obviously isn't particularly useful. The solution to get the perma-link of the parent to get an updated context returns a 403 when calling like so:

string urlFormat = "https://www.reddit.com{0}";
Uri permaLink = new Uri(String.Format(urlFormat, ((Comment)parent).Permalink));
Task<Comment> getPermaTask = Reddit.GetCommentAsync(permaLink);
getPermaTask.Wait();
parent = getPermaTask.Result;

So the called endpoint ends up being https://www.reddit.com/r/HFY/comments/3yi82b/oc_prey/cydnx3d/.json which resolves fine in the browser, nut not through RedditSharp?

I also tried adding depth support to a couple of endpoints in the library but that seems to have no impact either. Not sure what else to try - any thoughts?

@czf
Copy link
Contributor

czf commented Feb 7, 2020

Checked into this a little. It looks like more objects isn't including the children. I believe that when this was implemented that array was populated and the morechildren endpoint would return the comments. If there aren't ids in the children array it won't work anymore and EnumerateCommentTreeAsync probably won't either

Actually was thinking of the EnumerateComments method in Post on master. The test for EnumerateCommentTreeAsync still passes.

@Slord6
Copy link
Author

Slord6 commented Feb 17, 2020

@czf your changes didn't fix it, but a fresh look at your version and looking at Praw and SnooWrap meant I figured it out - thanks!

@CrustyJew I forked cfz's fork, so up to you if you merge both PRs or not, don't suppose it makes a difference? 🤷‍♂

@CrustyJew
Copy link
Owner

@czf thanks for the help on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants