Reputation: 27
import requests
import base64
def search(api_token, image):
endpoint="https://trace.moe/api/search"
params={"token":api_token}
data={"image":image}
r = requests.post(endpoint, params=params, data=data)
if r.status_code==403:
raise Exception("API token invalid")
elif r.status_code==401:
raise Exception("API token missing")
elif r.status_code==413:
raise Exception("The image is too large, please reduce the image size to below 1MB")
elif r.status_code==429:
raise Exception("You have exceeded your quota. Please wait and try again soon.")
elif r.status_code==200:
return(r.json())
with open("test.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
search = search("token", encoded_string)
I've tried to use Apache HttpClient with MultipartEntity, but it didn't help me.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://trace.moe/api/search");
File file = getImgFile();
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("image", cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
Any my try give "No data received" or empty "docs". What i can do?
Upvotes: 2
Views: 407
Reputation: 3154
Okay after a few minutes of keyboard bashing I have made the code that works with it, But it does not handle the request code and does not handle the actual JSON, You will need to implement it, It prints out the response for now. This is a basic start for what you wanna do. here is the code
public static void main(String[] args){
File file = new File("C:\\Users\\Rab\\Desktop\\lol\\anime.jpg");
try {
byte[] fileContent = Files.readAllBytes(file.toPath());
String imageData = Base64.getEncoder().encodeToString(fileContent);
search("", imageData);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void search(String token, String image){
try {
String urlParameters = String.format("token=%s&image=%s", token, image);
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
URL url = new URL("https://trace.moe/api/search");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.addRequestProperty("charset", "utf-8");
conn.addRequestProperty("User-Agent", "Mozilla/4.76");
conn.addRequestProperty("Content-Length", Integer.toString(postDataLength));
conn.setUseCaches(false);
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.write(postData);
}
InputStream inputStream = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String wtf = br.readLine();
System.out.println(wtf);
//Handle your data `wtf` is the response of the server
} catch (Exception e){
e.printStackTrace();
}
}
and the imports
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Base64;
Upvotes: 1