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