\" + \n", " html.escape(text) + \n", " \"\"\n", " )\n", " )" ] }, { "cell_type": "markdown", "id": "d37b46e5-c941-4ac9-a95f-a16061ecf7cf", "metadata": { "tags": [] }, "source": [ "## Code to print a post with all comments and replies\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 104, "id": "f80fa629-1b46-4287-8f51-0dadc13532e5", "metadata": {}, "outputs": [], "source": [ "def print_post_and_replies(postId, show_hidden=False):\n", " submission = reddit.submission(postId)\n", " \n", " print(\"Comments and replies for post from /\"+ submission.subreddit.display_name + \":\" )\n", " display(HTML(''+submission.title+''))\n", " \n", " submission.comment_sort = \"old\"\n", " submission.comments.replace_more() #make sure to load as many comments as possible\n", " comments = submission.comments\n", " \n", " for comment in comments:\n", " print_comment_and_replies(comment, show_hidden=show_hidden)" ] }, { "cell_type": "markdown", "id": "6d28fb60-3e2b-40e1-a3cb-c3b4afac9649", "metadata": {}, "source": [ "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.)" ] }, { "cell_type": "code", "execution_count": 105, "id": "d0b83265-3a27-47a3-a68a-5d3293ed7a3d", "metadata": {}, "outputs": [], "source": [ "def print_comment_and_replies(comment, num_indents=0, show_hidden=False):\n", " \n", " replies = comment.replies\n", "\n", " display_text = (\n", " comment.body + \"\\n\" +\n", " \"-- \" + str(comment.author) + \n", " \" (score \" + str(comment.score) + \")\"\n", " )\n", " \n", " display_indented(display_text, num_indents*20)\n", "\n", " #print replies (and the replies of those, etc.)\n", " for reply in replies:\n", " print_comment_and_replies(reply, num_indents = num_indents + 1, show_hidden=show_hidden)" ] }, { "cell_type": "markdown", "id": "b8d5835b-ef1d-45a3-a101-065e94cc0d17", "metadata": {}, "source": [ "## Finding post IDs and testing our code\n", "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.\n", "\n", "For example, in this post, the id is 'fuulky':\n", "\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 106, "id": "cb70bbcf-2585-426e-b5ff-55ca84a1f35d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Comments and replies for post from //MovieDetails(Fake):\n" ] }, { "data": { "text/html": [ "This is a fake fact about movie costuming!" ], "text/plain": [ "
Wow! That is a cool fake fact!\n", "-- FakeAuthor (score 6)" ], "text/plain": [ "
You're totally right! I never saw that before!\n", "-- FalseAuthor (score 3)" ], "text/plain": [ "
Yeah, and did you see this other detail too?\n", "-- FalseAuthor (score 7)" ], "text/plain": [ "
Wow! No way!\n", "-- FakeAuthor (score 2)" ], "text/plain": [ "
It's not cool! You are a bad person for saying it's cool!\n", "-- TrollAuthor (score -10)" ], "text/plain": [ "
I saw a completely unrelated movie once!\n", "-- PretendAuthor (score 1)" ], "text/plain": [ "
I don't see how that's relevant\n", "-- FakeAuthor (score 2)" ], "text/plain": [ "