Skip to content

Commit c7d164e

Browse files
author
Taois
committed
example:
1 parent b2194eb commit c7d164e

File tree

2 files changed

+478
-0
lines changed

2 files changed

+478
-0
lines changed

docs/other/NodeSpider.java

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
package com.quickjs.android.example.js;
2+
3+
import android.content.Context;
4+
5+
import com.caoccao.javet.interop.NodeRuntime;
6+
import com.caoccao.javet.values.V8Value;
7+
import com.caoccao.javet.values.reference.IV8ValuePromise;
8+
import com.caoccao.javet.values.reference.V8ValueArray;
9+
import com.caoccao.javet.values.reference.V8ValueFunction;
10+
import com.caoccao.javet.values.reference.V8ValueObject;
11+
import com.caoccao.javet.values.reference.V8ValuePromise;
12+
import com.quickjs.android.example.LOG;
13+
import com.quickjs.android.example.Spider;
14+
import com.quickjs.android.example.UpdateUaListener;
15+
16+
import org.json.JSONObject;
17+
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.concurrent.ConcurrentHashMap;
21+
22+
public class NodeSpider extends Spider {
23+
private final String modulePath;
24+
private final NodeRuntime nodeRuntime;
25+
private final UpdateUaListener listener;
26+
ConcurrentHashMap<String, String> queryMap;
27+
28+
public NodeSpider(String path, NodeRuntime ctx, UpdateUaListener listener) {
29+
this.modulePath = path;
30+
this.nodeRuntime = ctx;
31+
this.listener = listener;
32+
this.queryMap = new ConcurrentHashMap<>();
33+
}
34+
35+
private void func(Object... args) {
36+
try {
37+
V8ValueFunction v8ValueFunction = nodeRuntime.getGlobalObject().get("getEngine");
38+
Object result = v8ValueFunction.call(null, args);
39+
if (result instanceof V8ValueObject) {
40+
if (result instanceof V8ValuePromise) {
41+
V8ValuePromise v8ValuePromise = ((V8ValuePromise) result);
42+
v8ValuePromise.register(new IV8ValuePromise.IListener() {
43+
@Override
44+
public void onCatch(V8Value v8Value) {
45+
//listener.saved(((V8ValueObject) v8Value).toJsonString());
46+
}
47+
48+
@Override
49+
public void onFulfilled(V8Value v8Value) {
50+
listener.saved(((V8ValueObject) v8Value).toJsonString());
51+
}
52+
53+
@Override
54+
public void onRejected(V8Value v8Value) {
55+
//listener.saved(((V8ValueObject) v8Value).toJsonString());
56+
}
57+
});
58+
} else {
59+
listener.saved(((V8ValueObject) result).toJsonString());
60+
}
61+
} else {
62+
listener.saved(result.toString());
63+
}
64+
} catch (Throwable throwable) {
65+
LOG.e(throwable);
66+
}
67+
}
68+
69+
@Override
70+
public void init(Context context) throws Exception {
71+
super.init(context);
72+
}
73+
74+
@Override
75+
public void init(Context context, String extend) throws Exception {
76+
super.init(context, extend);
77+
try {
78+
String go = "ds";
79+
boolean refresh = false;
80+
if (extend.startsWith("{")) {
81+
JSONObject json = new JSONObject(extend);
82+
extend = json.optString("extend", "");
83+
go = json.optString("go", "ds");
84+
refresh = json.optBoolean("refresh", false);
85+
}
86+
queryMap.put("go", go);
87+
if(refresh) queryMap.put("refresh", "");
88+
queryMap.put("extend", extend);
89+
} catch (Throwable throwable) {
90+
LOG.e(throwable);
91+
}
92+
}
93+
94+
@Override
95+
public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) {
96+
try {
97+
V8ValueObject ext = nodeRuntime.createV8ValueObject();
98+
if (extend != null) {
99+
for (String s : extend.keySet()) {
100+
ext.set(s, extend.get(s));
101+
}
102+
}
103+
V8ValueObject query = nodeRuntime.createV8ValueObject();
104+
if (queryMap != null) {
105+
for (String s : queryMap.keySet()) {
106+
query.set(s, queryMap.get(s));
107+
}
108+
}
109+
query.set("ac", "category");
110+
query.set("t", tid);
111+
query.set("pg", pg);
112+
query.set("ext", ext);
113+
func(modulePath, query);
114+
return "";
115+
} catch (Throwable throwable) {
116+
LOG.e(throwable);
117+
return "";
118+
}
119+
}
120+
121+
@Override
122+
public String detailContent(List<String> list) {
123+
try {
124+
V8ValueArray array = nodeRuntime.createV8ValueArray();
125+
if (list != null) {
126+
for (int i = 0; i < list.size(); i++) {
127+
array.push(list.get(i));
128+
}
129+
}
130+
V8ValueObject query = nodeRuntime.createV8ValueObject();
131+
if (queryMap != null) {
132+
for (String s : queryMap.keySet()) {
133+
query.set(s, queryMap.get(s));
134+
}
135+
}
136+
query.set("ac", "detail");
137+
query.set("ids", array);
138+
func(modulePath, query);
139+
return "";
140+
} catch (Throwable throwable) {
141+
LOG.e(throwable);
142+
return "";
143+
}
144+
}
145+
146+
@Override
147+
public String homeContent(boolean filter) {
148+
try {
149+
V8ValueObject query = nodeRuntime.createV8ValueObject();
150+
if (queryMap != null) {
151+
for (String s : queryMap.keySet()) {
152+
query.set(s, queryMap.get(s));
153+
}
154+
}
155+
query.set("filter", filter);
156+
func(modulePath, query);
157+
} catch (Throwable throwable) {
158+
LOG.e(throwable);
159+
return "";
160+
}
161+
return "";
162+
}
163+
164+
@Override
165+
public String homeVideoContent() {
166+
try {
167+
V8ValueObject query = nodeRuntime.createV8ValueObject();
168+
if (queryMap != null) {
169+
for (String s : queryMap.keySet()) {
170+
query.set(s, queryMap.get(s));
171+
}
172+
}
173+
func(modulePath, query);
174+
} catch (Throwable throwable) {
175+
LOG.e(throwable);
176+
return "";
177+
}
178+
return "";
179+
}
180+
181+
@Override
182+
public String playerContent(String flag, String id, List<String> vipFlags) {
183+
try {
184+
V8ValueArray array = nodeRuntime.createV8ValueArray();
185+
if (vipFlags != null) {
186+
for (int i = 0; i < vipFlags.size(); i++) {
187+
array.push(vipFlags.get(i));
188+
}
189+
}
190+
V8ValueObject query = nodeRuntime.createV8ValueObject();
191+
if (queryMap != null) {
192+
for (String s : queryMap.keySet()) {
193+
query.set(s, queryMap.get(s));
194+
}
195+
}
196+
query.set("play", id);
197+
query.set("flag", flag);
198+
func(modulePath, query);
199+
return "";
200+
} catch (Throwable throwable) {
201+
LOG.e(throwable);
202+
return "";
203+
}
204+
}
205+
206+
@Override
207+
public String searchContent(String key, boolean quick) {
208+
try {
209+
V8ValueObject query = nodeRuntime.createV8ValueObject();
210+
if (queryMap != null) {
211+
for (String s : queryMap.keySet()) {
212+
query.set(s, queryMap.get(s));
213+
}
214+
}
215+
query.set("wd", key);
216+
query.set("quick", quick);
217+
func(modulePath, query);
218+
} catch (Throwable throwable) {
219+
LOG.e(throwable);
220+
return "";
221+
}
222+
return "";
223+
}
224+
225+
@Override
226+
public String searchContent(String key, boolean quick, String pg) {
227+
try {
228+
V8ValueObject query = nodeRuntime.createV8ValueObject();
229+
if (queryMap != null) {
230+
for (String s : queryMap.keySet()) {
231+
query.set(s, queryMap.get(s));
232+
}
233+
}
234+
query.set("wd", key);
235+
query.set("quick", quick);
236+
query.set("pg", pg);
237+
func(modulePath, query);
238+
} catch (Throwable throwable) {
239+
LOG.e(throwable);
240+
return "";
241+
}
242+
return "";
243+
}
244+
}

0 commit comments

Comments
 (0)