pull/605/head
jhengazuji 4 months ago
parent 9755008582
commit 557a280acc
  1. 2
      app/src/main/java/com/fongmi/android/tv/api/EpgParser.java
  2. 11
      app/src/main/java/com/fongmi/android/tv/api/loader/JarLoader.java
  3. 7
      app/src/main/java/com/fongmi/android/tv/player/extractor/TVBus.java
  4. 9
      app/src/main/java/com/fongmi/android/tv/utils/Download.java
  5. 10
      catvod/src/main/java/com/github/catvod/net/OkHttp.java
  6. 35
      catvod/src/main/java/com/github/catvod/utils/Path.java
  7. 47
      quickjs/src/main/java/com/fongmi/quickjs/utils/Module.java

@ -36,7 +36,7 @@ public class EpgParser {
public static boolean start(Live live, String url) throws Exception {
File file = Path.epg(Uri.parse(url).getLastPathSegment());
if (shouldDownload(file)) Download.create(url, file).start();
Download.create(url, file).get(shouldDownload(file));
if (file.getName().endsWith(".gz")) readGzip(live, file);
else readXml(live, file);
return true;

@ -3,6 +3,7 @@ package com.fongmi.android.tv.api.loader;
import android.content.Context;
import com.fongmi.android.tv.App;
import com.fongmi.android.tv.utils.Download;
import com.fongmi.android.tv.utils.UrlUtil;
import com.github.catvod.crawler.Spider;
import com.github.catvod.crawler.SpiderNull;
@ -78,14 +79,6 @@ public class JarLoader {
}
}
private File download(String url) {
try {
return Path.write(Path.jar(url), OkHttp.bytes(url));
} catch (Exception e) {
return Path.jar(url);
}
}
public synchronized void parseJar(String key, String jar) {
if (loaders.containsKey(key)) return;
String[] texts = jar.split(";md5;");
@ -95,7 +88,7 @@ public class JarLoader {
if (!md5.isEmpty() && Util.equals(jar, md5)) {
load(key, Path.jar(jar));
} else if (jar.startsWith("http")) {
load(key, download(jar));
load(key, Download.create(jar, Path.jar(jar)).get());
} else if (jar.startsWith("file")) {
load(key, Path.local(jar));
} else if (jar.startsWith("assets")) {

@ -9,9 +9,9 @@ import com.fongmi.android.tv.api.config.LiveConfig;
import com.fongmi.android.tv.bean.Core;
import com.fongmi.android.tv.exception.ExtractException;
import com.fongmi.android.tv.player.Source;
import com.fongmi.android.tv.utils.Download;
import com.fongmi.android.tv.utils.ResUtil;
import com.fongmi.android.tv.utils.UrlUtil;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Path;
import com.google.gson.JsonObject;
import com.orhanobut.logger.Logger;
@ -47,9 +47,8 @@ public class TVBus implements Source.Extractor, Listener {
}
private String getPath(String url) {
String name = UrlUtil.path(url);
File file = new File(Path.so(), name);
if (file.length() < 10240) Path.write(file, OkHttp.bytes(url));
File file = new File(Path.so(), UrlUtil.path(url));
Download.create(url, file).get(!Path.exists(file));
return file.getAbsolutePath();
}

@ -32,6 +32,15 @@ public class Download {
this.callback = callback;
}
public File get() {
return get(true);
}
public File get(boolean force) {
if (force) performSync();
return file;
}
public void start() {
if (url.startsWith("file")) return;
if (callback == null) performSync();

@ -135,16 +135,6 @@ public class OkHttp {
}
}
public static byte[] bytes(String url) {
if (!url.startsWith("http")) return new byte[0];
try (Response res = newCall(url).execute()) {
return res.body().bytes();
} catch (Exception e) {
e.printStackTrace();
return new byte[0];
}
}
public static Call newCall(String url) {
return client().newCall(new Request.Builder().url(url).build());
}

@ -128,14 +128,17 @@ public class Path {
}
public static String read(File file) {
return new String(readToByte(file), StandardCharsets.UTF_8);
try {
return new String(readToByte(file), StandardCharsets.UTF_8);
} catch (Exception e) {
return "";
}
}
public static String read(InputStream is) {
try {
return new String(readToByte(is), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
@ -149,23 +152,31 @@ public class Path {
}
private static byte[] readToByte(InputStream is) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
try (InputStream input = is; ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
int read;
byte[] buffer = new byte[16384];
while ((read = is.read(buffer)) != -1) bos.write(buffer, 0, read);
while ((read = input.read(buffer)) != -1) bos.write(buffer, 0, read);
return bos.toByteArray();
}
}
public static File write(File file, InputStream is) {
try (InputStream input = is; FileOutputStream output = new FileOutputStream(create(file))) {
int read;
byte[] buffer = new byte[16384];
while ((read = input.read(buffer)) != -1) output.write(buffer, 0, read);
return file;
} catch (IOException e) {
return file;
}
}
public static File write(File file, byte[] data) {
try {
FileOutputStream fos = new FileOutputStream(create(file));
try (FileOutputStream fos = new FileOutputStream(create(file))) {
fos.write(data);
fos.flush();
fos.close();
return file;
} catch (IOException e) {
e.printStackTrace();
return file;
}
}
@ -184,13 +195,10 @@ public class Path {
}
public static void copy(InputStream in, File out) {
try {
try (InputStream input = in; FileOutputStream output = new FileOutputStream(create(out))) {
int read;
byte[] buffer = new byte[16384];
FileOutputStream fos = new FileOutputStream(create(out));
while ((read = in.read(buffer)) != -1) fos.write(buffer, 0, read);
fos.close();
in.close();
while ((read = input.read(buffer)) != -1) output.write(buffer, 0, read);
} catch (IOException ignored) {
}
}
@ -227,7 +235,6 @@ public class Path {
Shell.exec("chmod 777 " + file);
return file;
} catch (IOException e) {
e.printStackTrace();
return file;
}
}

@ -1,18 +1,15 @@
package com.fongmi.quickjs.utils;
import android.net.Uri;
import android.text.TextUtils;
import android.util.LruCache;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Asset;
import com.github.catvod.utils.Path;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ConcurrentHashMap;
public class Module {
private final ConcurrentHashMap<String, String> cache;
private final LruCache<String, String> cache;
private static final int MAX_SIZE = 50;
private static class Loader {
static volatile Module INSTANCE = new Module();
@ -23,37 +20,15 @@ public class Module {
}
public Module() {
this.cache = new ConcurrentHashMap<>();
this.cache = new LruCache<>(MAX_SIZE);
}
public String fetch(String name) {
if (cache.contains(name)) return cache.get(name);
if (name.startsWith("http")) cache.put(name, request(name));
if (name.startsWith("assets")) cache.put(name, Asset.read(name));
if (name.startsWith("lib/")) cache.put(name, Asset.read("js/" + name));
return cache.get(name);
}
private String request(String url) {
try {
Uri uri = Uri.parse(url);
byte[] data = OkHttp.bytes(url);
File file = Path.js(uri.getLastPathSegment());
boolean cache = !"127.0.0.1".equals(uri.getHost());
if (cache) new Thread(() -> Path.write(file, data)).start();
return new String(data, StandardCharsets.UTF_8);
} catch (Exception e) {
return cache(url);
}
}
private String cache(String url) {
try {
Uri uri = Uri.parse(url);
File file = Path.js(uri.getLastPathSegment());
return Path.exists(file) ? Path.read(file) : "";
} catch (Exception e) {
return "";
}
String content = cache.get(name);
if (!TextUtils.isEmpty(content)) return content;
if (name.startsWith("http")) cache.put(name, content = OkHttp.string(name));
else if (name.startsWith("assets")) cache.put(name, content = Asset.read(name));
else if (name.startsWith("lib/")) cache.put(name, content = Asset.read("js/" + name));
return content;
}
}

Loading…
Cancel
Save