Wednesday, July 14, 2021

Reddit API

What is Reddit?

Reddit is a community-driven link-sharing site. Users submit links to articles, photos and other contents.
Other users upvote the submissions they like and downvote the ones they dislike. Users can also comment on submissions and even upvote or downvote other people's comments.

This is Python subreddit link: Python Focussed Community

So in this article, we will explore trending posts on a particular subreddit, comments on a single article, posting comments on the article using reddit API.

Like GitHub, Reddit API also requires authentication. We will use OAuth (This mechanism is a bit complex but we won't get into details here)

{"Authorization": "bearer 13426216-4U1ckno9J5AiK72VRbpEeBaMSKk", "User-Agent": "Dataquest/1.0"}

Trending Posts on a particular subreddit (r/python)

script.py
Output
{'kind': 'Listing', 'data': {'before': None, 'modhash': None, 'children': [{'kind': 't3', 'data': {'selftext_html': None, 'mod_reports': [], 'author': 'ingvij', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': False, 'created_utc': 1458486941.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': '', 'domain': 'hkupty.github.io', 'title': 'Functional Philosophy and applying it to Python', 'hide_score': False, 'name': 't3_4b7w9u', 'removal_reason': None, 'quarantine': False, 'num_comments': 13, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458515741.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 53, 'locked': False, 'url': 'http://hkupty.github.io/2016/Functional-Programming-Concepts-Idioms-and-Philosophy/', 'num_reports': None, 'id': '4b7w9u', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 53, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b7w9u/functional_philosophy_and_applying_it_to_python/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': '<!-- SC_OFF --><div class="md"><p>Hey guys, </p>\n\n<p>So I made a really simple demonstration of exploiting a local <code>SQLite</code> database using <code>tkinter</code> in <code>python</code></p>\n\n<p>Thought I would share it with you guys</p>\n\n<hr/>\n\n<ul>\n<li>Github link: <a href="https://github.com/prodicus/thanos">https://github.com/prodicus/thanos</a></li>\n<li>screenshots: \n\n<ul>\n<li>SQL injection: <a href="http://i.imgur.com/qlzSCuP.jpg">http://i.imgur.com/qlzSCuP.jpg</a></li>\n<li>mitigated threat: <a href="http://i.imgur.com/42YhmpU.jpg">http://i.imgur.com/42YhmpU.jpg</a></li>\n</ul></li>\n</ul>\n</div><!-- SC_ON -->', 'mod_reports': [], 'author': 'madboy1995', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': True, 'created_utc': 1458479931.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': 'Hey guys, \n\nSo I made a really simple demonstration of exploiting a local `SQLite` database using `tkinter` in `python`\n\nThought I would share it with you guys\n\n***\n\n- Github link: [https://github.com/prodicus/thanos](https://github.com/prodicus/thanos)\n- screenshots: \n - SQL injection: http://i.imgur.com/qlzSCuP.jpg\n - mitigated threat: http://i.imgur.com/42YhmpU.jpg', 'domain': 'self.Python', 'title': 'SQL Injection demonstration on a local sqlite database', 'hide_score': False, 'name': 't3_4b7gnk', 'removal_reason': None, 'quarantine': False, 'num_comments': 14, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458508731.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 18, 'locked': False, 'url': 'https://www.reddit.com/r/Python/comments/4b7gnk/sql_injection_demonstration_on_a_local_sqlite/', 'num_reports': None, 'id': '4b7gnk', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 18, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b7gnk/sql_injection_demonstration_on_a_local_sqlite/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': None, 'mod_reports': [], 'author': 'iamspoilt', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': False, 'created_utc': 1458480023.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': '', 'domain': 'mrafayaleem.com', 'title': 'Interfacing Jython with Kafka 0.8.x', 'hide_score': False, 'name': 't3_4b7gv4', 'removal_reason': None, 'quarantine': False, 'num_comments': 3, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458508823.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 9, 'locked': False, 'url': 'http://mrafayaleem.com/2016/03/19/interfacing-jython-with-kafka/', 'num_reports': None, 'id': '4b7gv4', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 9, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b7gv4/interfacing_jython_with_kafka_08x/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': None, 'mod_reports': [], 'author': 'danwin', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': False, 'created_utc': 1458510378.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': '', 'domain': 'gist.github.com', 'title': 'Using Selenium and Python to screenshot a javascript-heavy page', 'hide_score': False, 'name': 't3_4b9g8m', 'removal_reason': None, 'quarantine': False, 'num_comments': 1, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458539178.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 6, 'locked': False, 'url': 'https://gist.github.com/dannguyen/61af3c7cbdef9d04a5fe', 'num_reports': None, 'id': '4b9g8m', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 6, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b9g8m/using_selenium_and_python_to_screenshot_a/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': '<!-- SC_OFF --><div class="md"><p>Hi, I am using Pillow to handle images in Django, and I would like to optimize images using mozjpeg before saving them.</p>\n\n<p>I've tried searching and reading documentation, but there isn't any information on how to use these libraries together, and these tags don't appear to be very active on Stackoverflow either. Am I not looking hard enough, or is this something that I'm expected to figure out on my own from reading source code by this point? I haven't had any formal training with programming, so this is probably out of my league but I really want to figure it out.</p>\n\n<p>My goal is to take a user-uploaded image in Django and a boolean input, and do the following:</p>\n\n<ul>\n<li>If original > 10mb, reject upload</li>\n<li>If 10mb >= original > 1mb, resize and compress and optimize to < 500kb</li>\n<li>If original <= 1mb and boolean is False, compress and optimize to < 500kb</li>\n<li>If original <= 1mb and boolean is True, only optimize</li>\n</ul>\n\n<p>Then save the resulting ImageField and image file to an Amazon S3 bucket, and retrieve it from a different URL on CloudFront (currently using django-storages and boto for this). The filename would be randomly generated (I have yet to write an exception to retry if there's a "filename exists" collision when saving to S3):</p>\n\n<pre><code>def generate_filename():\n return get_random_string(length=12, allowed_chars=u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')\n</code></pre>\n\n<p>After saving the original image, generate thumbnails of various sizes with the same filename and a suffix corresponding to the size, ex: <code>filename_s.jpg</code>, <code>filename_m.jpg</code>, <code>filename_l.jpg</code> and save these to another S3 bucket.</p>\n\n<p>The difficulty for me mostly has to do with not knowing how to implement this in code. Here are the questions I have:</p>\n\n<ol>\n<li><p>How can images be <a href="https://github.com/mozilla/mozjpeg/blob/master/usage.txt#L25">piped to mozjpeg</a> from Pillow directly and losslessly in memory (without saving to a NamedTemporaryFile)? I primarily want to avoid the situation where Pillow compresses the image and then mozjpeg compresses the already compressed image again, especially if mozjpeg could do a better job at it.</p></li>\n<li><p>I don't expect anyone to write my code for me, but a minimal snippet of sample code to help me understand how Python/Pillow can interact with mozjpeg and how everything fits together (where to put the logic of the If statements above) would be very much appreciated. </p></li>\n</ol>\n</div><!-- SC_ON -->', 'mod_reports': [], 'author': 'lelease', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': True, 'created_utc': 1458447203.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': 'Hi, I am using Pillow to handle images in Django, and I would like to optimize images using mozjpeg before saving them.\n\nI\'ve tried searching and reading documentation, but there isn\'t any information on how to use these libraries together, and these tags don\'t appear to be very active on Stackoverflow either. Am I not looking hard enough, or is this something that I\'m expected to figure out on my own from reading source code by this point? I haven\'t had any formal training with programming, so this is probably out of my league but I really want to figure it out.\n\nMy goal is to take a user-uploaded image in Django and a boolean input, and do the following:\n\n - If original > 10mb, reject upload\n - If 10mb >= original > 1mb, resize and compress and optimize to < 500kb\n - If original <= 1mb and boolean is False, compress and optimize to < 500kb\n - If original <= 1mb and boolean is True, only optimize\n\nThen save the resulting ImageField and image file to an Amazon S3 bucket, and retrieve it from a different URL on CloudFront (currently using django-storages and boto for this). The filename would be randomly generated (I have yet to write an exception to retry if there\'s a "filename exists" collision when saving to S3):\n\n def generate_filename():\n return get_random_string(length=12, allowed_chars=u\'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\')\n\nAfter saving the original image, generate thumbnails of various sizes with the same filename and a suffix corresponding to the size, ex: `filename_s.jpg`, `filename_m.jpg`, `filename_l.jpg` and save these to another S3 bucket.\n\nThe difficulty for me mostly has to do with not knowing how to implement this in code. Here are the questions I have:\n\n1. How can images be [piped to mozjpeg](https://github.com/mozilla/mozjpeg/blob/master/usage.txt#L25) from Pillow directly and losslessly in memory (without saving to a NamedTemporaryFile)? I primarily want to avoid the situation where Pillow compresses the image and then mozjpeg compresses the already compressed image again, especially if mozjpeg could do a better job at it.\n\n2. I don\'t expect anyone to write my code for me, but a minimal snippet of sample code to help me understand how Python/Pillow can interact with mozjpeg and how everything fits together (where to put the logic of the If statements above) would be very much appreciated. ', 'domain': 'self.Python', 'title': 'Using PIL/Pillow with mozjpeg', 'hide_score': False, 'name': 't3_4b6bew', 'removal_reason': None, 'quarantine': False, 'num_comments': 4, 'author_flair_css_class': None, 'hidden': False, 'edited': 1458447615.0, 'over_18': False, 'created': 1458476003.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 3, 'locked': False, 'url': 'https://www.reddit.com/r/Python/comments/4b6bew/using_pilpillow_with_mozjpeg/', 'num_reports': None, 'id': '4b6bew', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 3, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b6bew/using_pilpillow_with_mozjpeg/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': None, 'mod_reports': [], 'author': 'kracekumar', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': False, 'created_utc': 1458493844.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': '', 'domain': 'kracekumar.com', 'title': 'Django admin permission for staff user', 'hide_score': False, 'name': 't3_4b8bw0', 'removal_reason': None, 'quarantine': False, 'num_comments': 0, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458522644.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 1, 'locked': False, 'url': 'http://kracekumar.com/post/141377389440/permissions-in-django-admin', 'num_reports': None, 'id': '4b8bw0', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 1, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b8bw0/django_admin_permission_for_staff_user/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': '<!-- SC_OFF --><div class="md"><p>I frequently have two Jupyter QtConsole windows open. One running under 2.7, and one under 3.5. At the moment I distinguish between the two by using two completely different colour schemes so that I can tell which one I'm working in.</p>\n\n<p>Ideally I'd prefer to be able to change the window title from "Jupyter QtConsole" into something customised, e.g. "Jupyter 3.5". I haven't been able to find anything in the documentation that describes changing the title. Is is possible?</p>\n</div><!-- SC_ON -->', 'mod_reports': [], 'author': 'ThatOtherBatman', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': True, 'created_utc': 1458511042.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': 'I frequently have two Jupyter QtConsole windows open. One running under 2.7, and one under 3.5. At the moment I distinguish between the two by using two completely different colour schemes so that I can tell which one I\'m working in.\n\nIdeally I\'d prefer to be able to change the window title from "Jupyter QtConsole" into something customised, e.g. "Jupyter 3.5". I haven\'t been able to find anything in the documentation that describes changing the title. Is is possible?', 'domain': 'self.Python', 'title': 'Setting Title Jupyter QtConsole Window', 'hide_score': False, 'name': 't3_4b9hve', 'removal_reason': None, 'quarantine': False, 'num_comments': 1, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458539842.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 1, 'locked': False, 'url': 'https://www.reddit.com/r/Python/comments/4b9hve/setting_title_jupyter_qtconsole_window/', 'num_reports': None, 'id': '4b9hve', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 1, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b9hve/setting_title_jupyter_qtconsole_window/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': '<!-- SC_OFF --><div class="md"><p>I learned to do image processing with PIL/Pillow and I successfully used it for many graphics tasks.</p>\n\n<p>I'm currently trying to nice lines and curves for a new project, but I found that the <code>ImageDraw</code>module doesn't use anti-aliasing, and the result is pretty ugly. What library could I use to generate better images drawing lines and curves?</p>\n</div><!-- SC_ON -->', 'mod_reports': [], 'author': 'loderunnr', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': True, 'created_utc': 1458504195.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': "I learned to do image processing with PIL/Pillow and I successfully used it for many graphics tasks.\n\nI'm currently trying to nice lines and curves for a new project, but I found that the `ImageDraw`module doesn't use anti-aliasing, and the result is pretty ugly. What library could I use to generate better images drawing lines and curves?", 'domain': 'self.Python', 'title': 'Drawing library with anti-aliasing?', 'hide_score': False, 'name': 't3_4b918x', 'removal_reason': None, 'quarantine': False, 'num_comments': 2, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458532995.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 1, 'locked': False, 'url': 'https://www.reddit.com/r/Python/comments/4b918x/drawing_library_with_antialiasing/', 'num_reports': None, 'id': '4b918x', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 1, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b918x/drawing_library_with_antialiasing/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': '<!-- SC_OFF --><div class="md"><p>I have read that this might be because of spaces in my path, but i've tried to manually put quotes around each pyi... file in Scripts, and the same error occurs.</p>\n\n<p>There's no problem with gaining access to the Scripts folder anyways, so i don't think that's the issue. I'm using python 3.5.1.</p>\n</div><!-- SC_ON -->', 'mod_reports': [], 'author': 'Grandfather_Clock', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': True, 'created_utc': 1458505049.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': "I have read that this might be because of spaces in my path, but i've tried to manually put quotes around each pyi... file in Scripts, and the same error occurs.\n\nThere's no problem with gaining access to the Scripts folder anyways, so i don't think that's the issue. I'm using python 3.5.1.", 'domain': 'self.Python', 'title': 'I can\'t get PyInstaller to run; prints "failed to create process"', 'hide_score': False, 'name': 't3_4b93c0', 'removal_reason': None, 'quarantine': False, 'num_comments': 4, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458533849.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 0, 'locked': False, 'url': 'https://www.reddit.com/r/Python/comments/4b93c0/i_cant_get_pyinstaller_to_run_prints_failed_to/', 'num_reports': None, 'id': '4b93c0', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 0, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b93c0/i_cant_get_pyinstaller_to_run_prints_failed_to/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': '<!-- SC_OFF --><div class="md"><p>Hey,\nI'm about to programm a python script. In this I need to know if the screen is on or in standby at the moment. Is there a Unix (Raspbian) command which tells me this and which I could use in the script?\nbtw. does somebody know how to edit the "go to standby" time for the monitor?\nThanks</p>\n</div><!-- SC_ON -->', 'mod_reports': [], 'author': 'xBlackShad0w', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': True, 'created_utc': 1458502831.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': 'Hey,\nI\'m about to programm a python script. In this I need to know if the screen is on or in standby at the moment. Is there a Unix (Raspbian) command which tells me this and which I could use in the script?\nbtw. does somebody know how to edit the "go to standby" time for the monitor?\nThanks', 'domain': 'self.Python', 'title': 'Command: "is Monitor in standby" in Python?', 'hide_score': False, 'name': 't3_4b8xx5', 'removal_reason': None, 'quarantine': False, 'num_comments': 3, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458531631.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 0, 'locked': False, 'url': 'https://www.reddit.com/r/Python/comments/4b8xx5/command_is_monitor_in_standby_in_python/', 'num_reports': None, 'id': '4b8xx5', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 0, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b8xx5/command_is_monitor_in_standby_in_python/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': '<!-- SC_OFF --><div class="md"><p>Here is the code guys!If you could explain it somehow it would be great!</p>\n\n<pre><code>def reverse(text):\n rev=""\n for i in text:\n rev = i+rev\n return rev\nprint reverse("Hello guys")\n</code></pre>\n</div><!-- SC_ON -->', 'mod_reports': [], 'author': 'Py_learner222', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': True, 'created_utc': 1458501283.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': 'Here is the code guys!If you could explain it somehow it would be great!\n\n def reverse(text):\n rev=""\n for i in text:\n rev = i+rev\n return rev\n print reverse("Hello guys")', 'domain': 'self.Python', 'title': 'How did this guy made this code reverse the text?', 'hide_score': False, 'name': 't3_4b8u3h', 'removal_reason': None, 'quarantine': False, 'num_comments': 8, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458530083.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 0, 'locked': False, 'url': 'https://www.reddit.com/r/Python/comments/4b8u3h/how_did_this_guy_made_this_code_reverse_the_text/', 'num_reports': None, 'id': '4b8u3h', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 0, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b8u3h/how_did_this_guy_made_this_code_reverse_the_text/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': None, 'mod_reports': [], 'author': 'iniy', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': False, 'created_utc': 1458485572.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': '', 'domain': 'github.com', 'title': 'Planing for a new book, any suggestion?', 'hide_score': False, 'name': 't3_4b7szt', 'removal_reason': None, 'quarantine': False, 'num_comments': 2, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458514372.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 0, 'locked': False, 'url': 'https://github.com/rainyear/Data-Structures-and-Algorithms-in-Interview', 'num_reports': None, 'id': '4b7szt', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 0, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b7szt/planing_for_a_new_book_any_suggestion/', 'archived': False}}, {'kind': 't3', 'data': {'selftext_html': '<!-- SC_OFF --><div class="md"><p>Hello. I am not a coder by trade and am an older guy still in school. I find programming to be an incredibly soothing, creative outlet and have a reasonable amount of experience in C/C++/Java, assembly and VHDL and enjoy them all. I'm having my second go-around trying to learn Python since it seems so popular these days, and I am interested in it currently because of the statistical packages that are offered for it. I find the language incredibly goofy and strange, yet everyone touts it as 'easy to learn' and 'elegant' and stuff like that. I feel that I'm in the minority here and was wondering if anyone could shed some light on what I'm missing here.</p>\n\n<p>I <em>am</em> interested in portable code, since I'm primarily a Linux user but would like to create things readily-runnable on other platforms. But the syntax and whole philosophy behind Python...I'm just not getting it.</p>\n\n<p>Any takers? I'm not trying to spark a fire here, I just really want to know if there's something I'm missing. I have scoured the net for reasons and am still not convinced. </p>\n</div><!-- SC_ON -->', 'mod_reports': [], 'author': 'jaques_sauvignon', 'distinguished': None, 'from_id': None, 'approved_by': None, 'is_self': True, 'created_utc': 1458479500.0, 'link_flair_text': None, 'media': None, 'subreddit': 'Python', 'selftext': "Hello. I am not a coder by trade and am an older guy still in school. I find programming to be an incredibly soothing, creative outlet and have a reasonable amount of experience in C/C++/Java, assembly and VHDL and enjoy them all. I'm having my second go-around trying to learn Python since it seems so popular these days, and I am interested in it currently because of the statistical packages that are offered for it. I find the language incredibly goofy and strange, yet everyone touts it as 'easy to learn' and 'elegant' and stuff like that. I feel that I'm in the minority here and was wondering if anyone could shed some light on what I'm missing here.\n\nI *am* interested in portable code, since I'm primarily a Linux user but would like to create things readily-runnable on other platforms. But the syntax and whole philosophy behind Python...I'm just not getting it.\n\nAny takers? I'm not trying to spark a fire here, I just really want to know if there's something I'm missing. I have scoured the net for reasons and am still not convinced. ", 'domain': 'self.Python', 'title': 'As a C-family guy, convince me to learn Python?', 'hide_score': False, 'name': 't3_4b7fph', 'removal_reason': None, 'quarantine': False, 'num_comments': 18, 'author_flair_css_class': None, 'hidden': False, 'edited': False, 'over_18': False, 'created': 1458508300.0, 'from_kind': None, 'suggested_sort': None, 'thumbnail': '', 'likes': None, 'stickied': False, 'downs': 0, 'banned_by': None, 'media_embed': {}, 'ups': 0, 'locked': False, 'url': 'https://www.reddit.com/r/Python/comments/4b7fph/as_a_cfamily_guy_convince_me_to_learn_python/', 'num_reports': None, 'id': '4b7fph', 'clicked': False, 'from': None, 'subreddit_id': 't5_2qh0y', 'user_reports': [], 'score': 0, 'secure_media': None, 'author_flair_text': None, 'gilded': 0, 'visited': False, 'secure_media_embed': {}, 'report_reasons': None, 'link_flair_css_class': None, 'saved': False, 'permalink': '/r/Python/comments/4b7fph/as_a_cfamily_guy_convince_me_to_learn_python/', 'archived': False}}], 'after': None}}



The variable python_top is a {} containing all the information about all of the individual posts that reddit users submitted during the past day. The actual list of posts is inside a dictionary key.

Output
<class 'dict'>

How to get the list of posts from dictionary key? 

One Key-Value pair for example:

{'data': {'approved_by': None, 'archived': False, 'author': 'ingvij', 'author_flair_css_class': None, 'author_flair_text': None, 'banned_by': None, 'clicked': False, 'created': 1458515741.0, 'created_utc': 1458486941.0, 'distinguished': None, 'domain': 'hkupty.github.io', 'downs': 0, 'edited': False, 'from': None, 'from_id': None, 'from_kind': None, 'gilded': 0, 'hidden': False, 'hide_score': False, 'id': '4b7w9u', 'is_self': False, 'likes': None, 'link_flair_css_class': None, 'link_flair_text': None, 'locked': False, 'media': None, 'media_embed': {}, 'mod_reports': [], 'name': 't3_4b7w9u', 'num_comments': 13, 'num_reports': None, 'over_18': False, 'permalink': '/r/Python/comments/4b7w9u/functional_philosophy_and_applying_it_to_python/', 'quarantine': False, 'removal_reason': None, 'report_reasons': None, 'saved': False, 'score': 53, 'secure_media': None, 'secure_media_embed': {}, 'selftext': '', 'selftext_html': None, 'stickied': False, 'subreddit': 'Python', 'subreddit_id': 't5_2qh0y', 'suggested_sort': None, 'thumbnail': '', 'title': 'Functional Philosophy and applying it to Python', 'ups': 53, 'url': 'http://hkupty.github.io/2016/Functional-Programming-Concepts-Idioms-and-Philosophy/', 'user_reports': [], 'visited': False}, 'kind': 't3'}


Exploring the dictionary python_top

The list of posts is inside the "children" key, which is inside the "data" key.

Output
<class 'dict'> dict_keys(['data', 'kind']) dict_keys(['modhash', 'before', 'after', 'children']) 2

How to extract the list of posts now?

ups - num. of people who upvoted
id - reddit's unique ID for the post

script.py
Output
13 4b7w9u 53

Getting post comments (Comments on a single article)

Now we have the id for the most upvoted post. Comments for this post can be retrieved using the endpoint.

API Documentation says - Get Comments API

GET [/r/subreddit]/comments/article


Include the above format with the API's base URL https://oauth.reddit.com

So now the full URL is https://oauth.reddit.com/r/python/comments/4b7w9u

script.py
Output
<class 'list'> 2

 Comments is a list and has two items.

Item 1 has information about the post 
Item 2 contains information about the comments

Reddit users can comment on comments. This means that comments have one more key than posts do. The additional key "replies" contains the nested comments.

Item 2 information about the comments below:

The structure of "comments" is complex, and you'll need to look for the "children" key in the second list item to find the list of comments.

{'kind': 'Listing', 'data': {'children': [{'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458525326.0, 'score_hidden': False, 'replies': '', 'name': 't1_d16wtxk', 'downs': 0, 'score': 8, 'id': 'd16wtxk', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': "Increasingly I've been writing my Python libraries in a functional way but with an object-oriented API where methods just call the appropriate function.\n\nI was motivated to do this because I found classes with lots of lines of code difficult to work with.\n\nAlso it made tests easier to write.\n\nNamed tuples are also very useful (they're like immutable dicts/objects).", 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458496526.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't3_4b7w9u', 'author': 'mcilrain', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;Increasingly I&amp;#39;ve been writing my Python libraries in a functional way but with an object-oriented API where methods just call the appropriate function.&lt;/p&gt;\n\n&lt;p&gt;I was motivated to do this because I found classes with lots of lines of code difficult to work with.&lt;/p&gt;\n\n&lt;p&gt;Also it made tests easier to write.&lt;/p&gt;\n\n&lt;p&gt;Named tuples are also very useful (they&amp;#39;re like immutable dicts/objects).&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 8, 'user_reports': [], 'controversiality': 0}}, {'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458524890.0, 'score_hidden': False, 'replies': '', 'name': 't1_d16wkim', 'downs': 0, 'score': 6, 'id': 'd16wkim', 'gilded': 0, 'banned_by': None, 'author_flair_text': 'Guise I got a job, thanks! | github.com/justanr', 'mod_reports': [], 'body': "To add to this, functional programming doesn't mean *completely forbidding* objects. It's entirely acceptable to have classes and objects, you're just not allowed to mutate their state once they've been created. \n\nThink datetime, it has state but aside from some C juju (in CPython at least), you can't change that state. Instead, when you do `replace` or `astimezone`, you get a brand new datetime object.\n\nUsing immutable objects is very similar to how Haskell uses closures and even some monads. Option in Scala is implemented as an interface while Some and None are classes that implement it.", 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458496090.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': '', 'report_reasons': None, 'parent_id': 't3_4b7w9u', 'author': 'kurashu89', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;To add to this, functional programming doesn&amp;#39;t mean &lt;em&gt;completely forbidding&lt;/em&gt; objects. It&amp;#39;s entirely acceptable to have classes and objects, you&amp;#39;re just not allowed to mutate their state once they&amp;#39;ve been created. &lt;/p&gt;\n\n&lt;p&gt;Think datetime, it has state but aside from some C juju (in CPython at least), you can&amp;#39;t change that state. Instead, when you do &lt;code&gt;replace&lt;/code&gt; or &lt;code&gt;astimezone&lt;/code&gt;, you get a brand new datetime object.&lt;/p&gt;\n\n&lt;p&gt;Using immutable objects is very similar to how Haskell uses closures and even some monads. Option in Scala is implemented as an interface while Some and None are classes that implement it.&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 6, 'user_reports': [], 'controversiality': 0}}, {'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458527532.0, 'score_hidden': False, 'replies': {'kind': 'Listing', 'data': {'children': [{'kind': 't1', 'data': {'edited': 1458505300.0, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458531570.0, 'score_hidden': False, 'replies': {'kind': 'Listing', 'data': {'children': [{'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458544461.0, 'score_hidden': False, 'replies': '', 'name': 't1_d1788t8', 'downs': 0, 'score': 3, 'id': 'd1788t8', 'gilded': 0, 'banned_by': None, 'author_flair_text': 'Guise I got a job, thanks! | github.com/justanr', 'mod_reports': [], 'body': "I'd consider mutating a completely internally contained list to be functional enough. It's a pragmatic approach rather than a pure one.\n\nThe list mutation is an implementation detail. ", 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458515661.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': '', 'report_reasons': None, 'parent_id': 't1_d170h9i', 'author': 'kurashu89', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;I&amp;#39;d consider mutating a completely internally contained list to be functional enough. It&amp;#39;s a pragmatic approach rather than a pure one.&lt;/p&gt;\n\n&lt;p&gt;The list mutation is an implementation detail. &lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 3, 'user_reports': [], 'controversiality': 0}}, {'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458544581.0, 'score_hidden': False, 'replies': '', 'name': 't1_d178bf2', 'downs': 0, 'score': 3, 'id': 'd178bf2', 'gilded': 0, 'banned_by': None, 'author_flair_text': 'I &lt;3 duck typing', 'mod_reports': [], 'body': "&gt; I don't think mutating lists is very functional-like.\n\nThe function, when considered as a black box, follows functional design. In a multiparadigm language like Python, I'm happy to mix styles under encapsulation like this.", 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458515781.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': '', 'report_reasons': None, 'parent_id': 't1_d170h9i', 'author': 'minno', 'body_html': '&lt;div class="md"&gt;&lt;blockquote&gt;\n&lt;p&gt;I don&amp;#39;t think mutating lists is very functional-like.&lt;/p&gt;\n&lt;/blockquote&gt;\n\n&lt;p&gt;The function, when considered as a black box, follows functional design. In a multiparadigm language like Python, I&amp;#39;m happy to mix styles under encapsulation like this.&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 3, 'user_reports': [], 'controversiality': 0}}], 'modhash': None, 'after': None, 'before': None}}, 'name': 't1_d170h9i', 'downs': 0, 'score': 9, 'id': 'd170h9i', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': "I don't think mutating lists is very functional-like.\n\nEdit:\n\nThe issue with the first example isn't that it's 'functional'; it's that it's comically cryptic. Use the remainder to index the bag? Check. Use `or` to return the accumulator to avoid having to write a multi-line function, knowing that `append` is a mutator? Check. Items in the bag are unnamed? Check. I mean.... Jesus.", 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458502770.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't1_d16y4ry', 'author': 'hotpotatobeans', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;I don&amp;#39;t think mutating lists is very functional-like.&lt;/p&gt;\n\n&lt;p&gt;Edit:&lt;/p&gt;\n\n&lt;p&gt;The issue with the first example isn&amp;#39;t that it&amp;#39;s &amp;#39;functional&amp;#39;; it&amp;#39;s that it&amp;#39;s comically cryptic. Use the remainder to index the bag? Check. Use &lt;code&gt;or&lt;/code&gt; to return the accumulator to avoid having to write a multi-line function, knowing that &lt;code&gt;append&lt;/code&gt; is a mutator? Check. Items in the bag are unnamed? Check. I mean.... Jesus.&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 9, 'user_reports': [], 'controversiality': 0}}], 'modhash': None, 'after': None, 'before': None}}, 'name': 't1_d16y4ry', 'downs': 0, 'score': 9, 'id': 'd16y4ry', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': 'I really wish the author had used [the partition recipe](https://docs.python.org/3.5/library/itertools.html#itertools-recipes) from the itertools library as partition_values, while more efficient, highlights a lot of what I think drives people away from functional programming\n\n def partition_values(vals):\n return reduce(lambda l, v: l[v % 2].append(v) or l, vals, ([], []))\n\nAlternatively, it would be a **great** place to demonstrate that functional methods can themselves utilize state that most people are quite comfortable with...\n\n def partition_values(vals):\n odds, evens = [], []\n for v in vals:\n if v % 2:\n odds.append(v)\n else:\n evens.append(v)\n return evens, odds', 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458498732.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't3_4b7w9u', 'author': 'justanotherbody', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;I really wish the author had used &lt;a href="https://docs.python.org/3.5/library/itertools.html#itertools-recipes"&gt;the partition recipe&lt;/a&gt; from the itertools library as partition_values, while more efficient, highlights a lot of what I think drives people away from functional programming&lt;/p&gt;\n\n&lt;pre&gt;&lt;code&gt;def partition_values(vals):\n return reduce(lambda l, v: l[v % 2].append(v) or l, vals, ([], []))\n&lt;/code&gt;&lt;/pre&gt;\n\n&lt;p&gt;Alternatively, it would be a &lt;strong&gt;great&lt;/strong&gt; place to demonstrate that functional methods can themselves utilize state that most people are quite comfortable with...&lt;/p&gt;\n\n&lt;pre&gt;&lt;code&gt;def partition_values(vals):\n odds, evens = [], []\n for v in vals:\n if v % 2:\n odds.append(v)\n else:\n evens.append(v)\n return evens, odds\n&lt;/code&gt;&lt;/pre&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 9, 'user_reports': [], 'controversiality': 0}}, {'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458525072.0, 'score_hidden': False, 'replies': {'kind': 'Listing', 'data': {'children': [{'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458544784.0, 'score_hidden': False, 'replies': '', 'name': 't1_d178fsz', 'downs': 0, 'score': 3, 'id': 'd178fsz', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': 'I mean, in English the proper way to express it would be "some code" instead of "a code", but I didn\'t have a hard time understanding what he meant. Looks like the author lives in Brazil so maybe it\'s just an English quirk (\'code\' always being plural when talking about software) that isn\'t true in other languages.', 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458515984.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't1_d16wofl', 'author': 'Deto', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;I mean, in English the proper way to express it would be &amp;quot;some code&amp;quot; instead of &amp;quot;a code&amp;quot;, but I didn&amp;#39;t have a hard time understanding what he meant. Looks like the author lives in Brazil so maybe it&amp;#39;s just an English quirk (&amp;#39;code&amp;#39; always being plural when talking about software) that isn&amp;#39;t true in other languages.&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 3, 'user_reports': [], 'controversiality': 0}}, {'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458547093.0, 'score_hidden': False, 'replies': '', 'name': 't1_d179ufj', 'downs': 0, 'score': 2, 'id': 'd179ufj', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': "I'm with you. I also think the same thing every time I read it.", 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458518293.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't1_d16wofl', 'author': 'FoolofGod', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;I&amp;#39;m with you. I also think the same thing every time I read it.&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 2, 'user_reports': [], 'controversiality': 0}}], 'modhash': None, 'after': None, 'before': None}}, 'name': 't1_d16wofl', 'downs': 0, 'score': 3, 'id': 'd16wofl', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': '"A code"? That grates on me so bad every time I read it. I don\'t even know what it is (in the context of programming).', 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458496272.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't3_4b7w9u', 'author': 'larsga', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;&amp;quot;A code&amp;quot;? That grates on me so bad every time I read it. I don&amp;#39;t even know what it is (in the context of programming).&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 3, 'user_reports': [], 'controversiality': 0}}, {'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458542450.0, 'score_hidden': False, 'replies': {'kind': 'Listing', 'data': {'children': [{'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458542723.0, 'score_hidden': False, 'replies': '', 'name': 't1_d1776xd', 'downs': 0, 'score': 1, 'id': 'd1776xd', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': "You are correct. I just wanted to make a smoother introduction to FP at, as it seems, the cost of correctnes. I'll probably fix, as it was already pointed out on [other subreddit](https://www.reddit.com/r/programming/comments/4b8gcm/functional_programming_philosophy/d16yqya)", 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458513923.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't1_d1770vs', 'author': 'ingvij', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;You are correct. I just wanted to make a smoother introduction to FP at, as it seems, the cost of correctnes. I&amp;#39;ll probably fix, as it was already pointed out on &lt;a href="https://www.reddit.com/r/programming/comments/4b8gcm/functional_programming_philosophy/d16yqya"&gt;other subreddit&lt;/a&gt;&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 1, 'user_reports': [], 'controversiality': 0}}], 'modhash': None, 'after': None, 'before': None}}, 'name': 't1_d1770vs', 'downs': 0, 'score': 2, 'id': 'd1770vs', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': 'I believe that what the author is referring to as monads are actually called functors.', 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458513650.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't3_4b7w9u', 'author': 'unimpaired', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;I believe that what the author is referring to as monads are actually called functors.&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 2, 'user_reports': [], 'controversiality': 0}}, {'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458541719.0, 'score_hidden': False, 'replies': {'kind': 'Listing', 'data': {'children': [{'kind': 't1', 'data': {'edited': False, 'subreddit': 'Python', 'archived': False, 'stickied': False, 'created': 1458542535.0, 'score_hidden': False, 'replies': '', 'name': 't1_d1772pi', 'downs': 0, 'score': 2, 'id': 'd1772pi', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': 'Sorry for that, You can continue your reading now, sir.', 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458513735.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't1_d176l9s', 'author': 'ingvij', 'body_html': '&lt;div class="md"&gt;&lt;p&gt;Sorry for that, You can continue your reading now, sir.&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 2, 'user_reports': [], 'controversiality': 0}}], 'modhash': None, 'after': None, 'before': None}}, 'name': 't1_d176l9s', 'downs': 0, 'score': 0, 'id': 'd176l9s', 'gilded': 0, 'banned_by': None, 'author_flair_text': None, 'mod_reports': [], 'body': '&gt;Should it alter any of the parameters that **where** supplied? No\n\nI feel like a complete tool for not continuing to read an article when I stumble such grammar mistake :(\n\nedit: not implying my grammar is error free', 'num_reports': None, 'distinguished': None, 'approved_by': None, 'created_utc': 1458512919.0, 'subreddit_id': 't5_2qh0y', 'link_id': 't3_4b7w9u', 'removal_reason': None, 'likes': None, 'author_flair_css_class': None, 'report_reasons': None, 'parent_id': 't3_4b7w9u', 'author': 'resamune', 'body_html': '&lt;div class="md"&gt;&lt;blockquote&gt;\n&lt;p&gt;Should it alter any of the parameters that &lt;strong&gt;where&lt;/strong&gt; supplied? No&lt;/p&gt;\n&lt;/blockquote&gt;\n\n&lt;p&gt;I feel like a complete tool for not continuing to read an article when I stumble such grammar mistake :(&lt;/p&gt;\n\n&lt;p&gt;edit: not implying my grammar is error free&lt;/p&gt;\n&lt;/div&gt;', 'saved': False, 'ups': 0, 'user_reports': [], 'controversiality': 0}}], 'modhash': None, 'after': None, 'before': None}}

Getting the most upvoted comment

script.py
Output
6 d16y4ry 9


Upvoting a comment in Reddit

Endpoint: /api/vote
Parameters: dir - vote direction: 1, 0 or -1 and id of the post

script.py
Output
201

Status code 201 means the POST request has been fulfilled. 

Here are the takeaways from this article

  • Reddit APIs
  • Authenticating with the API using OAuth
  • Get the list of trending posts in subreddit (Most Upvoted Post)
  • Get the comments of the post
  • Getting the most upvoted comments
  • Posting a comment

Skills Acquired:
  • Authenticating using OAuth
  • How to explore complex dictionaries
  • How to extract list from Python dictionary
  • Logic to find the post with most upvotes
  • How to upvote a comment on Reddit using their vote API
Tools Used:
  • Python requests library - requests.get(), requests.post()
  • Python .json()
  • OAuth Authorization headers

Thank you!










No comments: