diff --git a/src/main/java/sh/adb/RandomRedditMemesAPI/CommunityHandler.java b/src/main/java/sh/adb/RandomRedditMemesAPI/CommunityHandler.java index 76d2da5..60d4f95 100644 --- a/src/main/java/sh/adb/RandomRedditMemesAPI/CommunityHandler.java +++ b/src/main/java/sh/adb/RandomRedditMemesAPI/CommunityHandler.java @@ -1,18 +1,34 @@ package sh.adb.RandomRedditMemesAPI; +import com.github.jreddit.parser.entity.Submission; +import com.github.jreddit.parser.exception.RedditParseException; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.List; class CommunityHandler implements HttpHandler { + RedditAPI api; + public CommunityHandler(RedditAPI redditClient) { + this.api = redditClient; + } + @Override public void handle(HttpExchange t) throws IOException { - InputStream is = t.getRequestBody(); + //InputStream is = t.getRequestBody(); //is.read(); - String response = "This is the response"; + List submissions = null; + try { + submissions = this.api.getSub("ProgrammerHumor"); + } catch (RedditParseException e) { + e.printStackTrace(); + } + assert submissions != null; + String response = submissions.get(0).getURL(); + System.out.println("response > "+response); t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); os.write(response.getBytes()); diff --git a/src/main/java/sh/adb/RandomRedditMemesAPI/Main.java b/src/main/java/sh/adb/RandomRedditMemesAPI/Main.java index a019972..23be638 100644 --- a/src/main/java/sh/adb/RandomRedditMemesAPI/Main.java +++ b/src/main/java/sh/adb/RandomRedditMemesAPI/Main.java @@ -13,17 +13,18 @@ import java.net.InetSocketAddress; public class Main { public static void main(String[] args) throws IOException, RedditOAuthException, ParseException { + //start RedditAPI + RedditAPI redditClient = new RedditAPI(); + System.out.println("RedditAPI started - the token is"); + System.out.println(redditClient.token.getAccessToken()); + //start httpServer HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("/", new FallbackHandler()); //server.createContext("/api/v1/", new StandardHandler()); - server.createContext("/api/v1/r/", new CommunityHandler()); + server.createContext("/api/v1/r/", new CommunityHandler(redditClient)); server.setExecutor(null); server.start(); System.out.println("server started"); - - RedditAPI client = new RedditAPI(); - System.out.println("the token is"); - System.out.println(client.token.getAccessToken()); } } diff --git a/src/main/java/sh/adb/RandomRedditMemesAPI/RedditAPI.java b/src/main/java/sh/adb/RandomRedditMemesAPI/RedditAPI.java index 4cc9d34..c81d135 100644 --- a/src/main/java/sh/adb/RandomRedditMemesAPI/RedditAPI.java +++ b/src/main/java/sh/adb/RandomRedditMemesAPI/RedditAPI.java @@ -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 getSub(String subreddit) throws RedditParseException { + SubmissionsListingParser parser = new SubmissionsListingParser(); + SubmissionsOfSubredditRequest request = (SubmissionsOfSubredditRequest) new SubmissionsOfSubredditRequest(subreddit, SubmissionSort.HOT).setLimit(100); + List submissions = parser.parse(client.get(token, request)); + return submissions; + } }