Last active
January 19, 2024 00:18
-
-
Save ikatkov/d1e421460c46d03df87fde9dcda0e12b to your computer and use it in GitHub Desktop.
This file contains 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
import praw | |
from praw.models import MoreComments | |
# go through the comment tree and find all the top level MoreComments objects | |
def gather_more_comments(submission): | |
top_level_comment_count = 0 | |
more_comments = [] | |
for top_level_comment in submission.comments: | |
if isinstance(top_level_comment, MoreComments): | |
more_comments.append(top_level_comment) | |
else: | |
top_level_comment_count += 1 | |
print(f"Comments: {top_level_comment_count}, MoreComments objects: {len(more_comments)}") | |
return more_comments | |
reddit = praw.Reddit( | |
client_id="97RnjxHE3NV56oXM6ZlhIA", | |
client_secret="o8DBEGhjB097K6uRejKDdq1VX3hgWA", | |
user_agent="testscript", | |
) | |
print("Start...") | |
submission = reddit.submission(url="https://www.reddit.com/r/AskReddit/comments/197rmz0/those_who_got_a_useless_degree_what_do_you_do_now") | |
# count the number of API requests we've made | |
count_requests = 1 | |
# get the initial MoreComments list | |
more_comments = gather_more_comments(submission) | |
# loop until the list is empty, ie, there are no top level MoreComments objects left | |
while more_comments: | |
# grab the first one, just in case there's more than one for some reason | |
more_comment = more_comments.pop() | |
# call the fetch method to get all the comments from the API | |
new_comments = more_comment.comments(update=False) | |
count_requests += 1 | |
# insert all new comments into the tree | |
for comment in new_comments: | |
if isinstance(comment, MoreComments) and comment.parent_id.startswith('t1_'): | |
continue | |
submission.comments._insert_comment(comment) | |
# remove this MoreComments item from the tree | |
submission.comments._comments.remove(more_comment) | |
# iterate through the top level tree again to find the new MoreComments object | |
more_comments = gather_more_comments(submission) | |
print(f"Done in {count_requests} requests") | |
for top_level_comment in submission.comments.list(): | |
if type(top_level_comment) is not MoreComments: | |
print(top_level_comment.body) | |
print("----") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment