Support postType

pull/137/head
FongMi 2 years ago
parent 4aff0780ee
commit 09aee78027
  1. 6
      quickjs/src/main/java/com/fongmi/quickjs/bean/Req.java
  2. 15
      quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java
  3. 12
      quickjs/src/main/java/com/fongmi/quickjs/method/Function.java
  4. 11
      quickjs/src/main/java/com/fongmi/quickjs/method/Global.java

@ -17,6 +17,8 @@ public class Req {
private Integer redirect;
@SerializedName("timeout")
private Integer timeout;
@SerializedName("postType")
private String postType;
@SerializedName("method")
private String method;
@SerializedName("body")
@ -42,6 +44,10 @@ public class Req {
return timeout == null ? 10000 : timeout;
}
public String getPostType() {
return TextUtils.isEmpty(postType) ? "" : postType;
}
public String getMethod() {
return TextUtils.isEmpty(method) ? "get" : method;
}

@ -28,8 +28,6 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import dalvik.system.DexClassLoader;
@ -184,22 +182,17 @@ public class Spider extends com.github.catvod.crawler.Spider {
}
private String getContent() {
String global = "globalThis." + key;
String content = Module.get().load(api);
if (content.contains("__jsEvalReturn")) {
return catvod(content);
return content.concat(global).concat(" = __jsEvalReturn()");
} else if (content.contains("__JS_SPIDER__")) {
return content.replace("__JS_SPIDER__", "globalThis." + key);
return content.replace("__JS_SPIDER__", global);
} else {
return content.replaceAll("export default.*?[{]", "globalThis." + key + " = {");
return content.replaceAll("export default.*?[{]", global + " = {");
}
}
private String catvod(String content) {
String[] split = content.split("export\\s+function\\s+__jsEvalReturn.*?[{]");
Matcher matcher = Pattern.compile("\\s?return\\s?([\\s+\\S]*)\\s?\\}").matcher(split[1]);
return matcher.find() ? split[0].concat("globalThis." + key + " = ").concat(matcher.group(1)) : content;
}
private JSObject convert(HashMap<String, String> map) {
JSObject obj = ctx.createNewJSObject();
if (map == null || map.isEmpty()) return obj;

@ -26,15 +26,11 @@ public class Function implements Callable<Object[]> {
@Override
public Object[] call() throws Exception {
JSFunction func = jsObject.getJSFunction(name);
boolean async = func.getJSFunction("toString").call().toString().startsWith("async");
return new Object[]{async ? async(func) : func.call(args)};
}
private Object async(JSFunction func) {
JSObject promise = (JSObject) func.call(args);
JSFunction then = promise.getJSFunction("then");
JSObject object = (JSObject) func.call(args);
JSFunction then = object.getJSFunction("then");
if (then == null) return new Object[]{object};
then.call(jsCallFunction);
return result;
return new Object[]{result};
}
private final JSCallFunction jsCallFunction = new JSCallFunction() {

@ -9,6 +9,7 @@ import com.fongmi.quickjs.bean.Req;
import com.fongmi.quickjs.utils.Parser;
import com.fongmi.quickjs.utils.Proxy;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Json;
import com.google.gson.Gson;
import com.whl.quickjs.wrapper.JSArray;
import com.whl.quickjs.wrapper.JSFunction;
@ -19,12 +20,14 @@ import com.whl.quickjs.wrapper.QuickJSContext;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
@ -183,11 +186,19 @@ public class Global {
}
private RequestBody getPostBody(Req req, String contentType) {
if (req.getData() != null && req.getPostType().equals("form")) return getFormBody(req);
if (req.getData() != null) return RequestBody.create(gson.toJson(req.getData()), MediaType.get("application/json"));
if (req.getBody() != null && contentType != null) return RequestBody.create(gson.toJson(req.getBody()), MediaType.get(contentType));
return RequestBody.create("", null);
}
private RequestBody getFormBody(Req req) {
FormBody.Builder formBody = new FormBody.Builder();
Map<String, String> params = Json.toMap(req.getData());
for (String key : params.keySet()) formBody.add(key, params.get(key));
return formBody.build();
}
private String getCharset(Headers headers) {
String contentType = headers.get("Content-Type");
if (TextUtils.isEmpty(contentType)) return "UTF-8";

Loading…
Cancel
Save