Created
April 24, 2022 02:33
-
-
Save readytheory/a197be1803bcd1d2de9847d253b1568a to your computer and use it in GitHub Desktop.
praw get comments depth first
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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