|
|
|
@ -1,5 +1,13 @@
|
|
|
|
|
package sh.adb.RandomRedditMemesAPI;
|
|
|
|
|
|
|
|
|
|
import com.github.jreddit.oauth.client.RedditClient;
|
|
|
|
|
import com.github.jreddit.oauth.client.RedditHttpClient;
|
|
|
|
|
import com.github.jreddit.parser.entity.Submission;
|
|
|
|
|
import com.github.jreddit.parser.exception.RedditParseException;
|
|
|
|
|
import com.github.jreddit.parser.listing.SubmissionsListingParser;
|
|
|
|
|
import com.github.jreddit.request.retrieval.param.SubmissionSort;
|
|
|
|
|
import com.github.jreddit.request.retrieval.submissions.SubmissionsOfSubredditRequest;
|
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
|
|
|
|
|
|
import com.github.jreddit.oauth.RedditOAuthAgent;
|
|
|
|
@ -8,8 +16,11 @@ import com.github.jreddit.oauth.app.RedditApp;
|
|
|
|
|
import com.github.jreddit.oauth.app.RedditInstalledApp;
|
|
|
|
|
import com.github.jreddit.oauth.exception.RedditOAuthException;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class RedditAPI {
|
|
|
|
|
private final RedditOAuthAgent agent;
|
|
|
|
|
private final RedditClient client;
|
|
|
|
|
protected RedditToken token;
|
|
|
|
|
|
|
|
|
|
public RedditAPI() throws RedditOAuthException, ParseException {
|
|
|
|
@ -24,6 +35,9 @@ public class RedditAPI {
|
|
|
|
|
// Create OAuth agent
|
|
|
|
|
agent = new RedditOAuthAgent(userAgent, redditApp);
|
|
|
|
|
|
|
|
|
|
// Create request executor
|
|
|
|
|
client = new RedditHttpClient(userAgent, HttpClientBuilder.create().build());
|
|
|
|
|
|
|
|
|
|
// Create token (will be valid for 1 hour)
|
|
|
|
|
token = agent.tokenAppOnly(false);
|
|
|
|
|
System.out.println("Access Token: " + token.getAccessToken());
|
|
|
|
@ -37,4 +51,10 @@ public class RedditAPI {
|
|
|
|
|
public void newToken() throws RedditOAuthException {
|
|
|
|
|
this.token = this.agent.tokenAppOnly(false);
|
|
|
|
|
}
|
|
|
|
|
public List<Submission> getSub(String subreddit) throws RedditParseException {
|
|
|
|
|
SubmissionsListingParser parser = new SubmissionsListingParser();
|
|
|
|
|
SubmissionsOfSubredditRequest request = (SubmissionsOfSubredditRequest) new SubmissionsOfSubredditRequest(subreddit, SubmissionSort.HOT).setLimit(100);
|
|
|
|
|
List<Submission> submissions = parser.parse(client.get(token, request));
|
|
|
|
|
return submissions;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|