|
|
|
|
@ -2,24 +2,16 @@ package com.fongmi.chaquo; |
|
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
|
|
|
|
|
|
import androidx.collection.ArrayMap; |
|
|
|
|
|
|
|
|
|
import com.chaquo.python.PyObject; |
|
|
|
|
import com.github.catvod.Proxy; |
|
|
|
|
import com.github.catvod.net.OkHttp; |
|
|
|
|
import com.github.catvod.utils.Json; |
|
|
|
|
import com.google.common.net.HttpHeaders; |
|
|
|
|
import com.github.catvod.utils.Util; |
|
|
|
|
import com.google.gson.Gson; |
|
|
|
|
import com.google.gson.JsonObject; |
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
import fi.iki.elonen.NanoHTTPD; |
|
|
|
|
import okhttp3.Headers; |
|
|
|
|
|
|
|
|
|
public class Spider extends com.github.catvod.crawler.Spider { |
|
|
|
|
|
|
|
|
|
private final PyObject app; |
|
|
|
|
@ -88,25 +80,26 @@ public class Spider extends com.github.catvod.crawler.Spider { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Object[] proxyLocal(Map<String, String> params) throws Exception { |
|
|
|
|
public Object[] proxyLocal(Map<String, String> params) { |
|
|
|
|
List<PyObject> list = app.callAttr("localProxy", obj, gson.toJson(params)).asList(); |
|
|
|
|
JsonObject action = Json.parse(list.get(2).toString()).getAsJsonObject(); |
|
|
|
|
Map<String, String> headers = Json.toMap(action.get("header")); |
|
|
|
|
String url = action.get("url").getAsString(); |
|
|
|
|
String content = list.get(3).toString(); |
|
|
|
|
String type = list.get(1).toString(); |
|
|
|
|
int code = list.get(0).toInt(); |
|
|
|
|
if (action.get("type").getAsString().equals("redirect")) { |
|
|
|
|
NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.lookup(code), NanoHTTPD.MIME_HTML, ""); |
|
|
|
|
for (Map.Entry<String, String> entry : headers.entrySet()) response.addHeader(entry.getKey(), entry.getValue()); |
|
|
|
|
response.addHeader(HttpHeaders.LOCATION, url); |
|
|
|
|
return new Object[]{response}; |
|
|
|
|
} else if (action.get("type").getAsString().equals("stream")) { |
|
|
|
|
ArrayMap<String, String> param = Json.toArrayMap(action.get("param")); |
|
|
|
|
return new Object[]{code, type, OkHttp.newCall(url, Headers.of(headers), param).execute().body().byteStream()}; |
|
|
|
|
Map<PyObject, PyObject> headers = list.size() > 3 ? list.get(3).asMap() : null; |
|
|
|
|
boolean base64 = list.size() > 4 && list.get(4).toInt() == 1; |
|
|
|
|
PyObject r2 = list.get(2); |
|
|
|
|
Object[] result = new Object[4]; |
|
|
|
|
result[0] = list.get(0).toInt(); |
|
|
|
|
result[1] = list.get(1).toString(); |
|
|
|
|
result[2] = r2 == null ? null : getStream(r2, base64); |
|
|
|
|
result[3] = headers; |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private ByteArrayInputStream getStream(PyObject o, boolean base64) { |
|
|
|
|
if (o.type().toString().contains("bytes")) { |
|
|
|
|
return new ByteArrayInputStream(o.toJava(byte[].class)); |
|
|
|
|
} else { |
|
|
|
|
if (content.isEmpty()) content = OkHttp.newCall(url, Headers.of(headers)).execute().body().string(); |
|
|
|
|
return new Object[]{code, type, new ByteArrayInputStream(replaceProxy(content).getBytes())}; |
|
|
|
|
String content = replaceProxy(o.toString()); |
|
|
|
|
if (base64 && content.contains("base64,")) content = content.split("base64,")[1]; |
|
|
|
|
return new ByteArrayInputStream(base64 ? Util.decode(content) : content.getBytes()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|