Skip to content

Instantly share code, notes, and snippets.

@lordjbs
Created April 7, 2018 17:48
Show Gist options
  • Save lordjbs/58ce72c14e380b5ca745a72db59e8b42 to your computer and use it in GitHub Desktop.
Save lordjbs/58ce72c14e380b5ca745a72db59e8b42 to your computer and use it in GitHub Desktop.
the script :o
package io.github.lordjbs.tests;
import asjt.network.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
/**
* @author lordjbs
*/
public class RedditGetPostWithFlair {
/*
* !!IMPORTANT!!
* I have no clue what the devs are using for MemeExchange so i just made this thing. Basic.
*/
public static JSONObject getOne(String subreddit) throws IOException, JSONException {
//Standard HTTP Request. I used my own lib, which is basicly wrapping OkHTTP
String response = HTTP.get("https://reddit.com/r/" + subreddit + "/random.json").body().string();
// Reddits outputs.. meh
JSONObject reddit_response = new JSONObject(response.substring(1, response.length()-1));
//Parse
JSONObject data = reddit_response.getJSONObject("data").getJSONArray("children").getJSONObject(0).getJSONObject("data");
String flair_name = data.getString("link_flair_text");
String imageurl = data.getString("url");
return new JSONObject().put("flair", flair_name).put("image", imageurl);
}
public static void main(String args[]) throws Exception {
JSONObject response = getOne("LinusFaces");
System.out.println("Flair: " + response.getString("flair") + "\nImage: " + response.getString("image"));
/*
* This Outputs:
Flair: Other
Image: https://i.redd.it/r8bwjr3xy5m01.png
so you'll only need to do some if's and stuff.
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment