-
Notifications
You must be signed in to change notification settings - Fork 125
Cookbook
Here are some of the most basic actions commonly done by Reddit bots.
####Login
See the Quickstart page.
####Get my Information
// Assuming your client is authenticated and has requested the 'identity' scope:
LoggedInAccount me = redditClient.me();
####Get the Front Page
Iterating through data provided by the Reddit API is handled by the Paginator
class.
// Set up a Paginator for the front page
SubredditPaginator paginator = new SubredditPaginator(redditClient);
// Request the first page
Listing<Submission> firstPage = paginator.next();
// Iterate over the submissions
for (Submission submission : firstPage) {
// do something
}
You can view more information about Paginators in their wiki page
####Traversing a Comment Tree
JRAW provides a CommentNode
class specifically designed to make traversing a comment tree simple.
CommentNode rootNode = submission.getComments();
// By default, this Iterable will use pre-order traversal.
// By passing a TraversalMethod in the walkTree() method,
// you can change the way in which the comments will be iterated.
Iterable<CommentNode> iterable = rootNode.walkTree();
for (CommentNode node : iterable) {
// do something
}
####Checking a Method's OAuth2 scope
If a method is annotated with EndpointImplementation
, then that method will send a request to the Reddit API when called. To check the scope of an endpoint, one may find the annotation's value in the Endpoints
class and it will tell you what the required scope is.
For example, let's say we want to find out what the scope is for RedditClient.getSubmission()
is. All we have to do is see that that the value of the method's EndpointImplementation
is Endpoints.COMMENTS_ARTICLE
. From there, it says:
Represents the endpoint
GET /comments/{article}
included in the "read" scope.
So now we know that to use this method without error, we need to request the "read" scope.