Why is Python Easy to Build a Reddit Bot

Reddit Bot Development 101

Photo by Brett Jordan on Unsplash

For a coding competition I recently took part in, I was asked to create a product or concept that would benefit society, while still being innovative and accessible. After some consideration, I ended up with the idea of creating a Reddit bot that tells people COVID-19 statistics for a particular region the original poster intends to travel to. The catch is, I didn't want the bot to be invoked consciously, rather, I wanted the bot to automatically detect the user's destination, retrieve well-maintained data, and post it as a comment in an unobtrusive manner.

While I was doing research on how to create a bot as per my requirements, I didn't find the information I needed at a single source. So in this article, I hope to fill that gap by explaining the exact step-by-step process I took in creating my bot, and hopefully this helps you out as well!

The Software You'll Need

Photo by Timothy Dykes on Unsplash

In this article, I'll be looking at how to make a bot using Python, version 3.6 or higher to be specific, so make sure you have that installed on your computer.

The next thing you'll be needing is PRAW — the Python Reddit API Wrapper. PRAW is a Python package that allows you to access Reddit's APIs in the easiest way possible. It has been designed to follow all of Reddit's API guidelines, so breaking the rules is a nonissue. Installing PRAW is also extremely easy; just type pip install praw into your Command Prompt Terminal. pip is the Python package installer. If you receive an error saying you don't have pip installed, then learn how to install it here.

How Our Bot Sees a Reddit Page

Now that we've got all the software we'll need in place, let's take a quick look at how the Reddit API really works. It's quite simple, to be honest. Essentially, Reddit gives all the data on a Reddit page in the .json format, which you can parse in Python the way you would a dictionary.

I created the subreddit r/SpaceJam2021 for testing my bot; here's how it looks to you, and here's how it looks to our bot.

Registering Our Bot on Reddit

The next step in creating our bot is to register it on Reddit. We do this by creating a Reddit app. Sign into the account you would like to operate your bot out of (create a new account if you'd rather the bot remain separate from your personal account), and go to this page. Click on create an app at the bottom of the page. Here you'll be prompted to fill in a name, and a redirect uri. Choose the bullet point script, and leave the other text boxes blank (they're optional). The redirect uri is irrelevant to our Reddit bot, so use a dummy URL like example.com.

The page it takes you to gives us the user ID (under the text personal use script) and the secret key. These two pieces of information will be useful for later.

Updating the praw.ini File With Our Bot's Details

This part may be a little complicated, especially if your computer is cluttered. You're gonna have to find your Python folder, navigate to the Lib folder, then the site-packages folder, and find the praw folder.

In the praw folder, you'll see a file named praw.ini. We have to add our bot's details to this file, but I would recommend avoiding changing the original praw.ini file, just in case we mess something up. So create a copy of praw.ini, and then open the original one.

Open praw.ini with notepad, and add the following lines at the bottom:

bot1 is just to differentiate our bot from existing/future bots of yours. Add client ID and client secret after the respective '=' signs (in single or double quotes). username and password correspond to those of the account your bot is to operate out of, and user_agent is just a random name you can give your project. All the details you fill in here are sensitive, which is why we add it to praw.ini instead of the code itself.

On a side note, if you couldn't find the praw folder, and since the File Explorer search is quite underwhelming, I recommend using Wox for Windows.

Testing Out Our Bot's Ability to Read Posts

The PRAW package has plenty of attributes, which you can find here. But for testing the bot's ability to read we need the following lines of code:

Let's go through the lines one-by-one:

  • import praw imports the PRAW package to our code.
  • r = praw.Reddit('bot1') essentially gives bot1 access to Reddit.
  • We use r.subreddit("SpaceJam2021") to indicate which subreddit our bot will be functioning in. You can replace SpaceJam2021 with the subreddit of your choice.
  • limit = 5 means we will see only the first five posts of the page. We can use hot, top, new, or rising to sort the posts.

You can try it out, and leave any findings or suggestions in the comments.

That's all for this article. In the next one, I'll explain how the interactive part of the bot works. Let me know if this was helpful!

biggshight1960.blogspot.com

Source: https://medium.com/analytics-vidhya/a-comprehensive-guide-to-creating-a-basic-reddit-bot-part-1-15fb0e4cebcb

0 Response to "Why is Python Easy to Build a Reddit Bot"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel