How to, Programming

How to create a Reddit bot using python(Praw)?

python programming

Reddit is a social networking site based on people’s interests. It is estimated that Reddit has more than 430 million users.  In this tutorial, we will learn to make a Reddit bot that translates English words to Nepali. We will use the Google translator package to do so. Reddit bot is a program that scans a particular subreddit in which the bot is deployed, gathers information, and responds accordingly to the task assigned to it. You must have basic python knowledge to understand this tutorial. I hope to make this tutorial as easily understandable as I can. Let’s get into the coding.

First of all, you need to install praw, PyDictionary, and googletrans. To do so type the following command on your terminal:

$ pip install praw
$ pip install PyDictionary
$ pip install googletrans

Now let’s import packages to our project:

import praw
from PyDictionary import PyDictionary
from googletrans import Translator
import enchant

Like our previous tutorial on twitter bot, we need to generate credentials.

Create an application

Go to the site :

https://ssl.reddit.com/prefs/apps/ and choose “are you a developer? create an app..” section.

create application
  • Type the name of your application.
  • Select the script as an option.
  • Give a description of the functionality of your bot.
  • You can give any random link in “redirect uri”.

Once you create an app you are given a secret and personal use script. Copy it, we will use it later in our code.

Secret and personal use script generated

Use the credentials we just generated below:

reddit = praw.Reddit(client_id='',
                     client_secret='',
                     user_agent='',
                     username='',
                     password=''

Let’s initialize the packages:

dictionary = PyDictionary()
translator = Translator()

Declare the subreddit you want your bot to deploy:

subreddit = reddit.subreddit('pythonforengineers')

Declare the phrase to activate the bot:

keyphrase = '!translate '

Now look for the phrase and reply appropriately :

comments=subreddit.stream.comments()
for comment in comments:

    if keyphrase in comment.body:
        word = comment.body.replace(keyphrase, '')
        try: 
words = translator.translate(word,dest='ne',src='auto')
 #here we have given dest='ne', which will translate to nepali , for spanish you can use 'es' 
reply = words.text
                comment.reply(word + ': ' + reply + 'I learnt this from wodan.xyz tutorial')
print('posted')
                
   except:
            print('to frequent')

Now we have successfully created the Reddit bot. Happy botting!

Full code:

import os

import praw
from PyDictionary import PyDictionary
from googletrans import Translator
import enchant

reddit = praw.Reddit(client_id='',
                     client_secret='',
                     user_agent='',
                     username='',
                     password=''
                     )

dictionary = PyDictionary()
translator = Translator()



# the subreddits you want your bot to live on
subreddit = reddit.subreddit('pythonforengineers')

# phrase to activate the bot
keyphrase = '!translate '

# look for phrase and reply appropriately
comments=subreddit.stream.comments()
for comment in comments:

    if keyphrase in comment.body:
        word = comment.body.replace(keyphrase, '')
        try:
            # if isWord(word):
                # get meaning as object, get the index of a sentence and reply it
                words = translator.translate(word,dest='ne',src='auto')
                # reply = [item[0] for item in words.text()]
                reply = words.text
                comment.reply(word + ': ' + reply + 'I learnt this tutorial on wodan.xyz ')
                print('posted')
                # else:
                #  reply = 'This is not a word.'
                # comment.reply(reply)
                #   print('posted')
        except:
            print('to frequent')

Also check out: Python | How to download all the images from the website?

Spread the love

Leave a Reply