(WIP) flaresolverr client
This commit is contained in:
parent
33f99488e2
commit
f397ad471d
|
@ -0,0 +1,67 @@
|
|||
package ctbrec.io;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import okhttp3.*;
|
||||
import okhttp3.OkHttpClient.Builder;
|
||||
import ctbrec.GlobalThreadPool;
|
||||
|
||||
|
||||
public class FlaresolverrClient {
|
||||
public String api_url = "http://localhost:8191/v1";
|
||||
protected OkHttpClient client = new OkHttpClient();
|
||||
protected JsonMapper mapper = new JsonMapper();
|
||||
|
||||
public FlaresolverrClient() {
|
||||
}
|
||||
|
||||
public FlaresolverrClient(String apiUrl) {
|
||||
api_url = apiUrl;
|
||||
}
|
||||
|
||||
public JsonNode createSession(String name) throws Exception {
|
||||
var body = mapper.createObjectNode()
|
||||
.put("cmd", "sessions.create")
|
||||
.put("session", name);
|
||||
|
||||
return makeApiCall(body);
|
||||
}
|
||||
|
||||
public JsonNode destroySession(String name) throws Exception {
|
||||
var body = mapper.createObjectNode()
|
||||
.put("cmd", "sessions.destroy")
|
||||
.put("session", name);
|
||||
|
||||
return makeApiCall(body);
|
||||
}
|
||||
|
||||
public JsonNode getCookies(String url) throws Exception {
|
||||
return getCookies(url, 60000);
|
||||
}
|
||||
|
||||
public JsonNode getCookies(String url, int timeout_ms) throws Exception {
|
||||
var body = mapper.createObjectNode()
|
||||
.put("cmd", "request.get")
|
||||
.put("url", url)
|
||||
.put("maxTimeout", timeout_ms)
|
||||
.put("returnOnlyCookies", true);
|
||||
|
||||
return makeApiCall(body);
|
||||
}
|
||||
|
||||
protected JsonNode makeApiCall(ObjectNode body) throws Exception {
|
||||
var requestBody = RequestBody.create(mapper.writeValueAsString(body), MediaType.get("application/json"));
|
||||
var request = new Request.Builder()
|
||||
.url(api_url)
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
var response = client.newCall(request).execute();
|
||||
return mapper.readTree(response.body().string());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue