Note: This article is a little outdated. It uses an older version of PRAW. If you are interested in using PRAW 4, I suggest you read my new article, which is a lot more up-to-date and comprehensive: Creating a Reddit bot that can reply to comments using PRAW4.
Ever read a comment from a Reddit bot? It is very likely that you have, if you are an avid Redditor. You must have noticed some of the famous ones like /u/imirror_bot and /u/xkcdcomic_bot. For a longer list of Reddit bots, check out this comment on Reddit.
So, how do you make one?
Well, it is very simple to make a bot. Most bots are written in Python using an open-source library named PRAW. And today, we will be using just that. If you know a little bit of Python, you are good to go.
Step 1 : Install PRAW
Now, I am going to assume you are using Ubuntu. Just type out the following on the command prompt,
Bots are generally designed to automate one task. Let us create a bot that goes through all the comments on a subreddit, and lists the users that use the words "thanks" and "please". So, we are finding all the users of a subreddit who are polite.
Step 3 : The algorithm
Here's how most bots work,
Step 4 : Now for the actual code
This code should be self-explanatory. The in-line comments should help you if something is not clear.
As always, please comment and share.
Ever read a comment from a Reddit bot? It is very likely that you have, if you are an avid Redditor. You must have noticed some of the famous ones like /u/imirror_bot and /u/xkcdcomic_bot. For a longer list of Reddit bots, check out this comment on Reddit.
So, how do you make one?
Well, it is very simple to make a bot. Most bots are written in Python using an open-source library named PRAW. And today, we will be using just that. If you know a little bit of Python, you are good to go.
Step 1 : Install PRAW
Now, I am going to assume you are using Ubuntu. Just type out the following on the command prompt,
sudo apt-get install python-pip #in case you don't have pip installed sudo pip install prawStep 2 : Decide what your bot should do
Bots are generally designed to automate one task. Let us create a bot that goes through all the comments on a subreddit, and lists the users that use the words "thanks" and "please". So, we are finding all the users of a subreddit who are polite.
Step 3 : The algorithm
Here's how most bots work,
- Fetch comments
- Process those comments
- Sleep for sometime
- Go to 1
Step 4 : Now for the actual code
This code should be self-explanatory. The in-line comments should help you if something is not clear.
# author: Hathy (WhyCouch) import praw import time # Initialize PRAW with a custom User-Agent r = praw.Reddit('Simple comment parser from WhyCouch') polite_users = set() # to avoid duplicates for i in xrange(0,10): # Run the loop 10 times comments = r.get_comments('askreddit') for comment in comments: body = comment.body.lower() if body.find("thank") != -1 or body.find("please") != -1: polite_users.add(comment.author) time.sleep(120) # Sleep for 2 minutes print "The polite users were :" for user in polite_users: print userYour output would look something like this,
The polite users were : ChaletMontagne Brackit-That is all there is to it, and I am sure you too were able to create it in less than 10 minutes. You now have your own little Reddit bot that performs a meaningful action. Refer to the PRAW documentation if you want to create more advanced bots. I will soon be creating a new tutorial, that will tell you how to create a bot that can post comments.
As always, please comment and share.
Please share the code for posting comments.
ReplyDeletewhat is the point of the string inside praw.Reddit?
ReplyDeleteAccording to the comment
Delete># Initialize PRAW with a custom User-Agent
it initializes the user-agent which is required as a first step (see https://praw.readthedocs.io/en/stable/)