Program a Reddit Bot - Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome clarity coders and guests today we have a fun little project that I've been working on we're going to create an automated reddit bot if you haven't ever used reddit reddit is a news and kind of social media site where you can find information and they have a community that posts on different topics and they have sub reddits for almost any topic you can think of so there might be a subreddit for sports or programming or pictures whatever you would like I'm gonna walk you through today how to either read data from reddit into a program and even how to make your own bot that comments on reddit posts so to start out let's talk about the tools I'm going to be using I'm going to be working in spider IDE and I'm going to be working with Python 3.7 if you don't have Python 3 or you don't have an IDE to work in the simplest way to do this would be to grab the distribution of anaconda anaconda is going to come with a version of Python that will support this tutorial and it will also give you the exact same editor I'm using right here if you already have your own editor that you like and you already have Python installed great you'll be ready to jump right in with me so let's start this out by taking a quick peek at reddit itself all right you can see here that I have reddit pulled up this is the home page of Reddit so we have a bunch of different sub reddits that all populate this first page and if you look at these individual posts you can see that they belong to specific sub reddits like this one is politics so we're gonna go ahead and sign up for a reddit account now if you already have a reddit account and you use it actively I would still recommend signing up for a new account for this tutorial we're gonna be building a bot and in case you accidentally break a rule you might get your account banned or something like that so go ahead and sign up for a new one with me as well so we're gonna click sign up go ahead and type your email here and then we're gonna pick a username for our bot so choose anything you would like I'm gonna do happy post bot because I'm going to be making some inspirational posts with my bot and select a password and then go ahead and click sign up it's gonna give you some subreddits here you can join for our purposes we can leave this as is right now so I'm just gonna click finish and you can see that I've logged into my new account here and now I want to learn how to create a bot so to do that we're gonna use reddit's api and there's a wrapper built for Python for that API so I'm going to search here for reddit Python Docs and you can see it brought me up to this PR aw the Python reddit API wrapper let's take a peek at that now this is actually a really good set of documentation so after this tutorial is over if you want to do something else that I didn't cover here which is highly likely you can edit this program to do anything you want using this API so don't be afraid to use this we're gonna go ahead and just click on quick start and if you scroll down you can see the code that we're going to use to create our reddit instance so I'm just gonna copy this and paste it right into my Spyder window here and you'll see we need a client ID a client secret and a user agent so back on reddit we're gonna go to reddit.com slash prefs slash apps and you'll see this tiny icon up here that says are you a developer create an app and we are so we're gonna create a name for our app we're gonna call it happy bot app this is just a script that we're going to use and you can see here that it will want a redirect URI it'll require it and our app isn't going to use it it's just gonna be a little script so we can you just use localhost here and then click create app and you can see it's created our app here now keep in mind I am going to disable this count afterwards so you have to create your own account if you try to use my secret key or my client ID it will not work so go ahead and follow along with these steps and create your own account if you want this to work like it's doing in my tutorial here so going back to our code here we need to grab a client ID that client ID is this number up here so I'm gonna copy this remember one last warning you are going to have to use your own client ID so don't try to type this one in and then we're gonna grab our secret key here as well now this last user agent part reddit recommends that you name this a unique name so we're gonna start with the kind of app it is we're gonna say it's a console app I'm just gonna name it happy and I'm gonna name I'm gonna say it's version 1.0 awesome now this should be all you need to grab data from Reddit and read it out to your program so let's try and do that the first thing I'm going to do is I'm going to use our reddit instance that we just created up here to grab a subreddit and I'm gonna create a variable to hold that subreddit for me so I'm gonna call this variable subreddit and you can call it whatever you want it just makes sense to me and I'm gonna use my instance of Reddit and I'm gonna grab a subreddit using their subreddit function that's built into this wrapper and I'm gonna pass in whatever subreddit I want to go to and if I run this you'll notice it didn't find the module for our wrapper here so if you haven't installed this library you need to do this now now if you're using spider and anaconda like me you can do it right here in this command window if not you can do it in command prompt or however you would typically install packages so I'm gonna do pip install brah we're gonna let that run cool I'm gonna run this again and you'll notice it didn't have any airs but it also didn't do anything you might also be wondering how I knew to do reddit dot subreddit well I knew to do that by using the documentation that explains how to grab a subreddit you can see here that they do dot subreddit learn Python so again you can find all this in the docs I didn't know this and when you want to change stuff in this program you're going to have to reference the documentation as well ok so we got our subreddit now let's try and print something out to the screen so let's do a little for loop to loop over our subreddit so we're gonna say for post in subreddit and now we got to say how we want to order these items in our subreddit so you'll see here that if we look back at our television subreddit there's a bunch of posts here and we can order them by hot new or top the default is hot so that's what we're going to use so if I jump back here I say how I want to order them I'm gonna say dot hot and again I knew this was a function on it because I looked at the documentation and I'm gonna limit this to just the first ten results now each time I loop through this I want to print my post dot title again I picked that variable post I could have called it anything I wanted but it's going to be an individual post in this subreddit and then I'm going to use dot title to grab the title of that post again how did I know that it had that attribute the documentation so if I run this you'll notice it's a little messy but we do look like we're getting some data back from Reddit awesome I'm just adding this in just to make it a little more readable for us and you'll notice ok cool we're getting these posts back I wanted to talk a little bit about the goal of my program in particular so my goal here I'm going to create a bot that looks through all the comments on an individual post so it's going to go to an individual post like this Seinfeld one and it's gonna look at all the top level comments so all the first comments here and what I'm gonna have it do is look to see if they say anything with the word sad in it just s ad with a space before and after and if they do say sad I'm gonna make a bot reply to that comment with an inspirational quote we got the individual titles but we need to grab the comments from that title so if we go to our documentation and we search for comment we can see comment extraction and parsing that seems right up our alley and you can see that they did the same connection as us they connected to a subreddit they got an individual submission which we have we called ours posts and then they used a for loop to go over those submissions by doing submission got comments perfect so before we get too far again we could have picked any variable but let's use the same verbage that they're using here just to help us in the future so instead of calling this post I'm gonna call it submission just to match the documentation again we don't have to this is just to make it easier on ourself and then we're gonna do another for loop to go over every comment inside each individual post so I'm gonna say for comment in submission dot comments print comment so now I'm iterating over the submission once I'm inside of an individual submission like the Seinfeld one and I'm gonna print out each individual comment inside there and this is probably going to get a little messy here so we will do a little separator there and run this and you can see that it's posting a bunch of gibberish it is grabbing a comment but it doesn't make a lot of sense to us we are printing out the comment object data so if we go back to comment in our documentation we click on the comment itself you can see the attributes that are on it so it has a author a body a created all this good stuff here so let's go back in our code and instead of printing out the comment let's print out comment dot body here we get a little err it says more comments object has no attribute body so this must be an optional attribute so we want to only print these out if they do have a body so we're gonna say if has attribute we're gonna pass in comment and then we're gonna say has to have a body and our if statement here then we're going to tab both of these lines inside of that if statement and now it should only run this code if the comment does in fact have a body awesome so this is running through now you can see it's printing out all the posts and then all the comments associated with that post so now we're really getting somewhere so now let's step back and remember what we're trying to accomplish here so on each individual comment we're now printing out every single comment but I said that I was going to try and create a bot that replies to anyone who mentions the word sad in their post and if they do I want to respond to that post with an inspirational quote so inside this if statement I don't want to work with every single comment that has a body the first thing I want to do is remove all capitalization because remember sad with a capital S is different than sad with a lowercase s but I want to treat them the same so I'm just gonna take the comment and change everything to be lowercase so I'm just gonna create a variable called comment underscore lower and I'm gonna set it equal to our comment dot body dot lower and this is gonna change it to be all lowercase now inside of this comment now that I have comment all in lowercase whatever was in the body of the comment I'm gonna say if the string space sa D space is in comment underscore lower the variable I just created then tab this inside so if sad exists in comment underscore lower then I want to print it out because these are going to be the comments that I'm going to eventually have my bot automatically post to quick question here why did I use the spaces before and after sad remember there are other words that might have sad inside of them that we don't really want to comment back on so we want to make sure it's a standalone word of sad so now if we try and run this again you can see that the comments don't come as frequently it's just giving back the comments that have sad somewhere in them awesome so now we're just grabbing the comments that say sad in them that's perfect you'll notice we're also still printing out the submission title so that's a little confusing so I'm gonna go ahead and comment these out just to help us visually a little bit I will run this one more time and now you'll see we're only printing out the individual comments and then we can eventually reply to these comments next our next objective I wanted to create a bot that replies with some inspirational quotes to someone that's sad and I found a very inspirational site called keep inspiring me cool it's about sadness I got that from googling I'm not a sponsor or anything like that at this site and you'll see that it has some inspirational quotes here by famous people of different sorts so we are going to copy some of those inspirational quotes I have pre copied these you can make whatever you would like so I'm gonna hold these quotes in a list so I'm gonna name my list sad quotes and I'm gonna set it equal to this list that I copied over and you'll see here it is just a list of strings separated by commas and I've replaced the names with Michael Scott a little office joke if you're a fan and these are the different choices that my bot is going to post to pick from to post so it's gonna pick one of these inspirational quotes to someone who wrote sad in their posts there's gonna be four sad quotes here and I want to pick one at random so I'm gonna import one more library and it is import random if you don't have random you can do pip install random if you download it and akanda you should have it already and now that I have my random library and I have my sad quotes down here when I get to a sad comment I can pick one of these quotes to randomly print out so the first thing I'm gonna do is I'm gonna get a random index and I'm gonna set it equal to random dot R and int and I'm gonna start at zero and I'm going to go to the links of sad underscore quotes so what am I doing here I am picking a random number between 0 and however long sad quotes is so in this case I have 4 quotes in here but you might add to that later so I'm going to do however long it happens to be and remember in computer science we're starting our indexing at 0 so this is index 0 1 2 & 3 so I actually am going to pick a number from 0 to 4 which we don't want to do because there is no index 4 so I'm gonna do minus 1 here and this will give me a random number between 0 & 3 0 1 2 & 3 and then after that I just want to reply to the comment with whatever I picked here so I'm going to do comment dot reply again from the documentation and I'm gonna use my sad quotes list and I'm gonna access the index at whatever random index I picked so remember lists are indexed so I'm gonna pick a random one out of our lists to reply to this comment with now if I run this I think we're gonna have an error here it says user required so everything we did up it up until this point was read-only so that worked fine but now Reddit saying hey if you want to post you got to enter some more information so you got to enter your username and your password so we're gonna grab our username that's whatever you signed up with mine was happy - post - bot so I am going to create another entry here and I'm gonna say username equals happy - post - but and then I'm going to say my password and don't get too excited here I'm going to change this is happy post bot 1 awesome so we did get an error here we got a rate limit so this comes back to some of the rules that reddit has about creating BOTS so I'm going to link you to the rules for creating BOTS and you can see that we tried to post too many times and hit a rate limit here so it's saying that we're trying to post too much and that's because there were quite a few comments with sat in it so it tried to post multiple times to those comments and reddit's making sure that we're not spamming them so they blocked us which is ok let's make one quick fix here so this can run like we expected let's import time and then after we post so down here let's do time sleep and this is going to sleep after every comment it makes I'm gonna sleep for 11 minutes just to be on the safe side so that is 660 seconds remember with this fix you're going to have to wait the original block time that we had there so you're gonna have to wait 9 minutes before this fix will work but once you have it implemented your program should sleep for 11 minutes after it comments and although it'll be a little slow it should execute correctly so let's go ahead and run this we'll let this run obviously it's gonna take some time I'll take this opportunity just to say keep in mind that this is a fun light-hearted program that we created here please stay out of the subreddits that are serious there's some about anxiety and depression we don't want to be running bots in subreddits like that alright we finished and you can see it was successful now if we go back to our reddit account here and we go to our happy bot my profile and if we pull up our bot you can see that they made two posts so in this tutorial we've talked about how to view data on reddit we've talked about how to grab an individual subreddit how to print out submissions from that subreddit and we made a cute little program about how to actually comment on fields as well now any questions on this or you want to change things please refer to the documentation that will get you there the quickest I'm also going to post a link to our website where you can sign up with an email to get our newsletter and we will have this program along with some other reddit examples in it this week for you for free all you need is your email to sign up until next time thank you guys for watching
Info
Channel: ClarityCoders
Views: 13,506
Rating: 4.9441862 out of 5
Keywords: program reddit bot, python reddit bot, reddit bot python, reddit bot guide, reddit bot, reddit api, reddit praw, reddit api python, reddit bot api, reddit bot upvote, reddit api python tutorial, python reddit api wrapper, reddit api python example, python projects, Reddit API, praw, Reddit API Wrapper, Python bot, reddit bot tutorial, python bot tutorial, how to create a reddit bot tutorial, how to create a bot, How to make a bot
Id: 3FpqXyJsd1s
Channel Id: undefined
Length: 20min 42sec (1242 seconds)
Published: Sat Feb 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.