{ "cells": [ { "cell_type": "markdown", "id": "0cd4388e-b71c-4bd1-a60e-d2ef3933f3de", "metadata": {}, "source": [ "# Demo: Display Real Reddit Comments and Replies" ] }, { "cell_type": "markdown", "id": "123456789-930485093240532940945-0324095320945904325", "metadata": { "tags": [] }, "source": [" _Choose Social Media Platform: __Reddit__ | Discord | Bluesky | No Coding_ "] }, { "cell_type": "markdown", "id": "5fdfe7cf-3641-4919-9d56-f3b8c74b7b96", "metadata": {}, "source": [ "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)." ] }, { "cell_type": "markdown", "id": "cb900b5a-c199-47b8-95a9-417d44a786ab", "metadata": { "tags": [] }, "source": [ "## Reddit Praw Setup" ] }, { "cell_type": "code", "execution_count": 99, "id": "883641dc-a5a4-47d7-ba9c-3532e9df58d8", "metadata": {}, "outputs": [], "source": [ "# make sure praw library is installed\n", "import praw" ] }, { "cell_type": "markdown", "id": "cc8ae3f6", "metadata": {}, "source": [ "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)." ] }, { "cell_type": "code", "execution_count": 100, "id": "d42b7010-2690-4efb-b404-02682e39b559", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Fake praw is replacing the praw library. Fake praw doesn't need real passwords, and prevents you from accessing real reddit" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%run ../../fake_apis/fake_praw.ipynb" ] }, { "cell_type": "code", "execution_count": 101, "id": "a45ce757-b313-4de6-b5c4-32d255946f0f", "metadata": {}, "outputs": [], "source": [ "# Load all your developer access passwords into Python\n", "# TODO: Put your reddit username, password, and special developer access passwords below:\n", "username=\"fake_reddit_username\"\n", "password=\"sa@#4*fdf_fake_password_$%DSG#%DG\"\n", "client_id=\"45adf$TW_fake_client_id_JESdsg1O\"\n", "client_secret=\"56sd_fake_client_secret_%Yh%\"" ] }, { "cell_type": "code", "execution_count": 102, "id": "9c7a1f06-fb4d-4ce6-bd0b-de421420a198", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Fake praw is pretending to collect account info to use on reddit" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Give the praw code your reddit account info so\n", "# it can perform reddit actions\n", "reddit = praw.Reddit(\n", " username=username, password=password,\n", " client_id=client_id, client_secret=client_secret,\n", " user_agent=\"a custom python script for user /\" + str(username)\n", ")" ] }, { "cell_type": "markdown", "id": "da45bc67-b72b-45ef-8f70-7eedf9261945", "metadata": {}, "source": [ "## Helper function to display text in an indented box\n", "(You don't need to worry about how this works. This is that function that helps display posts in indented boxes)" ] }, { "cell_type": "code", "execution_count": 103, "id": "98574fd2-9380-4653-8b42-d5f00fd634c3", "metadata": {}, "outputs": [], "source": [ "from IPython.display import HTML, Image, display\n", "import html\n", "def display_indented(text, left_margin=0):\n", " display(\n", " HTML(\n", " \"
\" + \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", "![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/\"](get_reddit_post_id.png)\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": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Wow! That is a cool fake fact!\n",
       "-- FakeAuthor (score 6)
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
You're totally right! I never saw that before!\n",
       "-- FalseAuthor (score 3)
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Yeah, and did you see this other detail too?\n",
       "-- FalseAuthor (score 7)
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Wow! No way!\n",
       "-- FakeAuthor (score 2)
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
It's not cool! You are a bad person for saying it's cool!\n",
       "-- TrollAuthor (score -10)
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
I saw a completely unrelated movie once!\n",
       "-- PretendAuthor (score 1)
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
I don't see how that's relevant\n",
       "-- FakeAuthor (score 2)
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print_post_and_replies('fuulky')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 5 }