summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/ken/command/nsfw/E621.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/net/tylermurphy/ken/command/nsfw/E621.java')
-rw-r--r--src/main/java/net/tylermurphy/ken/command/nsfw/E621.java28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/main/java/net/tylermurphy/ken/command/nsfw/E621.java b/src/main/java/net/tylermurphy/ken/command/nsfw/E621.java
index 837cca9..b62dec4 100644
--- a/src/main/java/net/tylermurphy/ken/command/nsfw/E621.java
+++ b/src/main/java/net/tylermurphy/ken/command/nsfw/E621.java
@@ -4,10 +4,14 @@ import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.tylermurphy.ken.Ken;
-import net.tylermurphy.ken.api.E621API;
-import net.tylermurphy.ken.command.Command;
-import net.tylermurphy.ken.command.Option;
+import net.tylermurphy.ken.api.HTTPMethod;
+import net.tylermurphy.ken.api.JsonRequest;
+import net.tylermurphy.ken.util.Command;
+import net.tylermurphy.ken.util.Option;
import net.tylermurphy.ken.command.Response;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
import java.util.List;
@@ -23,7 +27,7 @@ public class E621 {
if(!textChannel.isNSFW()){
return Response.error("This command can only be used in an NSFW channel");
}
- String url = E621API.request(args.get(0));
+ String url = request(args.get(0));
if(url == null){
return Response.error("Unable to find post with search: "+args.get(0));
} else {
@@ -33,4 +37,20 @@ public class E621 {
}
}
+ private String request(String query){
+ JSONObject json = (JSONObject) new JsonRequest()
+ .setURL("https://e621.net/posts.json?tags="+query)
+ .setType(HTTPMethod.GET)
+ .request();
+ try {
+ JSONArray results = json.getJSONArray("posts");
+ int choice = (int) (Math.random()*results.length()-1);
+ JSONObject post = (JSONObject) results.get(choice);
+ JSONObject file = post.getJSONObject("file");
+ return file.getString("url");
+ } catch (JSONException e) {
+ return null;
+ }
+ }
+
}