turnage / graw

Golang Reddit API Wrapper
MIT License
286 stars 50 forks source link

Can a way to access a sub's comments from a harvest be added? #24

Closed mz2212 closed 7 years ago

mz2212 commented 7 years ago

What I had in mind:

harvest, _ := bot.Listing("/r/bottesting", "")
for _, comment := range harvest.Comments[:30] {
  // Do stuff with comment.Body
}

What you currently have to do to access them doesn't work exactly. (Or at least I can't figure it out. I'm not sure if I'm doing it wrong.)

// At least in my testing, this doesn't work.
harvest, _ := bot.Listing("/r/bottesting", "")
for _, post := range harvest.Posts[:5] {
  for _, reply := range post.Replies[:5] {
    // Do stuff with reply.Body
  }
}
turnage commented 7 years ago

You can already do this! Harvests from listings just return the content at the endpoint. For a subreddit, the endpoint only offers posts. See the comment endpoint.

harvest, _ := bot.Listing("/r/bottesting/comments", "")
for _, comment := range harvest.Comments[:30] {
  // process comments
}

These comments will be flat and in reverse chronological order; if you want the tree structure of comments you'll need to use the post harvest and then request each thread individually to get the comment trees.

Also note that the posts from "/r/subreddit" endpoints in harvests do not contain their replies, as that data isn't part of the listing. You would need to call Thread() to get the replies.