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. 13
      quickjs/src/main/java/com/fongmi/quickjs/method/Global.java

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

@ -28,8 +28,6 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import dalvik.system.DexClassLoader; import dalvik.system.DexClassLoader;
@ -184,22 +182,17 @@ public class Spider extends com.github.catvod.crawler.Spider {
} }
private String getContent() { private String getContent() {
String global = "globalThis." + key;
String content = Module.get().load(api); String content = Module.get().load(api);
if (content.contains("__jsEvalReturn")) { if (content.contains("__jsEvalReturn")) {
return catvod(content); return content.concat(global).concat(" = __jsEvalReturn()");
} else if (content.contains("__JS_SPIDER__")) { } else if (content.contains("__JS_SPIDER__")) {
return content.replace("__JS_SPIDER__", "globalThis." + key); return content.replace("__JS_SPIDER__", global);
} else { } 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) { private JSObject convert(HashMap<String, String> map) {
JSObject obj = ctx.createNewJSObject(); JSObject obj = ctx.createNewJSObject();
if (map == null || map.isEmpty()) return obj; if (map == null || map.isEmpty()) return obj;

@ -26,15 +26,11 @@ public class Function implements Callable<Object[]> {
@Override @Override
public Object[] call() throws Exception { public Object[] call() throws Exception {
JSFunction func = jsObject.getJSFunction(name); JSFunction func = jsObject.getJSFunction(name);
boolean async = func.getJSFunction("toString").call().toString().startsWith("async"); JSObject object = (JSObject) func.call(args);
return new Object[]{async ? async(func) : func.call(args)}; JSFunction then = object.getJSFunction("then");
} if (then == null) return new Object[]{object};
private Object async(JSFunction func) {
JSObject promise = (JSObject) func.call(args);
JSFunction then = promise.getJSFunction("then");
then.call(jsCallFunction); then.call(jsCallFunction);
return result; return new Object[]{result};
} }
private final JSCallFunction jsCallFunction = new JSCallFunction() { 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.Parser;
import com.fongmi.quickjs.utils.Proxy; import com.fongmi.quickjs.utils.Proxy;
import com.github.catvod.net.OkHttp; import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Json;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.whl.quickjs.wrapper.JSArray; import com.whl.quickjs.wrapper.JSArray;
import com.whl.quickjs.wrapper.JSFunction; import com.whl.quickjs.wrapper.JSFunction;
@ -19,12 +20,14 @@ import com.whl.quickjs.wrapper.QuickJSContext;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Map;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import okhttp3.Call; import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.Headers; import okhttp3.Headers;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -161,7 +164,7 @@ public class Global {
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
if (!executor.isShutdown()) executor.submit(() -> {func.call();}); if (!executor.isShutdown()) executor.submit(() -> { func.call(); });
} }
}, delay); }, delay);
} }
@ -183,11 +186,19 @@ public class Global {
} }
private RequestBody getPostBody(Req req, String contentType) { 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.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)); if (req.getBody() != null && contentType != null) return RequestBody.create(gson.toJson(req.getBody()), MediaType.get(contentType));
return RequestBody.create("", null); 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) { private String getCharset(Headers headers) {
String contentType = headers.get("Content-Type"); String contentType = headers.get("Content-Type");
if (TextUtils.isEmpty(contentType)) return "UTF-8"; if (TextUtils.isEmpty(contentType)) return "UTF-8";

Loading…
Cancel
Save