|
|
|
|
@ -7,6 +7,7 @@ import android.util.Base64; |
|
|
|
|
|
|
|
|
|
import com.github.catvod.crawler.JarLoader; |
|
|
|
|
import com.github.catvod.crawler.Spider; |
|
|
|
|
import com.github.tvbox.osc.base.App; |
|
|
|
|
import com.github.tvbox.osc.bean.IJKCode; |
|
|
|
|
import com.github.tvbox.osc.bean.LiveChannel; |
|
|
|
|
import com.github.tvbox.osc.bean.ParseBean; |
|
|
|
|
@ -17,6 +18,7 @@ import com.github.tvbox.osc.server.ControlManager; |
|
|
|
|
import com.github.tvbox.osc.util.AdBlocker; |
|
|
|
|
import com.github.tvbox.osc.util.DefaultConfig; |
|
|
|
|
import com.github.tvbox.osc.util.HawkConfig; |
|
|
|
|
import com.github.tvbox.osc.util.MD5; |
|
|
|
|
import com.google.gson.Gson; |
|
|
|
|
import com.google.gson.JsonElement; |
|
|
|
|
import com.google.gson.JsonObject; |
|
|
|
|
@ -27,6 +29,11 @@ import com.orhanobut.hawk.Hawk; |
|
|
|
|
|
|
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
import java.io.FileOutputStream; |
|
|
|
|
import java.io.InputStreamReader; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
@ -71,45 +78,22 @@ public class ApiConfig { |
|
|
|
|
return instance; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void loadConfig(LoadConfigCallback callback, Activity activity) { |
|
|
|
|
/*boolean isSourceModeLocal = Hawk.get(HawkConfig.SOURCE_MODE_LOCAL, false); |
|
|
|
|
if (isSourceModeLocal) { |
|
|
|
|
loadConfigLocal(callback, activity); |
|
|
|
|
} else { |
|
|
|
|
loadConfigServer(callback, activity); |
|
|
|
|
}*/ |
|
|
|
|
loadConfigServer(callback, activity); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void loadJar(String spider, LoadConfigCallback callback) { |
|
|
|
|
OkGo.<byte[]>get(spider).execute(new AbsCallback<byte[]>() { |
|
|
|
|
@Override |
|
|
|
|
public byte[] convertResponse(okhttp3.Response response) { |
|
|
|
|
try { |
|
|
|
|
return response.body().bytes(); |
|
|
|
|
} catch (Throwable th) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onFinish() { |
|
|
|
|
super.onFinish(); |
|
|
|
|
public void loadConfig(boolean useCache, LoadConfigCallback callback, Activity activity) { |
|
|
|
|
String apiUrl = Hawk.get(HawkConfig.API_URL, ""); |
|
|
|
|
if (apiUrl.isEmpty()) { |
|
|
|
|
callback.error("-1"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
File cache = new File(App.getInstance().getFilesDir().getAbsolutePath() + "/" + MD5.encode(apiUrl)); |
|
|
|
|
if (useCache && cache.exists()) { |
|
|
|
|
try { |
|
|
|
|
parseJson(apiUrl, cache); |
|
|
|
|
callback.success(); |
|
|
|
|
return; |
|
|
|
|
} catch (Throwable th) { |
|
|
|
|
th.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onSuccess(Response<byte[]> response) { |
|
|
|
|
if (response != null && response.body() != null) { |
|
|
|
|
jarLoader.load(response.body()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void loadConfigServer(LoadConfigCallback callback, Activity activity) { |
|
|
|
|
String apiUrl = Hawk.get(HawkConfig.API_URL, ""); |
|
|
|
|
} |
|
|
|
|
String apiFix = apiUrl; |
|
|
|
|
if (apiUrl.startsWith("clan://")) { |
|
|
|
|
apiFix = clanToAddress(apiUrl); |
|
|
|
|
@ -119,7 +103,21 @@ public class ApiConfig { |
|
|
|
|
@Override |
|
|
|
|
public void onSuccess(Response<String> response) { |
|
|
|
|
try { |
|
|
|
|
String json = response.body(); |
|
|
|
|
parseJson(apiUrl, response.body()); |
|
|
|
|
try { |
|
|
|
|
File cacheDir = cache.getParentFile(); |
|
|
|
|
if (!cacheDir.exists()) |
|
|
|
|
cacheDir.mkdirs(); |
|
|
|
|
if (cache.exists()) |
|
|
|
|
cache.delete(); |
|
|
|
|
FileOutputStream fos = new FileOutputStream(cache); |
|
|
|
|
fos.write(json.getBytes("UTF-8")); |
|
|
|
|
fos.flush(); |
|
|
|
|
fos.close(); |
|
|
|
|
} catch (Throwable th) { |
|
|
|
|
th.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
callback.success(); |
|
|
|
|
} catch (Throwable th) { |
|
|
|
|
th.printStackTrace(); |
|
|
|
|
@ -130,6 +128,15 @@ public class ApiConfig { |
|
|
|
|
@Override |
|
|
|
|
public void onError(Response<String> response) { |
|
|
|
|
super.onError(response); |
|
|
|
|
if (cache.exists()) { |
|
|
|
|
try { |
|
|
|
|
parseJson(apiUrl, cache); |
|
|
|
|
callback.success(); |
|
|
|
|
return; |
|
|
|
|
} catch (Throwable th) { |
|
|
|
|
th.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
callback.error("拉取配置失败"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -148,11 +155,71 @@ public class ApiConfig { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void loadJar(String spider, LoadConfigCallback callback) { |
|
|
|
|
String[] urls = spider.split(";md5;"); |
|
|
|
|
String jarUrl = urls[0]; |
|
|
|
|
String md5 = urls.length > 1 ? urls[1].trim() : ""; |
|
|
|
|
File cache = new File(App.getInstance().getFilesDir().getAbsolutePath() + "/csp.jar"); |
|
|
|
|
|
|
|
|
|
if (!md5.isEmpty()) { |
|
|
|
|
if (cache.exists() && MD5.getFileMd5(cache).equalsIgnoreCase(md5)) { |
|
|
|
|
if (jarLoader.load(cache.getAbsolutePath())) { |
|
|
|
|
callback.success(); |
|
|
|
|
} else { |
|
|
|
|
callback.error(""); |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
OkGo.<File>get(jarUrl).execute(new AbsCallback<File>() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public File convertResponse(okhttp3.Response response) throws Throwable { |
|
|
|
|
File cacheDir = cache.getParentFile(); |
|
|
|
|
if (!cacheDir.exists()) |
|
|
|
|
cacheDir.mkdirs(); |
|
|
|
|
if (cache.exists()) |
|
|
|
|
cache.delete(); |
|
|
|
|
FileOutputStream fos = new FileOutputStream(cache); |
|
|
|
|
fos.write(response.body().bytes()); |
|
|
|
|
fos.flush(); |
|
|
|
|
fos.close(); |
|
|
|
|
return cache; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onSuccess(Response<File> response) { |
|
|
|
|
if (response.body().exists()) { |
|
|
|
|
if (jarLoader.load(response.body().getAbsolutePath())) { |
|
|
|
|
callback.success(); |
|
|
|
|
} else { |
|
|
|
|
callback.error(""); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
callback.error(""); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void parseJson(String apiUrl, File f) throws Throwable { |
|
|
|
|
System.out.println("从本地缓存加载" + f.getAbsolutePath()); |
|
|
|
|
BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8")); |
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
String s = ""; |
|
|
|
|
while ((s = bReader.readLine()) != null) { |
|
|
|
|
sb.append(s + "\n"); |
|
|
|
|
} |
|
|
|
|
bReader.close(); |
|
|
|
|
parseJson(apiUrl, sb.toString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void parseJson(String apiUrl, String jsonStr) { |
|
|
|
|
JsonObject infoJson = new Gson().fromJson(jsonStr, JsonObject.class); |
|
|
|
|
// spider
|
|
|
|
|
spider = DefaultConfig.safeJsonString(infoJson, "spider", ""); |
|
|
|
|
spider = spider.split(";md5;")[0]; |
|
|
|
|
// 远端站点源
|
|
|
|
|
for (JsonElement opt : infoJson.get("sites").getAsJsonArray()) { |
|
|
|
|
JsonObject obj = (JsonObject) opt; |
|
|
|
|
|