From 99e3566188cc5f6b464b0c116470b562805eee6a Mon Sep 17 00:00:00 2001 From: Alex Stewart Date: Thu, 26 Mar 2020 02:53:20 -0400 Subject: [PATCH] =E6 bug fixes E6 changed Json request and response Edited to mirror those changes --- src/network/NetworkE6.java | 104 ++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 35 deletions(-) diff --git a/src/network/NetworkE6.java b/src/network/NetworkE6.java index 1c65259..80db428 100644 --- a/src/network/NetworkE6.java +++ b/src/network/NetworkE6.java @@ -24,22 +24,54 @@ public class NetworkE6 // Internal JSON class and variables // //////////////////////////////////.//// private static final Gson jsonParser_ = new Gson(); - private static final String API_ROOT = "https://e621.net/post/index.json?"; + private static final String API_ROOT = "https://e621.net/posts.json?"; private static int maxSearchResults_ = 10; private static String[] blacklist = Ref.e621Blacklist; private class E6ResponseObject { - // public varaibles matching the case and the type we want for JSON. + // public variables matching the case and the type we want for JSON. // There are many more fields, but if we don't provide some it just // doesn't bother parsing them. - public String file_url; - public String id; - public String tags; - public String [] artist; - public String [] sources; - public String score; + public E6ResponseObjectPost [] posts; + } + + private class E6ResponseObjectPost + { + public String id; + public String [] sources; + public String[] pools; + public String rating; + public E6ResponseObjectFile file; + public E6ResponseObjectScore score; + public E6ResponseObjectTags tags; + public E6ResponseObjectRelationships relationships; + } + + private class E6ResponseObjectFile + { + public String url; + } + + private class E6ResponseObjectScore + { + public String total; + } + + private class E6ResponseObjectTags + { + public String[] general; + public String[] artist; + } + + private class E6ResponseObjectRelationships + { + public String has_children; + public String[] children; + } + + @@ -59,28 +91,26 @@ public class NetworkE6 // Configure and send request. Note: Random ordering added as first // tag by default. User-provided tags, therefore, will override it. // If order:score is provided, that will be honored over order:random. - String res = HTTPUtils.sendPOSTRequest(API_ROOT - , "tags=order:random%20" + input + "&limit=" + maxSearchResults_); - - + String res = HTTPUtils.sendGETRequest(API_ROOT + "tags=order:random%20" + input + "&limit=" + maxSearchResults_); if(res != null) { - // Use class evaluation on an array of the response imageObject to be able to hold multiple. - E6ResponseObject[] imageObj = jsonParser_.fromJson(res, E6ResponseObject[].class); + // Use class evaluation on an array of the response imageObject to be able to hold multiple. + System.out.println("Still"); + E6ResponseObject imageObj = jsonParser_.fromJson(res, E6ResponseObject.class); + System.out.println("Broken"); // For now, we really just wanna display images and their source. // Append them all separately to a response string w/ some flavor text. - if(imageObj.length < 1) + if(imageObj.posts.length < 1) { } else { - - for(int i = 0; i < imageObj.length; ++i) + for(int i = 0; i < imageObj.posts.length; ++i) { - String [] TagsList = imageObj[i].tags.split(" "); + String [] TagsList = imageObj.posts[i].tags.general; blacklisted = false; for(int j = 0; j < blacklist.length; j++) { @@ -99,10 +129,15 @@ public class NetworkE6 return null; } // We will always have a file URL. That's a given. - image.editImageURL(imageObj[i].file_url); - image.editPostURL("https://e621.net/post/show/" + imageObj[i].id); - if(imageObj[i].artist.length > 0) - image.editArtist(imageObj[i].artist[0]); + image.editImageURL(imageObj.posts[i].file.url); + image.editPostURL("https://e621.net/posts/" + imageObj.posts[i].id); + if(imageObj.posts[i].tags.artist.length > 0) + { + String artists = ""; + for(int j = 0; j < imageObj.posts[i].tags.artist.length; j++) + artists += imageObj.posts[i].tags.artist[j]; + image.editArtist(artists); + } else image.editArtist("Artist Not Found!"); @@ -110,13 +145,13 @@ public class NetworkE6 String sources = ""; - if(imageObj[i].sources != null) + if(imageObj.posts[i].sources != null) { - for(int k = 0; k < imageObj[i].sources.length; k ++) + for(int k = 0; k < imageObj.posts[i].sources.length; k ++) { - if(!imageObj[i].sources[k].endsWith(imageObj[i].file_url.substring(imageObj[i].file_url.lastIndexOf('.')))) + if(!imageObj.posts[i].sources[k].endsWith(imageObj.posts[i].file.url.substring(imageObj.posts[i].file.url.lastIndexOf('.')))) { - String source = imageObj[i].sources[k]; + String source = imageObj.posts[i].sources[k]; if(source.contains("//www.")) { source = source.substring(source.indexOf(".") + 1); @@ -127,31 +162,30 @@ public class NetworkE6 source = source.substring(source.indexOf("/") + 2); source = source.substring(0, source.indexOf('.')); } - sources += " [" + source + "](" + imageObj[i].sources[k] + ") "; + sources += " [" + source + "](" + imageObj.posts[i].sources[k] + ") "; } } - image.editDescriptionText("Score: " + imageObj[i].score + "\n**Source:**" + sources); + image.editDescriptionText("Score: " + imageObj.posts[i].score.total + "\n**Source:**" + sources); } else { - image.editDescriptionText("Score: " + imageObj[i].score); + image.editDescriptionText("Score: " + imageObj.posts[i].score.total); } - - if(imageObj[i].tags.split(" ").length > 15) + String descTags = ""; + if(imageObj.posts[i].tags.general.length > 15) { - String [] splitTags = imageObj[i].tags.split(" "); - String descTags = ""; for(int o = 0; o < 15; o++) { - descTags += splitTags[o] + ", "; + descTags += imageObj.posts[i].tags.general[o] + ", "; } image.editFooterText("[Tags] " + descTags + " etc."); } else { - image.editFooterText("[Tags] " + imageObj[i].tags.replace(" ", ", ")); + descTags = imageObj.posts[i].tags.general.toString(); + image.editFooterText("[Tags] " + descTags); } if(image.output().authorText != null)