Monday, 12 May 2014

How to create a simple Reddit Bot in 10 minutes

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,
sudo apt-get install python-pip   #in case you don't have pip installed
sudo pip install praw
Step 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,
  1. Fetch comments
  2. Process those comments
  3. Sleep for sometime
  4. Go to 1
Most bots work on entire subreddits, instead of just one post. For this tutorial, we will be using /r/AskReddit.

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 user
Your 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.

3 comments:

  1. Please share the code for posting comments.

    ReplyDelete
  2. what is the point of the string inside praw.Reddit?

    ReplyDelete
    Replies
    1. According to the comment

      ># 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/)

      Delete