Skip to content

Instantly share code, notes, and snippets.

@readytheory
Created April 24, 2022 02:33
Show Gist options
  • Save readytheory/a197be1803bcd1d2de9847d253b1568a to your computer and use it in GitHub Desktop.
Save readytheory/a197be1803bcd1d2de9847d253b1568a to your computer and use it in GitHub Desktop.
praw get comments depth first
def get_comments(submission):
if type(submission) == str:
forest = reddit.submission(id=submission).comments
else:
forest = submission.comments
forest.replace_more(limit=None)
# now forest is a flat list of comments. Each comment may have children
def recur_get_comments(forest, level=0):
level += 1
for x in forest:
try:
print(f"{x.id} parent {x.parent()} {x.author}, {x.permalink}, {level=}", flush=True)
x.replies.replace_more(limit=None)
recur_get_comments(x.replies, level)
except AttributeError:
print("got attribute error at {level=}", flush=True)
recur_get_comments(forest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment