Skip to content

jlisam/JRAW

 
 

Repository files navigation

#Java Reddit API Wrapper travis-ci build status Coverage Status

JRAW is currently in an experimental stage, and therefore subject to application-breaking API changes that could occur at any time before v1.0.

JRAW requires language features that are only available in Java 8 and above. Please adjust your project accordingly. Voice your opinion on the matter at issue #12.

##Features

  • Mini-framework that wraps Square's OkHttp
  • (Optional) request management to prevent sending over 30 requests per minute
  • Full multireddit support
  • Full captcha support
  • Basic wiki access
  • Ability to iterate through posts on the front page or specific subreddits, with support for limits, sortings (hot, new, top, etc.) and time periods when using 'top' (day, week, all, etc.)
  • Get random posts and subreddits
  • Get trending subreddits
  • Get the submit text of a subreddit
  • Searching subreddits
  • Comment on/vote on/submit posts
  • Hide/unhide and save/unsave posts
  • Delete posts and comments
  • Reply to a post or comment
  • Adding/removing developers from Reddit apps
  • Iterate through new/popular subreddits
  • Get your subscribed subreddits
  • Iterate through your posts/comments/saved/hidden posts and comments, etc.
  • Lots of smaller features not mentioned here
  • Get a subreddit's stylesheet
  • Search for subreddits (by topic and by name)

##Design Philosiphy JRAW was built off the principle that as few classes as possible should be able to access the internet. Therefore, only RestClient and its subclasses (most notably RedditClient) can send HTTP requests. In order for other classes to send HTTP requests, they must acquire a RestClient, such as what Paginator does.

All models are instantiated with a Jackson JsonNode parsed from responses from the Reddit API.

##Getting Started ####Adding the Dependency

JRAW is hosted on Bintray's jCenter.

Gradle:

repositories {
    jcenter()
}

dependencies {
    compile(group: 'net.dean.jraw', name: 'JRAW', version: '0.4.0')
}

Maven:

Add jCenter to your repositories (see here and press "Set me up!" on the right hand side) and then add the repository:

<dependency>
    <groupId>net.dean.jraw</groupId>
    <artifactId>JRAW</artifactId>
    <version>0.4.0</version>
</dependency>

####Using the Library

Get yourself a RedditClient

RedditClient reddit = new RedditClient(MY_USER_AGENT);

Login (if necessary)

LoggedInAccount me = reddit.login(MY_USERNAME, MY_PASSWORD);

Start using the library! Some examples:

// Iterate through the front page
SubredditPaginator frontPage = new SubredditPaginator(reddit); // Second parameter could be a subreddit
while (frontPage.hasNext()) {
    Listing<Submission> submissions = frontPage.next();

    for (Submission submission : submissions) {
        System.out.println(submission.getTitle());
    }
}

// Post a link
URL url = // ...
me.submitContent(new LoggedInAccount.SubmissionBuilder(url, SUBREDDIT, TITLE));

// Post a self-post
String content = // ...
me.submitContent(new LoggedInAccount.SubmissionBuilder(content, SUBREDDIT, TITLE));

// Do stuff with a submission
Submission submission = reddit.getSubmission("28d6vv"); // http://redd.it/28d6vv
me.vote(submission, VoteDirection.UPVOTE);
me.setSaved(submission, true);
me.setHidden(submission, true);

For even more examples, see the unit tests.

Javadoc can be found here

##Models ####Hierarchy JsonModel is the superclass for all models in JRAW, including Thing. All core models defined by the Reddit wiki on GitHub extend Thing (except for Listing, which extends Thing's superclass, RedditObject

An overview of the models looks like this:

UML

####Data Retrieval

The workings behind getter methods of models are not the same as most Java objects. All models are instantiated with a Jackson JsonNode. Each getter method retrieves a value from the "data" node (see here for an example) by using a key specific to that method.

##API Endpoints See ENDPOINTS.md for full list of Reddit's API endpoints.

####Updating Endpoints The subproject endpoints uses annotations and the Reflections library to find methods that implement different API endpoints and then compile them into ENDPOINTS.md. Running ./gradlew endpoints:update will run the EndpointAnalyzer class, which will in turn generate the endpoints markdown file.

##Building

JRAW uses Gradle as its build system. If you're coming from a Maven background, you can read this StackOverflow question to help you get started.

./gradlew release will generate four Jar files in build/releases/: a normal jar with just the library, a "fat" jar with all of JRAW's runtime dependencies, a Javadoc jar, and a sources jar. See here for an example.

./gradlew test will run the unit tests

##Contributing

Before contributing, it is recommended that you have a decent knowledge of how the Reddit API works.

Some references:

Want to contribute? Follow these steps:

  1. Fork the repository
  2. Put your testing user's credentials in /src/test/java/resources/credentials.txt. The first line should be the username, and the second should be its password.
  3. Add your code. Implement an API endpoint, make the code prettier, or even just fix up some whitespace or documentation.
  4. Write some TestNG unit tests relevant to your changes
  5. Test your code by executing ./gradlew test
  6. Update ENDPOINTS.md and Endpoints.java by running ./gradlew endpoints:update
  7. Send the pull request

####Creating a user for unit testing

  1. Create a multireddit whose name is not "jraw_testing", containing at least one subreddit
  2. Have at least 10 link karma (otherwise you will have to use captchas)
  3. Submit at least one post (how about on /r/jraw_testing2?)