Cookbook
This page shows some examples of what the library can do
The basics
View the front page
// frontPage() returns a Paginator.Builder
DefaultPaginator<Submission> frontPage = redditClient.frontPage()
.sorting(SubredditSort.TOP)
.timePeriod(TimePeriod.DAY)
.limit(30)
.build();
Listing<Submission> submissions = frontPage.next();
for (Submission s : submissions) {
System.out.println(s.getTitle());
}
Find imgur links from several subreddits
DefaultPaginator<Submission> earthPorn = redditClient.subreddits("EarthPorn", "spaceporn").posts().build();
List<String> images = new ArrayList<String>();
for (Submission s : earthPorn.next()) {
if (!s.isSelfPost() && s.getUrl().contains("i.imgur.com")) {
images.add(s.getUrl());
}
}
// do something with `images`
Get user subscriptions
// Make sure we have a logged-in user or this call will fail!
BarebonesPaginator<Subreddit> paginator = redditClient.me().subreddits("subscriber")
// Request as many items as possible
.limit(Paginator.RECOMMENDED_MAX_LIMIT)
.build();
List<Subreddit> subscribed = new ArrayList<Subreddit>();
// Paginator implements Iterable, so we can use the enhanced for loop to iterate the Paginator until reddit
// can't give us anything else. Don't do this for posts on a subreddit or the front page!
for (Listing<Subreddit> page : paginator) {
subscribed.addAll(page);
}
// Do something with `subscribed`
Account
See also UserReference, SelfUserReference, OtherUserReference
Get basic information
Account me = redditClient.me().about();
Account someoneElse = redditClient.user("Shitty_Watercolour").about();
Get trophies
List<Trophy> mine = redditClient.me().trophies();
List<Trophy> someoneElses = redditClient.user("Shitty_Watercolour").trophies();
Flair
See also FlairReference, SubmissionFlairReference, UserFlairReference, SelfUserFlairReference, and OtherUserFlairReference
List<Flair> userFlairOptions = redditClient.subreddit("RocketLeague").userFlairOptions();
redditClient.subreddit("RocketLeague")
.selfUserFlair()
.updateToTemplate(userFlairOptions.get(0).getId(), "");
Links and comments
Comment tree traversal
// Request the comments of some submission. comments() takes more parameters where you
// can customize things like comment sorting and only returning a specific comment
// thread.
RootCommentNode root = redditClient.submission("92dd8").comments();
// walkTree() returns a Kotlin Sequence. Since we're using Java, we're going to have to
// turn it into an Iterator to get any use out of it.
Iterator<CommentNode<PublicContribution<?>>> it = root.walkTree().iterator();
while (it.hasNext()) {
// A PublicContribution is either a Submission or a Comment.
PublicContribution<?> thing = it.next().getSubject();
// Do something with each Submission/Comment
System.out.println(thing.getBody());
}
Live threads
Live threads are covered here.
Private messages
Inbox functionality requires a logged-in user
Iterate inbox
BarebonesPaginator<Message> unread = redditClient.me().inbox().iterate("unread")
.build();
Listing<Message> firstPage = unread.next();
Mark messages as read/unread
// Get the first unread message in the inbox
Listing<Message> unread = redditClient.me().inbox().iterate("unread")
.limit(1)
.build().next();
if (!unread.isEmpty()) {
Message first = unread.get(0);
// The first message
redditClient.me().inbox().markRead(true, first.getFullName());
}
Send a private message
InboxReference inbox = redditClient.me().inbox();
inbox.compose("thatJavaNerd", "receiver username", "body");
Send a private message as a subreddit moderator
// Make sure to exclude the "/r/" prefix
redditClient.me().inbox().compose("some_subreddit_i_moderate", "receiver username",
"subject", "body");
Moderation/modmail
TODO
Multireddits
Create/update a multireddit
MultiredditPatch multiSpec = new MultiredditPatch.Builder()
.description("Here's a cool multireddit!")
.subreddits("redditdev", "kotlin", "java")
.visibility("private")
.build();
Multireddit multi = redditClient.me().createMulti("my_multireddit", multiSpec);
// Turn the model class into a Reference, which we can use to interact with the API
MultiredditReference ref = multi.toReference(redditClient);
// Add or remove subreddits one at a time
ref.addSubreddit("programming");
ref.removeSubreddit("java");
// Or we can change things in bulk
multi = ref.update(new MultiredditPatch.Builder()
.subreddits("redditdev")
.build());
List multireddits
List<Multireddit> mine = redditClient.me().listMultis();
List<Multireddit> someoneElses = redditClient.user("reddit").listMultis();
Iterate a specific multireddit
// reddit staff members have subreddits for their pets, the multireddit at
// "/user/reddit/m/redditpets" contains them all
MultiredditReference ref = redditClient.user("reddit").multi("redditpets");
// Get the first page
Listing<Submission> posts = ref.posts().build().next();
Subreddits
TODO
Users
TODO
Wiki
TODO