14.7.3. Demo: Display Real Reddit Comments and Replies#

Choose Social Media Platform: Reddit | Discord | Bluesky

Now lets do the same thing we did on the last page (using recursion to display comments and replies), but do it on Reddit! (Either for real or faked with the fake_praw library).

Reddit Praw Setup#

# make sure praw library is installed
import praw

Now lets do the same thing we did on the last page (using recursion to display comments and replies), but do it on Reddit! (Either for real or faked with the fake_praw library).

%run ../../fake_apis/fake_praw.ipynb
Fake praw is replacing the praw library. Fake praw doesn't need real passwords, and prevents you from accessing real reddit
# Load all your developer access passwords into Python
# TODO: Put your reddit username, password, and special developer access passwords below:
username="fake_reddit_username"
password="sa@#4*fdf_fake_password_$%DSG#%DG"
client_id="45adf$TW_fake_client_id_JESdsg1O"
client_secret="56sd_fake_client_secret_%Yh%"
# Give the praw code your reddit account info so
# it can perform reddit actions
reddit = praw.Reddit(
    username=username, password=password,
    client_id=client_id, client_secret=client_secret,
    user_agent="a custom python script for user /" + str(username)
)
Fake praw is pretending to collect account info to use on reddit

Helper function to display text in an indented box#

(You don’t need to worry about how this works. This is that function that helps display posts in indented boxes)

from IPython.display import HTML, Image, display
import html
def display_indented(text, left_margin=0):
    display(
        HTML(
            "<pre style='border:solid 1px;padding:3px;margin-left:"+ str(left_margin) + "px'>" + 
            html.escape(text) + 
            "</pre>"
        )
    )

Code to print a post with all comments and replies#

The print_post_and_replies is a function that takes a postId (instructions on where to get one below), prints information about that post, and then uses the print_comment_and_replies function to print out all comments and replies.

def print_post_and_replies(postId, show_hidden=False):
    submission = reddit.submission(postId)
    
    print("Comments and replies for post from /"+ submission.subreddit.display_name + ":" )
    display(HTML('<a href="'+"https://www.reddit.com/" + submission.permalink +'">'+submission.title+'</a>'))
    
    submission.comment_sort = "old"
    submission.comments.replace_more() #make sure to load as many comments as possible
    comments = submission.comments
    
    for comment in comments:
        print_comment_and_replies(comment, show_hidden=show_hidden)

The print_comment_and_replies function takes a given comment and recursively prints that comment as well as all replies to that comment (which will as well as all replies to those replies, etc.)

def print_comment_and_replies(comment, num_indents=0, show_hidden=False):
    
    replies = comment.replies

    display_text = (
        comment.body + "\n" +
        "-- " + str(comment.author) + 
        " (score " + str(comment.score) + ")"
    )
    
    display_indented(display_text, num_indents*20)

    #print replies (and the replies of those, etc.)
    for reply in replies:
        print_comment_and_replies(reply, num_indents = num_indents + 1, show_hidden=show_hidden)

Finding post IDs and testing our code#

In order to test it out, we need to find an id of a reddit post that has comments on it. Once you have a reddit post open in your browser, find or copy the url website address and look for the piece of random letters after https://www.reddit.com/r/[subredditname]/comments/, which is the id.

For example, in this post, the id is ‘fuulky’: Screenshot of reddit with a post up. The website url is "https://www.reddit.com//r/MovieDetails/comments/fuulky/in_little_women_2019_laurie_and_jo_swap_articles/". There is a circle drawn around the letters "fuulky" which appears after "comments/"

Now we can test it out by calling the print_post_and_replies with post id strings like 'fuulky', 'vfs5oh' or 'lzvvwp' as the argument, and see what the comment tree.

print_post_and_replies('fuulky')
Comments and replies for post from /MovieDetails(Fake):
Wow! That is a cool fake fact!
-- FakeAuthor (score 6)
You're totally right! I never saw that before!
-- FalseAuthor (score 3)
Yeah, and did you see this other detail too?
-- FalseAuthor (score 7)
Wow! No way!
-- FakeAuthor (score 2)
It's not cool! You are a bad person for saying it's cool!
-- TrollAuthor (score -10)
I saw a completely unrelated movie once!
-- PretendAuthor (score 1)
I don't see how that's relevant
-- FakeAuthor (score 2)