-
Notifications
You must be signed in to change notification settings - Fork 127
Paginators
thatJavaNerd edited this page Oct 31, 2014
·
8 revisions
Iteration through any sort of data returned by the Reddit API is done through the Paginator
class. Paginator
is used to iterate a listing
, a data type defined by the Reddit API (see here for more). Its respective class can be found here.
Perhaps the most simplistic way to demonstrate a Paginator
is by using SubredditPaginator
. You can use this class to iterate the submissions on the front page or a subreddit.
RedditClient reddit = new RedditClient("MY-USER-AGENT");
SubredditPaginator frontPage = new SubredditPaginator(reddit);
// Adjust the request parameters
frontPage.setLimit(50); // Default is 25 (Paginator.DEFAULT_LIMIT)
frontPage.setTimePeriod(TimePeriod.MONTH); // Default is DAY (Paginator.DEFAULT_TIME_PERIOD)
frontPage.setSorting(Sorting.TOP); // Default is HOT (Paginator.DEFAULT_SORTING)
// This Paginator is now set up to retrieve the highest-scoring links submitted within the past
// month, 50 at a time
// Since Paginator implements Iterator, you can use it just how you would expect to, using next() and hasNext()
Listing<Submission> submissions = frontPage.next();
for (Submission s : submissions) {
// Print some basic stats about the posts
System.out.printf("[/r/%s - %s karma] %s\n", s.getSubredditName(), s.getScore(), s.getTitle());
}
The output for this example might look something like this:
[/r/pics - 8510 karma] Got divorced, lost my job, so me and my buddy got on our motorcycles and rode North to the Alaskan Arctic until the road ran out.
[/r/funny - 6560 karma] Man trying to return a dog's toy gets tricked into playing fetch
[/r/videos - 6557 karma] Man races train overground from one station to the next.
(...)
Several Paginator
subclasses (found in the net.dean.jraw.pagination
package) are provided for you for data that is commonly accessed when writing bots and other applications.
Class | Use | Example Data |
---|---|---|
AllSubredditsPaginator |
View the newest or most popular subreddits | Here |
CompoundSubredditPaginator |
View submissions from multiple subreddits | Here |
InboxPaginator |
Read a user's inbox | Here |
LiveThreadPaginator |
View a live thread's updates (see here for more). | Here |
MultiHubPaginator |
Designed specifically to iterate /r/multihub and parse the owners and names of multireddits found there | Here |
MultiRedditPaginator |
Like SubredditPaginator , but instead iterates over a multireddit |
Here |
SearchPaginator |
Allows access to Reddit's search functionality | Here |
SubredditPaginator |
Iterates over the submissions posted in a subreddit | Here |
SpecificPaginator |
Allows you to iterate over a given set of submission IDs, if for some reason you would like to do that. | Here |
UserContributionPaginator |
Iterates over a user's submissions, comments, gilded comments/submissions, etc. | Here |
UserRecordPaginator |
View listings of users relevant to a specific subreddit (banned, moderators, approved contributors, etc.) | Here |
UserSubredditsPaginator |
View the subreddits a user is an approved contributor of, moderates, or is subscribed to | Here |