Press "Enter" to skip to content

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


what is going on everybody and welcome
to a Python reddit API wrapper or pro
tutorial series if you don’t know what
reddit is ready to just a place with
well welcome to the Internet so anyway
reddit comm is like it’s almost like a
forum for just pretty much any
contextual information so there are
basically just a bunch of sub reddits
that comprise reddit and then a
subreddit is basically the contextual
part where you’ve got sub reddits for
like Python programming news politics
technology up just and pretty much
everything you can think of and if you
can’t think of it you can make your own
subreddit for it so anyways that’s right
calm if you don’t have an account go
ahead and sign up just fill out the form
and all that I already have an account
for this tutorial so I’m going to go
ahead and log in it is pron tutorial
password is cookies don’t hack Network
so now we’re going to go to preferences
once you’ve created an account go to
apps and then go ahead and create an app
and this app needs to be a script and
then we can give it a name I’m just
going to call it fraud tutorial it’s
tough toriel and we’re going to say in
the description tutorial on lip for all
brawl brawl thank you and then for this
will just do Python program you got
Annette cool okay so now we’ll go ahead
and create an app and then here is your
client ID and then this is your secret
I’m going to move this over for now but
will be that’s useful information that
we’re going to need momentarily so what
we’re going to be using is prompts so
this these are the documents for pry you
should just it’s a pip install process
of rot and these are the documents but
if you if you just search for like pron
google you’ll probably get documentation
for 3.6 what you actually want to get is
just the the latest documentation so if
you do go through the documentation just
make sure it’s the latest one because
lots of things changed
so anyways moving this aside in fact I
think I might even just close this I
think I’m all done
it what I’m going to do now is open up
here broad tutorial make sure it fits on
my page and we’re just going to like
doing a really quick example of clearing
some information so make sure it’s fits
cool
so first thing we’re going to go ahead
and do is we need to import pro and then
once we’ve imported probably got to like
setup it like a reddit instance
basically so I’m going to say reddit
equals prof. dots reddit for reddit
instance and then you’re going to need
client ID client secret and we should
price it because we’re going to fill
these in in just a moment and then you
need a username password and a user
agent I’m not sure why you have to have
a user agent but you do I think
[Music]
so client ID is what I just showed you
guys a moment ago so this is my client
ID right here this this boom secret
right here username is prostitute Oreo
prom tutorial password was cookies a
user agent I mean you can put anything
in here so I’m not really sure pretty
sure at one point like I’ve used just
the straight reddit API before and you
actually had to have just anything in
user agent but not sure what’s required
anyway there you go so now we’ve got our
little reddit instance and we are ready
to do stuff with it so for example like
I was saying before red is comprised of
a bunch of subreddit so let’s go ahead
and specify a subreddit so I’m going to
say subreddit equals reddit dot write it
and a subreddit we’re going to choose is
Python now let’s say we want to because
like I’ve shown you guys before like if
we go to Reddit calm Erica Python right
here you can see like you can sort of
things in different ways like hot is a
combination basically of votes to time
new is just purely time what’s the
newest first kind of a forum or
something like that rising is just stuff
that is rising quite quickly
controversial is just anything that has
a combination of lots of up votes and
down votes top is I think just just
votes and plastic time much they’re not
positive with the difference between hot
and top is someone wants to post below
what the difference between hot and top
is one being known definitely ordered by
votes for sure I maybe it just favors
both slightly more but as you can see
you don’t really see too many more may
be tough oh this is pop from the last 24
hours
okay so then we could say top from the
past year for example yeah I think these
are just like top posts from everyone in
the last year
cool so who is this
cool guy anyway uh so you can do like
any of these that you wanted now
actually I’m not positive how to do this
you can probably check the documents
though for how to do this next like
links from XYZ anyways we’re going to
look for just hot so we’re going to say
hot Python equals sub reddit dot hot and
then what we’re going to do is we’re
going to just kind of go through these
so for example we could say like like
dot Hut I forget what the default is I
don’t know if like 30 or 200 or I don’t
know what the default number is but you
can also pass in the limit here so you
can say limit equals 1 so that will just
return the the hottest Python
information at the moment so what we can
do now is iterate through whatever the
return is so this is 1 but we could say
I don’t know 5 so 4 for submission in
hot Python we can just let’s print that
submission and see what we what we’re
working with at the time okay so here
are these submissions so these are
basically your IDs so this is a thread
ID for whatever these topics are we
don’t really know too much more about
them now I happen to know it’s an object
you might not know it’s an object it
could just be an ID so I mean with with
any module if you like if you don’t
really like reading Docs you can just do
derp and that will give you all the
attributes so for example this is going
to put out a lot of dirt but anyway
let’s just go up to the top so this is
like one of the dirt should have just
done one but anyways here you can
clearly see ok here seems like these are
a bunch of done ders up here but as you
come down here and we can see right away
like ok approved when or whatever and a
lot of this stuff is like you know cake
when was created created et Cie delete
which it would have be years definite
rights to delete it
but you can get try to find something
useful here that’s typical like you can
reply to it
you can check how many votes as you can
upvote it although don’t do that
as but you can grab the title anyway I’m
not going to focus too much on that but
anyways if you want to like you can just
kind of go through all the things but so
for example let’s just grab the title so
print rather than their submission and
rather than submission which is just the
ID but it’s actually an object we can
reference dot title so the ad like just
with anything in Python the attribute
title should be populated now pretty
much everything you see in improv that
has you know basically it’s a function
call you can pretty much guess that’s
going to be yet another API called it so
that data might not exist just yet let’s
go ahead and run that again and there
you go so you’ve got the first five so
this one’s the official job board
what’s everyone working on this week and
if we go here let’s go to high we’ll see
that these are not actually like these
are um these are stickies so we might
not actually want those right we might
not be interested in that information so
one of those things was one of the
attributes rather is sticky so it’s just
a boolean value so for example we could
take if not Submission dot sticky sticky
print submission dot title
so now we should just get the non sticky
ones so there you go so I guess he’s
just kind of live it should just be
three I think it’s just because of
wrapping yeah so anyways even though we
went through five we still we only post
the title to three of them because those
first two were just stickies so um so
now we can get all kinds of other
information on them so like rather than
just submission title we could we could
get all kinds of stuff like for example
we could say you know title and food
oops
not semicolons curly braces downs which
honestly downs doesn’t seem to work
we’ll see that soon enough but have we
visited visited curly brace now dot
format and then just same as before
submission title submission dot UPS
submission dot downs submission dot
visited cool so we’ll run that again
okay so for example title guide on how
to create a Twitter bot with Python so
it’s a little bit off screen but yeah
anyway this has visited false well let’s
see what happens if we click on it here
I can’t remember if you view the
comments or not surely it would still it
would say you visited but let’s find out
just as false maybe I got to actually
click the link I don’t click on it
let’s try again let’s try again the
other thing I wanted to point out was
the ups and downs they’re kind of funky
okay so since we haven’t visited I sort
of tested that in the past and it was
fine maybe you have to visit it via the
API I don’t know anyway um literally
everything has down to zero unless it’s
in the negatives so maybe the pro hasn’t
like updated to like a new change in the
subreddit API I really don’t know
anyways on any submission you can get
all kinds of information you can do all
kinds of stuff so just add some examples
you know here we just grabbed a bunch of
information but you can also you can do
all kinds of things you can take action
so you can do like vote right you can do
dot down vote although as a bot again
you wouldn’t do that the only times you
should really use these commands or like
if you make some sort of reddit like a
phone app or something like that and
it’s like a human that decides to do it
so that’s doing this would be like
against the Terms of Service basically
you can reply there’s nothing in the
docs that says a robot can’t apply reply
sorry and then you can also subscribe
and unsubscribe so at least some things
that we could do we could make a reply I
just don’t really know what to reply but
we can also subscribe so for example we
could say subreddit dot subscribe and
that should subscribe us to the Python
subreddit let’s go ahead and run that
real quick so suppose we have visited
your trading but let’s see if we
subscribed at least so initially we’re
not subscribed to the Python subreddit
we are subscribed we could I think we
should get away this subreddit dot
unsubscribe
and now if we were to refresh this page
we need to subscribe I swear I’m
enjoying all my coffee this morning so
subscribed one more time I’m pretty sure
it said unsubscribe but the button
I don’t think changed color that’s
what’s tripping me up right now usually
it’s like red or green yeah okay now we
have an unsubscribe I have to go back
and look and see if it said anyway we
have subscribed did I do just a dot no I
actually ran it so I don’t know should
have subscribed but that’s weird cuz it
was like it never changed color released
I don’t think so I might be losing my
mind but anyway okay so that’s like a
quick kind of introduction to the Python
reddit API from here you could just read
documents or use dirt and attributes and
maybe figure a bunch of stuff out on
your own otherwise in the next tutorial
we’re going to be kind of going through
and really honing in on actually
navigating like the comments because
navigating the comments is significantly
more challenging than navigating the
gist submissions right the submissions
are always ordered very nicely whereas
comments are like entry form it’s kind
of a recursion problem and it can just
kind of be a little more challenging so
anyways that’s we’re gonna be doing a
next tutorial if you get questions
comments concerns whatever feel free to
even below otherwise I’ll see you in the
next door
Please follow and like us:

Be First to Comment

Leave a Reply