avatarc0d3x27

Summary

This context provides a guide on how to build a Reddit bot using Python, Reddit API, and the Python Reddit API Wrapper (PRAW).

Abstract

The context begins with an introduction to Reddit and its bot-friendly environment, mentioning the prerequisites of basic Python knowledge and a Reddit account. It then discusses the Reddit API and its REST and JSON-based structure, emphasizing the importance of following Reddit's rules to avoid being banned. The guide proceeds to detail the steps for creating the bot, including naming, selecting the app type, and providing a description and redirect URL. It also explains the installation process for the Python Reddit API Wrapper (PRAW) and the configuration of DRAW files. Lastly, the context provides an example bot script and mentions the use of two-factor authentication for added security.

Opinions

  • Reddit is a bot-friendly platform that provides an API for developers to create bots.
  • Following Reddit's rules, especially when using the API for commercial purposes, is crucial to avoid being banned.
  • Each subreddit has its own regulations that bots should adhere to.
  • Reddit API is easy to use, based on REST and JSON, and doesn't require a hard setup to get it working.
  • The Python Reddit API Wrapper (PRAW) is recommended for interacting with Reddit's backend.
  • Two-factor authentication can be used for added security when creating a Reddit bot.
  • This guide is aimed at those with basic Python knowledge and a Reddit account.

How-To Build a Reddit Bot

No need to manually troll others, let your bot do it for you

Photo by Brett Jordan on Unsplash

r/

Reddit is a network of communities where people can dive into their interests, hobbies, and passions. There’s a community for whatever you’re interested in, such as social news aggregation, web content rating, and discussion website. Registered members submit content to the site, such as links, text posts, images, and videos, which are then voted up or down by other members.

Prerequisite

  • Python basic knowledge and Python install is required
  • Reddit Account

Reddit bot

Unlike many social platforms, Reddit is actually bot-friendly. It even provides an API to help developers create bots. However, keep in mind Reddit bot rules, especially if you are going to use the Reddit API for commercial purposes. Otherwise, you may be at risk of being banned. Every subreddit also has some regulations, and your bot should be in line with them.

Reddit API

Reddit API is actually easy to use, based on REST and JSON, not requiring any hard setup to get it up working. The important thing is to follow their rules, check them out here. In addition, it is advisable to be familiar with the way Reddit works to be able to determine what functions your bot has to fulfill. For more information click here

Create The Bot

— Steps

  • name: My Example App
  • App type: Choose the script option
  • description: You can leave this blank
  • about url: You can leave this blank
  • redirect url: http://www.example.com/unused/redirect/uri (We won’t be using this as a redirect)

Note: These examples will only work for script type apps, which will ONLY have access to accounts registered as "developers" of the app and require the application to know the user's password. Read more about app types.

Go to https://ssl.reddit.com/prefs/apps . There you will see this panel

create application

Create an application by typing the name of your bot, selecting the option ‘script,’ providing some description, writing any random link in the box ‘redirect URL,’ and clicking the button ‘create an app.’ Then you will get ‘personal use script’ and ‘secret’. You will use them while writing the script for your bot. Next, you need to download Python Reddit API Wrapper (PRAW) that allows you to log in to the Reddit API and interact with the backend of the website. To install PRAW;

ubuntu@Destktop: pip install praw

DRAW Configuration Files (draw.ini)

PRAW comes with a praw.ini file in the package directory, and looks for user defined praw.ini files in a few other locations:

  1. In the current working directory at the time Reddit is initialized.
  2. In the launching user’s config directory. This directory, if available, is detected in order as one of the following:
  3. In the directory specified by the XDG_CONFIG_HOME environment variable on operating systems that define such an environment variable (some modern Linux distributions).
  4. In the directory specified by $HOME/.config if the HOME environment variable is defined (Linux and Mac OS systems).
  5. In the directory specified by the APPDATA environment variable (Windows).

praw.ini uses the INI file format, which can contain multiple groups of settings separated into sections. PRAW refers to each section as a site. The default site, DEFAULT, is provided in the package’s praw.ini file. This site defines the default settings for interaction with Reddit. The contents of the package’s praw.ini file are:

[DEFAULT]
# A boolean to indicate whether or not to check for package updates.
check_for_updates=True

# Object to kind mappings
comment_kind=t1
message_kind=t4
redditor_kind=t2
submission_kind=t3
subreddit_kind=t5
trophy_kind=t6

# The URL prefix for OAuth-related requests.
oauth_url=https://oauth.reddit.com

# The amount of seconds of ratelimit to sleep for upon encountering a specific type of 429 error.
ratelimit_seconds=5

# The URL prefix for regular requests.
reddit_url=https://www.reddit.com

# The URL prefix for short URLs.
short_url=https://redd.it

# The timeout for requests to Reddit in number of seconds
timeout=16

Avoid modifying the package’s praw.ini file. Prefer instead to override its values in your own praw.ini file. You can even override settings of the DEFAULT site in user defined praw.ini files.

Example Bot Script

Two-Factor Authentication

A 2FA token can be used by joining it to the password with a colon:

reddit = praw.Reddit(
    client_id="SI8pN3DSbt0zor",
    client_secret="xaxkj7HNh8kwg8e5t4m6KvSrbTI",
    password="1guiwevlfo00esyy:955413", #as shown here
    user_agent="testscript by u/fakebot3",
    username="fakebot3",
)

To verify that you are authenticated as the correct user run:

print(reddit.user.me())

Summary

This tutorial is aimed to introduce you to the way Reddit bots are made. You should check out

Programming
Technology
Software Development
Data Science
Cybersecurity
Recommended from ReadMedium