You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.7 KiB
Java
61 lines
2.7 KiB
Java
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;
|
|
import com.github.jreddit.oauth.RedditToken;
|
|
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 {
|
|
// Information about the app
|
|
String userAgent = "jReddit: Reddit API Wrapper for Java";
|
|
String clientID = "PfnhLt3VahLrbg";
|
|
String redirectURI = "https://github.com/snkas/jReddit";
|
|
|
|
// Reddit application
|
|
RedditApp redditApp = new RedditInstalledApp(clientID, redirectURI);
|
|
|
|
// 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());
|
|
System.out.println("Token Type: " + token.getTokenType());
|
|
System.out.println("Refreshable: " + token.isRefreshable());
|
|
System.out.println("Expired: " + token.isExpired());
|
|
System.out.println("Expiration: " + token.getExpiration());
|
|
System.out.println("Will expire in 61 minutes: " + token.willExpireIn((long) 3660));
|
|
System.out.println("Will expire in 59 minutes: " + token.willExpireIn((long) 3540));
|
|
}
|
|
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;
|
|
}
|
|
}
|