完善python缓存;

pull/144/head
jun 1 year ago
parent 5e8f175ab7
commit b033ec4539
  1. 20
      app/src/main/java/com/github/catvod/crawler/PyLoader.java
  2. 41
      app/src/main/java/com/github/tvbox/osc/api/ApiConfig.java
  3. 1
      app/src/main/java/com/github/tvbox/osc/ui/activity/HomeActivity.java

@ -1,29 +1,35 @@
package com.github.catvod.crawler;
import android.util.Log;
import com.github.tvbox.osc.base.App;
import com.undcover.freedom.pyramid.PythonLoader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class PyLoader {
private final PythonLoader pythonLoader = PythonLoader.getInstance().setApplication(App.getInstance());
private static ConcurrentHashMap<String, Spider> spiders = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, Spider> spiders = new ConcurrentHashMap<>();
private String lastConfig = null; // 记录上次的配置
public void clear() {
spiders.clear();
}
public void setConfig(String jsonStr) {
if (jsonStr != null && !jsonStr.equals(lastConfig)) {
Log.i("PyLoader","echo-setConfig 初始化json ");
pythonLoader.setConfig(jsonStr);
lastConfig = jsonStr;
}
}
public Spider getSpider(String key, String cls, String ext) {
if (spiders.containsKey(key))
// Log.i("PyLoader", "echo-getSpider spider缓存: " + key);
// LOG.i("");
if (spiders.containsKey(key)){
Log.i("PyLoader","echo-getSpider spider缓存: " + key);
return spiders.get(key);
}
try {
Spider sp = pythonLoader.getSpider(key, getPyUrl(cls,ext));
spiders.put(key, sp);
Log.i("PyLoader","echo-getSpider 加载spider: " + key);
return sp;
} catch (Throwable th) {
th.printStackTrace();
@ -31,12 +37,12 @@ public class PyLoader {
return new SpiderNull();
}
public Object[] proxyInvoke(String key,String url,Map<String,String> params) {
public Object[] proxyInvoke(Map<String,String> params,String key,String api,String ext) {
try {
String doStr = params.get("do");
assert doStr != null;
if (doStr.equals("ck") || doStr.equals("live"))return pythonLoader.proxyLocal("", "", params);
return (Object[]) pythonLoader.proxyLocal(key, url, params);
return (Object[]) pythonLoader.proxyLocal(key, getPyUrl(api,ext), params);
} catch (Throwable th) {
th.printStackTrace();
}

@ -37,8 +37,6 @@ import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.Response;
import com.orhanobut.hawk.Hawk;
import com.github.catvod.crawler.SpiderNull;
import org.json.JSONObject;
import java.io.BufferedReader;
@ -64,11 +62,11 @@ import java.util.regex.Pattern;
*/
public class ApiConfig {
private static ApiConfig instance;
private LinkedHashMap<String, SourceBean> sourceBeanList;
private final LinkedHashMap<String, SourceBean> sourceBeanList;
private SourceBean mHomeSource;
private ParseBean mDefaultParse;
private List<LiveChannelGroup> liveChannelGroupList;
private List<ParseBean> parseBeanList;
private final List<LiveChannelGroup> liveChannelGroupList;
private final List<ParseBean> parseBeanList;
private List<String> vipParseFlags;
private Map<String,String> myHosts;
private List<IJKCode> ijkCodes;
@ -88,7 +86,7 @@ public class ApiConfig {
private String defaultLiveObjString="{\"lives\":[{\"name\":\"txt_m3u\",\"type\":0,\"url\":\"txt_m3u_url\"}]}";
private ApiConfig() {
jarLoader.clear();
clearLoader();
sourceBeanList = new LinkedHashMap<>();
liveChannelGroupList = new ArrayList<>();
parseBeanList = new ArrayList<>();
@ -877,22 +875,19 @@ public class ApiConfig {
}
public Spider getCSP(SourceBean sourceBean) {
boolean js = sourceBean.getApi().endsWith(".js") || sourceBean.getApi().contains(".js?");
if (js) return jsLoader.getSpider(sourceBean.getKey(), sourceBean.getApi(), sourceBean.getExt(), sourceBean.getJar());
if (sourceBean.getKey().startsWith("py_") || sourceBean.getApi().endsWith(".py")) {
try {
return pyLoader.getSpider(sourceBean.getKey(), sourceBean.getApi(), sourceBean.getExt());
} catch (Exception e) {
return new SpiderNull();
}
if (sourceBean.getApi().endsWith(".js") || sourceBean.getApi().contains(".js?")){
return jsLoader.getSpider(sourceBean.getKey(), sourceBean.getApi(), sourceBean.getExt(), sourceBean.getJar());
}
return jarLoader.getSpider(sourceBean.getKey(), sourceBean.getApi(), sourceBean.getExt(), sourceBean.getJar());
else if (sourceBean.getKey().startsWith("py_")) {
return pyLoader.getSpider(sourceBean.getKey(), sourceBean.getApi(), sourceBean.getExt());
}
else return jarLoader.getSpider(sourceBean.getKey(), sourceBean.getApi(), sourceBean.getExt(), sourceBean.getJar());
}
public Object[] proxyLocal(Map<String,String> param) {
SourceBean sourceBean = ApiConfig.get().getHomeSourceBean();
if (sourceBean.getKey().startsWith("py_") || sourceBean.getApi().endsWith(".py")) {
return pyLoader.proxyInvoke(sourceBean.getKey(), getPyUrl(sourceBean), param);
if (sourceBean.getKey().startsWith("py_")) {
return pyLoader.proxyInvoke(param,sourceBean.getKey(),sourceBean.getApi(),sourceBean.getExt());
}else {
return jarLoader.proxyInvoke(param);
}
@ -1047,14 +1042,8 @@ public class ApiConfig {
parseBeanList.add(0, superPb);
}
private String getPyUrl(SourceBean sb)
{
String api = sb.getApi();
String ext = sb.getExt();
StringBuilder urlBuilder = new StringBuilder(api);
if (!ext.isEmpty()) {
urlBuilder.append(api.contains("?") ? "&" : "?").append("extend=").append(ext);
}
return urlBuilder.toString();
public void clearLoader(){
jarLoader.clear();
pyLoader.clear();
}
}

@ -239,6 +239,7 @@ public class HomeActivity extends BaseActivity {
File cspCacheDir = new File(cspCachePath + MD5.string2MD5(jarUrl)+".jar");
Toast.makeText(mContext, "jar缓存已清除", Toast.LENGTH_LONG).show();
if (!cspCacheDir.exists()){
refreshHome();
return;
}
new Thread(() -> {

Loading…
Cancel
Save