Press "Enter" to skip to content

Introduction and Basics – Python Reddit API Wrapper (PRAW) tutorial p.1

00:00:01what is going on everybody and welcome

00:00:03to a Python reddit API wrapper or pro

00:00:06tutorial series if you don’t know what

00:00:09reddit is ready to just a place with

00:00:13well welcome to the Internet so anyway

00:00:16reddit comm is like it’s almost like a

00:00:17forum for just pretty much any

00:00:19contextual information so there are

00:00:22basically just a bunch of sub reddits

00:00:24that comprise reddit and then a

00:00:25subreddit is basically the contextual

00:00:27part where you’ve got sub reddits for

00:00:29like Python programming news politics

00:00:33technology up just and pretty much

00:00:34everything you can think of and if you

00:00:36can’t think of it you can make your own

00:00:37subreddit for it so anyways that’s right

00:00:40calm if you don’t have an account go

00:00:42ahead and sign up just fill out the form

00:00:43and all that I already have an account

00:00:46for this tutorial so I’m going to go

00:00:47ahead and log in it is pron tutorial

00:00:52password is cookies don’t hack Network

00:00:58so now we’re going to go to preferences

00:01:01once you’ve created an account go to

00:01:03apps and then go ahead and create an app

00:01:06and this app needs to be a script and

00:01:08then we can give it a name I’m just

00:01:10going to call it fraud tutorial it’s

00:01:12tough toriel and we’re going to say in

00:01:15the description tutorial on lip for all

00:01:18brawl brawl thank you and then for this

00:01:22will just do Python program you got

00:01:24Annette cool okay so now we’ll go ahead

00:01:28and create an app and then here is your

00:01:30client ID and then this is your secret

00:01:33I’m going to move this over for now but

00:01:35will be that’s useful information that

00:01:37we’re going to need momentarily so what

00:01:42we’re going to be using is prompts so

00:01:43this these are the documents for pry you

00:01:46should just it’s a pip install process

00:01:48of rot and these are the documents but

00:01:52if you if you just search for like pron

00:01:54google you’ll probably get documentation

00:01:55for 3.6 what you actually want to get is

00:01:58just the the latest documentation so if

00:02:00you do go through the documentation just

00:02:02make sure it’s the latest one because

00:02:04lots of things changed

00:02:05so anyways moving this aside in fact I

00:02:07think I might even just close this I

00:02:09think I’m all done

00:02:09it what I’m going to do now is open up

00:02:14here broad tutorial make sure it fits on

00:02:20my page and we’re just going to like

00:02:23doing a really quick example of clearing

00:02:26some information so make sure it’s fits


00:02:33so first thing we’re going to go ahead

00:02:35and do is we need to import pro and then

00:02:41once we’ve imported probably got to like

00:02:43setup it like a reddit instance

00:02:45basically so I’m going to say reddit

00:02:47equals prof. dots reddit for reddit

00:02:54instance and then you’re going to need

00:02:55client ID client secret and we should

00:03:00price it because we’re going to fill

00:03:02these in in just a moment and then you

00:03:07need a username password and a user

00:03:13agent I’m not sure why you have to have

00:03:17a user agent but you do I think


00:03:27so client ID is what I just showed you

00:03:30guys a moment ago so this is my client

00:03:32ID right here this this boom secret

00:03:38right here username is prostitute Oreo

00:03:43prom tutorial password was cookies a

00:03:47user agent I mean you can put anything

00:03:50in here so I’m not really sure pretty

00:03:52sure at one point like I’ve used just

00:03:54the straight reddit API before and you

00:03:56actually had to have just anything in

00:03:58user agent but not sure what’s required

00:04:00anyway there you go so now we’ve got our

00:04:03little reddit instance and we are ready

00:04:06to do stuff with it so for example like

00:04:10I was saying before red is comprised of

00:04:12a bunch of subreddit so let’s go ahead

00:04:14and specify a subreddit so I’m going to

00:04:16say subreddit equals reddit dot write it

00:04:20and a subreddit we’re going to choose is

00:04:22Python now let’s say we want to because

00:04:29like I’ve shown you guys before like if

00:04:30we go to Reddit calm Erica Python right

00:04:35here you can see like you can sort of

00:04:39things in different ways like hot is a

00:04:41combination basically of votes to time

00:04:44new is just purely time what’s the

00:04:47newest first kind of a forum or

00:04:49something like that rising is just stuff

00:04:51that is rising quite quickly

00:04:53controversial is just anything that has

00:04:55a combination of lots of up votes and

00:04:57down votes top is I think just just

00:05:02votes and plastic time much they’re not

00:05:05positive with the difference between hot

00:05:06and top is someone wants to post below

00:05:10what the difference between hot and top

00:05:11is one being known definitely ordered by

00:05:14votes for sure I maybe it just favors

00:05:18both slightly more but as you can see

00:05:20you don’t really see too many more may

00:05:23be tough oh this is pop from the last 24


00:05:25okay so then we could say top from the

00:05:28past year for example yeah I think these

00:05:32are just like top posts from everyone in

00:05:34the last year

00:05:35cool so who is this

00:05:39cool guy anyway uh so you can do like

00:05:43any of these that you wanted now

00:05:45actually I’m not positive how to do this

00:05:47you can probably check the documents

00:05:48though for how to do this next like

00:05:51links from XYZ anyways we’re going to

00:05:54look for just hot so we’re going to say

00:05:56hot Python equals sub reddit dot hot and

00:06:02then what we’re going to do is we’re

00:06:04going to just kind of go through these

00:06:05so for example we could say like like

00:06:09dot Hut I forget what the default is I

00:06:12don’t know if like 30 or 200 or I don’t

00:06:14know what the default number is but you

00:06:15can also pass in the limit here so you

00:06:17can say limit equals 1 so that will just

00:06:19return the the hottest Python

00:06:22information at the moment so what we can

00:06:24do now is iterate through whatever the

00:06:26return is so this is 1 but we could say

00:06:28I don’t know 5 so 4 for submission in

00:06:34hot Python we can just let’s print that

00:06:39submission and see what we what we’re

00:06:41working with at the time okay so here

00:06:46are these submissions so these are

00:06:49basically your IDs so this is a thread

00:06:51ID for whatever these topics are we

00:06:54don’t really know too much more about

00:06:55them now I happen to know it’s an object

00:07:01you might not know it’s an object it

00:07:02could just be an ID so I mean with with

00:07:04any module if you like if you don’t

00:07:06really like reading Docs you can just do

00:07:08derp and that will give you all the

00:07:10attributes so for example this is going

00:07:13to put out a lot of dirt but anyway

00:07:15let’s just go up to the top so this is

00:07:17like one of the dirt should have just

00:07:19done one but anyways here you can

00:07:21clearly see ok here seems like these are

00:07:23a bunch of done ders up here but as you

00:07:24come down here and we can see right away

00:07:26like ok approved when or whatever and a

00:07:30lot of this stuff is like you know cake

00:07:32when was created created et Cie delete

00:07:34which it would have be years definite

00:07:36rights to delete it

00:07:38but you can get try to find something

00:07:41useful here that’s typical like you can

00:07:42reply to it

00:07:43you can check how many votes as you can

00:07:47upvote it although don’t do that

00:07:48as but you can grab the title anyway I’m

00:07:56not going to focus too much on that but

00:07:57anyways if you want to like you can just

00:07:58kind of go through all the things but so

00:08:01for example let’s just grab the title so

00:08:03print rather than their submission and

00:08:05rather than submission which is just the

00:08:06ID but it’s actually an object we can

00:08:09reference dot title so the ad like just

00:08:12with anything in Python the attribute

00:08:13title should be populated now pretty

00:08:17much everything you see in improv that

00:08:19has you know basically it’s a function

00:08:21call you can pretty much guess that’s

00:08:23going to be yet another API called it so

00:08:25that data might not exist just yet let’s

00:08:27go ahead and run that again and there

00:08:31you go so you’ve got the first five so

00:08:33this one’s the official job board

00:08:36what’s everyone working on this week and

00:08:38if we go here let’s go to high we’ll see

00:08:43that these are not actually like these

00:08:45are um these are stickies so we might

00:08:48not actually want those right we might

00:08:51not be interested in that information so

00:08:53one of those things was one of the

00:08:57attributes rather is sticky so it’s just

00:09:00a boolean value so for example we could

00:09:03take if not Submission dot sticky sticky

00:09:07print submission dot title

00:09:10so now we should just get the non sticky

00:09:12ones so there you go so I guess he’s

00:09:18just kind of live it should just be

00:09:19three I think it’s just because of

00:09:21wrapping yeah so anyways even though we

00:09:24went through five we still we only post

00:09:26the title to three of them because those

00:09:28first two were just stickies so um so

00:09:32now we can get all kinds of other

00:09:33information on them so like rather than

00:09:35just submission title we could we could

00:09:37get all kinds of stuff like for example

00:09:39we could say you know title and food


00:09:45not semicolons curly braces downs which

00:09:49honestly downs doesn’t seem to work

00:09:51we’ll see that soon enough but have we

00:09:55visited visited curly brace now dot

00:10:04format and then just same as before

00:10:07submission title submission dot UPS

00:10:10submission dot downs submission dot

00:10:15visited cool so we’ll run that again

00:10:23okay so for example title guide on how

00:10:27to create a Twitter bot with Python so

00:10:29it’s a little bit off screen but yeah

00:10:31anyway this has visited false well let’s

00:10:33see what happens if we click on it here

00:10:38I can’t remember if you view the

00:10:40comments or not surely it would still it

00:10:42would say you visited but let’s find out

00:10:47just as false maybe I got to actually

00:10:51click the link I don’t click on it

00:10:58let’s try again let’s try again the

00:11:02other thing I wanted to point out was

00:11:03the ups and downs they’re kind of funky

00:11:05okay so since we haven’t visited I sort

00:11:10of tested that in the past and it was

00:11:11fine maybe you have to visit it via the

00:11:16API I don’t know anyway um literally

00:11:19everything has down to zero unless it’s

00:11:21in the negatives so maybe the pro hasn’t

00:11:24like updated to like a new change in the

00:11:27subreddit API I really don’t know

00:11:29anyways on any submission you can get

00:11:34all kinds of information you can do all

00:11:36kinds of stuff so just add some examples

00:11:39you know here we just grabbed a bunch of

00:11:42information but you can also you can do

00:11:44all kinds of things you can take action

00:11:45so you can do like vote right you can do

00:11:48dot down vote although as a bot again

00:11:51you wouldn’t do that the only times you

00:11:53should really use these commands or like

00:11:55if you make some sort of reddit like a

00:11:57phone app or something like that and

00:11:58it’s like a human that decides to do it

00:12:00so that’s doing this would be like

00:12:03against the Terms of Service basically

00:12:05you can reply there’s nothing in the

00:12:09docs that says a robot can’t apply reply

00:12:12sorry and then you can also subscribe

00:12:15and unsubscribe so at least some things

00:12:17that we could do we could make a reply I

00:12:19just don’t really know what to reply but

00:12:21we can also subscribe so for example we

00:12:23could say subreddit dot subscribe and

00:12:26that should subscribe us to the Python

00:12:31subreddit let’s go ahead and run that

00:12:33real quick so suppose we have visited

00:12:38your trading but let’s see if we

00:12:40subscribed at least so initially we’re

00:12:45not subscribed to the Python subreddit

00:12:46we are subscribed we could I think we

00:12:50should get away this subreddit dot


00:12:54and now if we were to refresh this page

00:13:03we need to subscribe I swear I’m

00:13:07enjoying all my coffee this morning so

00:13:09subscribed one more time I’m pretty sure

00:13:11it said unsubscribe but the button

00:13:12I don’t think changed color that’s

00:13:14what’s tripping me up right now usually

00:13:16it’s like red or green yeah okay now we

00:13:19have an unsubscribe I have to go back

00:13:21and look and see if it said anyway we

00:13:24have subscribed did I do just a dot no I

00:13:28actually ran it so I don’t know should

00:13:29have subscribed but that’s weird cuz it

00:13:31was like it never changed color released

00:13:34I don’t think so I might be losing my

00:13:36mind but anyway okay so that’s like a

00:13:39quick kind of introduction to the Python

00:13:42reddit API from here you could just read

00:13:44documents or use dirt and attributes and

00:13:46maybe figure a bunch of stuff out on

00:13:47your own otherwise in the next tutorial

00:13:49we’re going to be kind of going through

00:13:50and really honing in on actually

00:13:53navigating like the comments because

00:13:55navigating the comments is significantly

00:13:58more challenging than navigating the

00:14:02gist submissions right the submissions

00:14:04are always ordered very nicely whereas

00:14:05comments are like entry form it’s kind

00:14:08of a recursion problem and it can just

00:14:11kind of be a little more challenging so

00:14:13anyways that’s we’re gonna be doing a

00:14:14next tutorial if you get questions

00:14:15comments concerns whatever feel free to

00:14:16even below otherwise I’ll see you in the

00:14:19next door