Skip to content

mattbdean/JRAW

Repository files navigation

The Java Reddit API Wrapper

travis-ci build status Latest release Kotlin 1.1.3-2 API coverage Codecov branch

This branch is a rewrite of the library in Kotlin. Please note that this branch is not even close to being production ready! There are still tons of missing features from JRAW v0.9.0.

// Assuming we have a 'script' reddit app
Credentials oauthCreds = Credentials.script(username, password, clientId, clientSecret);

// Create a unique User-Agent for our bot
UserAgent userAgent = new UserAgent("desktop", "my.cool.bot", "1.0.0", "myRedditUsername");

// Create our RedditClient
RedditClient reddit = OAuthHelper.script(oauthCreds, OkHttpAdapter(userAgent));

// Iterate through posts
Paginator<Submission> pics = reddit.subreddit("pics").posts()
    .sorting(Sorting.TOP)
    .timePeriod(TimePeriod.ALL)
    .limit(100)
    .build();
    
for (Submission s : pics.next())
    System.out.println(s);

// Get user preferences
Map<String, Object> prefs = reddit.me().prefs();
boolean showNsfw = (Boolean) prefs.get("over_18")

// Update preferences
Map<String, Object> newPrefs = new HashMap<>();
newPrefs.put("over_18", true);
reddit.me().patchPrefs(newPrefs);

// Get a user's info
Account user = reddit.user("_vargas_").about();
System.out.println(user.linkKarma);

// Submit a post
reddit.subreddit("pics").submit(SubmissionKind.LINK, "check out this cool website i found",
        "https://www.google.com", false);

Proposed APIs

This section is a draft and is very likely to change as development continues

// Users
UserReference davinci = reddit.user("Shitty_Watercolour")
davinci.message(...) // send a PM

// Submitting/commenting
val submission = reddit.me().submit("pics", ...)
val comment = submission.comment(...)
val subcomment = comment.comment(...)

Contributing

To get started you'll need to create two reddit OAuth2 apps, one script and one installed.

To have this done automatically for you, run this command:

$ ./gradlew :meta:credentials --no-daemon --console plain

lib/src/test/resources/credentials.json:

{
    "script": {
        "username": "...",
        "password": "...",
        "clientId": "...",
        "clientSecret": "..."
    },
    "app": {
        "clientId": "...",
        "redirectUrl": "..."
    }
}

Then you can go ahead and run the tests

$ ./gradlew test

Tests are written with Spek and assertions are done with Expekt.

Project structure

The :lib subproject contains the actual library source code, which also includes some generated sources (see lib/src/gen)

:meta contains some utilities for doing metadata-based work. It: