Iterative

This commit is contained in:
Matthew Cech
2020-02-09 20:56:10 -08:00
parent c8061770ed
commit de2f3f4515
8 changed files with 206 additions and 24 deletions
+104
View File
@@ -0,0 +1,104 @@
package network;
import com.google.gson.Gson;
import dataStructures.GenericImage;
import offline.Ref;
import utils.*;
/**
* This is the e6 request class, designed for form and parse requests that
* use the e6 API, and is entirely static.
*
* If you ever find this class randomly not working,
* it may be a good idea to make sure the user agent string is set in
* HTTPUtils to something other than a browser emulating string, or the default
* java one.
*
* @author Wisp
* Edited by Rin
*/
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 int maxSearchResults_ = 10;
private static String[] blacklist = Ref.e621Blacklist;
private class E6ResponseObject
{
// public varaibles 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;
}
////////////////////
// Static methods //
////////////////////
// Requests a specific image, then returns a few.
public GenericImage getE6(String input)
{
GenericImage image = new GenericImage("","","");
boolean blacklisted;
// Clean up request and replace problematic characters for the query string.
input = input.trim();
input = input.replace("+", "%2B");
input = input.replace(" ", "%20");
// 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_);
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);
// 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)
{
}
else
{
for(int i = 0; i < imageObj.length; ++i)
{
blacklisted = false;
for(int j = 0; j < blacklist.length; j++)
{
if(imageObj[i].tags.contains(blacklist[j]))
{
blacklisted = true;
}
}
if(blacklisted)
{
continue;
}
// 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]);
else
image.editArtist("Artist Not Found!");
}
}
}
return image;
}
}