|
|
|
|
@ -2,6 +2,9 @@ package com.hiker.drpy; |
|
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
|
|
|
|
|
|
import com.hiker.drpy.method.Console; |
|
|
|
|
import com.hiker.drpy.method.Global; |
|
|
|
|
import com.hiker.drpy.method.Local; |
|
|
|
|
import com.whl.quickjs.wrapper.JSArray; |
|
|
|
|
import com.whl.quickjs.wrapper.JSObject; |
|
|
|
|
import com.whl.quickjs.wrapper.QuickJSContext; |
|
|
|
|
@ -9,42 +12,44 @@ import com.whl.quickjs.wrapper.QuickJSContext; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.UUID; |
|
|
|
|
import java.util.concurrent.Callable; |
|
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
|
import java.util.concurrent.Executors; |
|
|
|
|
import java.util.concurrent.Future; |
|
|
|
|
|
|
|
|
|
public class Spider extends com.github.catvod.crawler.Spider { |
|
|
|
|
|
|
|
|
|
private final QuickJSContext ctx; |
|
|
|
|
private JSObject jsObject; |
|
|
|
|
private final String ext; |
|
|
|
|
private final ExecutorService executor; |
|
|
|
|
private final String key; |
|
|
|
|
private final String ext; |
|
|
|
|
private final String api; |
|
|
|
|
private QuickJSContext ctx; |
|
|
|
|
private JSObject jsObject; |
|
|
|
|
|
|
|
|
|
public Spider(QuickJSContext ctx, String api, String ext) { |
|
|
|
|
public Spider(String api, String ext) { |
|
|
|
|
this.executor = Executors.newSingleThreadExecutor(); |
|
|
|
|
this.key = "__" + UUID.randomUUID().toString().replace("-", "") + "__"; |
|
|
|
|
this.ctx = ctx; |
|
|
|
|
this.ext = ext; |
|
|
|
|
this.api = api; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String getContent(Context context) { |
|
|
|
|
return Module.get().load(context, api) |
|
|
|
|
.replace("export default{", "globalThis." + key + " ={") |
|
|
|
|
.replace("export default {", "globalThis." + key + " ={") |
|
|
|
|
.replace("__JS_SPIDER__", "globalThis." + key); |
|
|
|
|
private void submit(Runnable runnable) { |
|
|
|
|
executor.submit(runnable); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void init(Context context, String extend) throws Exception { |
|
|
|
|
super.init(context, extend); |
|
|
|
|
Worker.submit(() -> { |
|
|
|
|
ctx.evaluateModule(getContent(context), api); |
|
|
|
|
jsObject = (JSObject) ctx.getProperty(ctx.getGlobalObject(), key); |
|
|
|
|
jsObject.getJSFunction("init").call(ext); |
|
|
|
|
}); |
|
|
|
|
private <T> Future<T> submit(Callable<T> callable) { |
|
|
|
|
return executor.submit(callable); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String post(String func, Object... args) throws ExecutionException, InterruptedException { |
|
|
|
|
return Worker.submit(() -> (String) jsObject.getJSFunction(func).call(args)).get(); |
|
|
|
|
return submit(() -> (String) jsObject.getJSFunction(func).call(args)).get(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void init(Context context, String extend) throws Exception { |
|
|
|
|
super.init(context, extend); |
|
|
|
|
submit(() -> createJS(context)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@ -59,7 +64,7 @@ public class Spider extends com.github.catvod.crawler.Spider { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) throws Exception { |
|
|
|
|
JSObject obj = Worker.submit(() -> convert(extend)).get(); |
|
|
|
|
JSObject obj = submit(() -> convert(extend)).get(); |
|
|
|
|
return post("category", tid, pg, filter, obj); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -75,10 +80,35 @@ public class Spider extends com.github.catvod.crawler.Spider { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public String playerContent(String flag, String id, List<String> vipFlags) throws Exception { |
|
|
|
|
JSArray array = Worker.submit(() -> convert(vipFlags)).get(); |
|
|
|
|
JSArray array = submit(() -> convert(vipFlags)).get(); |
|
|
|
|
return post("play", flag, id, array); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void destroy() { |
|
|
|
|
submit(() -> ctx.destroy()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void createJS(Context context) { |
|
|
|
|
if (jsObject == null) setProperty(); |
|
|
|
|
ctx.evaluateModule(getContent(context), api); |
|
|
|
|
jsObject = (JSObject) ctx.getProperty(ctx.getGlobalObject(), key); |
|
|
|
|
jsObject.getJSFunction("init").call(ext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setProperty() { |
|
|
|
|
ctx = QuickJSContext.create(); |
|
|
|
|
ctx.getGlobalObject().setProperty("console", Console.class); |
|
|
|
|
ctx.getGlobalObject().setProperty("local", Local.class); |
|
|
|
|
Global.create(ctx).setProperty(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String getContent(Context context) { |
|
|
|
|
return Module.get().load(context, api) |
|
|
|
|
.replace("export default{", "globalThis." + key + " ={") |
|
|
|
|
.replace("export default {", "globalThis." + key + " ={") |
|
|
|
|
.replace("__JS_SPIDER__", "globalThis." + key); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private JSObject convert(HashMap<String, String> map) { |
|
|
|
|
JSObject obj = ctx.createNewJSObject(); |
|
|
|
|
if (map == null || map.isEmpty()) return obj; |
|
|
|
|
|