At this point, we've got basically all we could need for historical parsing, but what about for live applications? Maybe you want to keep a database up to date, or maybe you want to setup alerts for topics, or even you want to create a live bot that actually responds to comments. In these cases, you probably wouldn't want to be constantly pinging subreddits for changes, you'd rather have them streaming live, which we can also do with the Python Reddit API Wrapper.
The Python subreddit doesn't quite move fast enough, so let's instead use /r/news this time, and we'll begin a stream of the comments by doing:
subreddit = reddit.subreddit('news')
for comment in subreddit.stream.comments():
print(30*'_')
print()
print(comment.body)
Now this will give you some historical comments first, and then the stream begins.
...but we probably want context. Let's assume that we want the streamed comments, and their parents. We can do this by first grabbing the comment's parent. Many comments simply wont have parents at all, being totally top-level comments. For me, I don't want these, unless there are replies to them. Here's how we can do it:
subreddit = reddit.subreddit('news')
for comment in subreddit.stream.comments():
try:
print(30*'_')
print()
parent_id = str(comment.parent())
submission = reddit.comment(parent_id)
print('Parent:')
print(submission.body)
print('Reply')
print(comment.body)
except praw.exceptions.PRAWException as e:
pass
Full code:
import praw
reddit = praw.Reddit(client_id='clientid',
client_secret='secret', password='password',
user_agent='PrawTut', username='username')
subreddit = reddit.subreddit('news')
for comment in subreddit.stream.comments():
try:
print(30*'_')
print()
parent_id = str(comment.parent())
submission = reddit.comment(parent_id)
print('Parent:')
print(submission.body)
print('Reply')
print(comment.body)
except praw.exceptions.PRAWException as e:
pass
Again, we get a stream, but you'll probably notice that the history doesn't quite load as fast. Well, that's because we're making TWO more API calls PER comment. One API call to get the parent_id (if one even exists), then, if that parent does exist, then we grab the comment by that ID.
If there is no parent, this will toss us an error, which we catch and move along from.
If we get the parent ID (comment.parent()), then we call our PRAW reddit object, and search for a specific comment by ID, which is the parent ID we just grabbed.
Basically, any time you run a function, you can expect that to be a new API call. Anything built into the response from the Reddit API will be contained as an attribute. I do have to admit that I find it fairly silly that all comments do not come with parent_ids built in, being False if there are none and then the actual id if there is one, or something like that. I can understand why the parent's content isn't included...but the ID? C'mon!
Anyway, I think we're all set at this point in terms of examples of what you can do with the Reddit API via the Python Reddit API Wrapper. Just to drive some points home:
If you wanted to converse with the comments, in stream or historically, you just take that comment object and do a .reply()!
You can also stream/monitor for thread submissions, rather than for comments:
for submission in subreddit.stream.submissions():
Alright, that's all for now, I hope you found this useful! Be careful if you read the docs or do searches on the PRAW. I found a whole bunch of outdated information and had to figure out a few things by trial and error.
Check out the PRAW API documentation for more information on it!