Clean code and change cache path to tv

This commit is contained in:
FongMi 2023-11-20 14:07:14 +08:00
parent f75bb8a679
commit a93edd1188
8 changed files with 42 additions and 88 deletions

View File

@ -73,18 +73,19 @@ public class AliYun {
} }
public File getUserCache() { public File getUserCache() {
return Path.cache("aliyundrive_user"); return Path.tv("aliyundrive_user");
} }
public File getOAuthCache() { public File getOAuthCache() {
return Path.cache("aliyundrive_oauth"); return Path.tv("aliyundrive_oauth");
} }
public File getDriveCache() { public File getDriveCache() {
return Path.cache("aliyundrive_drive"); return Path.tv("aliyundrive_drive");
} }
private AliYun() { private AliYun() {
Init.checkPermission();
tempIds = new ArrayList<>(); tempIds = new ArrayList<>();
user = User.objectFrom(Path.read(getUserCache())); user = User.objectFrom(Path.read(getUserCache()));
oauth = OAuth.objectFrom(Path.read(getOAuthCache())); oauth = OAuth.objectFrom(Path.read(getOAuthCache()));
@ -424,7 +425,7 @@ public class AliYun {
public String getMultiThreadedDownloadUrl(String shareId, String fileId) { public String getMultiThreadedDownloadUrl(String shareId, String fileId) {
String url = getDownloadUrl(shareId, fileId); String url = getDownloadUrl(shareId, fileId);
url = MultiThread.proxyUrl(url, 20); url = MultiThread.proxyUrl(url, 2);
return url; return url;
} }

View File

@ -78,7 +78,7 @@ public class Bili extends Spider {
} }
private File getUserCache() { private File getUserCache() {
return Path.cache("bilibili_user"); return Path.tv("bilibili_user");
} }
@Override @Override

View File

@ -1,8 +1,10 @@
package com.github.catvod.spider; package com.github.catvod.spider;
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
@ -53,6 +55,16 @@ public class Init {
get().handler.postDelayed(runnable, delay); get().handler.postDelayed(runnable, delay);
} }
public static void checkPermission() {
try {
Activity activity = Init.getActivity();
if (activity == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 9999);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Activity getActivity() throws Exception { public static Activity getActivity() throws Exception {
Class<?> activityThreadClass = Class.forName("android.app.ActivityThread"); Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null); Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);

View File

@ -1,11 +1,9 @@
package com.github.catvod.spider; package com.github.catvod.spider;
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import com.github.catvod.bean.Class; import com.github.catvod.bean.Class;
import com.github.catvod.bean.Result; import com.github.catvod.bean.Result;
@ -46,7 +44,7 @@ public class Market extends Spider {
public void init(Context context, String extend) throws Exception { public void init(Context context, String extend) throws Exception {
if (extend.startsWith("http")) extend = OkHttp.string(extend); if (extend.startsWith("http")) extend = OkHttp.string(extend);
datas = Data.arrayFrom(extend); datas = Data.arrayFrom(extend);
checkPermission(); Init.checkPermission();
} }
@Override @Override
@ -72,15 +70,6 @@ public class Market extends Spider {
return Result.string(vod); return Result.string(vod);
} }
private void checkPermission() {
try {
Activity activity = Init.getActivity();
if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 9999);
} catch (Exception e) {
e.printStackTrace();
}
}
private void finish() { private void finish() {
try { try {
Activity activity = Init.getActivity(); Activity activity = Init.getActivity();

View File

@ -15,10 +15,6 @@ import java.util.zip.ZipFile;
public class FileUtil { public class FileUtil {
public static File getWall(int index) {
return Path.files("wallpaper_" + index);
}
public static void openFile(File file) { public static void openFile(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

View File

@ -1,6 +1,5 @@
package com.github.catvod.utils; package com.github.catvod.utils;
import com.github.catvod.downloader.MultiThreadedMemoryDownloader;
import com.github.catvod.spider.Proxy; import com.github.catvod.spider.Proxy;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -17,21 +16,14 @@ public class MultiThread {
public static Object[] proxy(Map<String, String> params) throws Exception { public static Object[] proxy(Map<String, String> params) throws Exception {
String url = params.get("url"); String url = params.get("url");
int threadNum = Integer.parseInt(params.get("thread")); int thread = Integer.parseInt(params.get("thread"));
Map<String, String> reqHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); Map<String, String> reqHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
for (Map.Entry<String, String> entry : params.entrySet()) { for (String key : params.keySet()) reqHeaders.put(key, params.get(key));
if (entry.getKey() != null) {
reqHeaders.put(entry.getKey(), entry.getValue());
}
}
if (reqHeaders.containsKey("do")) reqHeaders.remove("do"); if (reqHeaders.containsKey("do")) reqHeaders.remove("do");
if (reqHeaders.containsKey("url")) reqHeaders.remove("url"); if (reqHeaders.containsKey("url")) reqHeaders.remove("url");
if (reqHeaders.containsKey("thread")) reqHeaders.remove("thread"); if (reqHeaders.containsKey("thread")) reqHeaders.remove("thread");
MultiThreadedMemoryDownloader downloader = new MultiThreadedMemoryDownloader(url, reqHeaders, threadNum); MultiThreadedDownloader downloader = new MultiThreadedDownloader(url, reqHeaders, thread);
NanoHTTPD.Response response = downloader.start(); NanoHTTPD.Response response = downloader.start();
Object[] result = new Object[1]; return new Object[]{response};
result[0] = response;
return result;
} }
} }

View File

@ -1,4 +1,4 @@
package com.github.catvod.downloader; package com.github.catvod.utils;
import static java.lang.Thread.sleep; import static java.lang.Thread.sleep;
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse; import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
@ -21,7 +21,8 @@ import fi.iki.elonen.NanoHTTPD;
import okhttp3.Response; import okhttp3.Response;
// 多线程内存下载器 // 多线程内存下载器
public class MultiThreadedMemoryDownloader { public class MultiThreadedDownloader {
private String url; // 资源URL private String url; // 资源URL
private Map<String, String> headers; // HTTP Headers. 主要关注`Range-Bytes`这个字段. private Map<String, String> headers; // HTTP Headers. 主要关注`Range-Bytes`这个字段.
private int chunkSize = 1024 * 128; // 每个线程每轮下载的字节数. private int chunkSize = 1024 * 128; // 每个线程每轮下载的字节数.
@ -36,7 +37,7 @@ public class MultiThreadedMemoryDownloader {
private BlockingQueue<Chunk> readyChunkQueue = new LinkedBlockingQueue<>(); // 已开始下载的chunk队列有序. private BlockingQueue<Chunk> readyChunkQueue = new LinkedBlockingQueue<>(); // 已开始下载的chunk队列有序.
private Lock lock = new ReentrantLock(); // . private Lock lock = new ReentrantLock(); // .
public MultiThreadedMemoryDownloader(String url, Map<String, String> headers, int numThreads) { public MultiThreadedDownloader(String url, Map<String, String> headers, int numThreads) {
this.url = url; this.url = url;
this.headers = headers; this.headers = headers;
this.numThreads = numThreads; this.numThreads = numThreads;
@ -115,7 +116,7 @@ public class MultiThreadedMemoryDownloader {
if (hContentLength == null) { if (hContentLength == null) {
throw new Exception("missing response header: Content-Length"); throw new Exception("missing response header: Content-Length");
} }
Long contentLength = Long.parseLong(hContentLength); long contentLength = Long.parseLong(hContentLength);
// 尝试从Content-Range获取下载结束的偏移量 // 尝试从Content-Range获取下载结束的偏移量
if (this.endOffset <= 0) { if (this.endOffset <= 0) {
@ -142,9 +143,7 @@ public class MultiThreadedMemoryDownloader {
// 开启多线程下载 // 开启多线程下载
this.running = true; this.running = true;
for (int i = 0; i < this.numThreads; ++i) { for (int i = 0; i < this.numThreads; ++i) {
new Thread(() -> { new Thread(MultiThreadedDownloader.this::worker).start();
MultiThreadedMemoryDownloader.this.worker();
}).start();
} }
// 构造response // 构造response
@ -153,7 +152,7 @@ public class MultiThreadedMemoryDownloader {
NanoHTTPD.Response mResponse = newFixedLengthResponse(status, contentType, input, contentLength); NanoHTTPD.Response mResponse = newFixedLengthResponse(status, contentType, input, contentLength);
for (String key : response.headers().names()) { for (String key : response.headers().names()) {
String value = response.headers().get(key); String value = response.headers().get(key);
if (key != null && !key.equalsIgnoreCase("Content-Type") && !key.equalsIgnoreCase("Content-Length")) { if (!key.equalsIgnoreCase("Content-Type") && !key.equalsIgnoreCase("Content-Length")) {
mResponse.addHeader(key, value); mResponse.addHeader(key, value);
} }
} }
@ -296,11 +295,8 @@ public class MultiThreadedMemoryDownloader {
} }
} }
private boolean shouldFilterRequestHeaderKey(String key) { private boolean shouldFilterRequestHeaderKey(String key) {
if (key == null) { if (key == null) return true;
return true;
}
key = key.toLowerCase(); key = key.toLowerCase();
return key.equals("host") || key.equals("http-client-ip") || key.equals("remote-addr"); return key.equals("host") || key.equals("http-client-ip") || key.equals("remote-addr");
} }
@ -309,9 +305,10 @@ public class MultiThreadedMemoryDownloader {
this.running = false; this.running = false;
} }
private class Chunk { private static class Chunk {
private long startOffset;
private long endOffset; private final long startOffset;
private final long endOffset;
private byte[] buffer; private byte[] buffer;
public Chunk(long startOffset, long endOffset) { public Chunk(long startOffset, long endOffset) {

View File

@ -3,8 +3,6 @@ package com.github.catvod.utils;
import android.os.Environment; import android.os.Environment;
import android.util.Log; import android.util.Log;
import com.github.catvod.spider.Init;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -24,52 +22,21 @@ public class Path {
return file; return file;
} }
public static boolean exists(String path) { public static File download() {
return new File(path.replace("file://", "")).exists(); return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
} }
public static File root() { public static File root() {
return Environment.getExternalStorageDirectory(); return Environment.getExternalStorageDirectory();
} }
public static File cache() { public static File tv() {
return Init.context().getCacheDir(); return check(new File(root() + File.separator + "TV"));
} }
public static File files() { public static File tv(String name) {
return Init.context().getFilesDir(); if (!name.startsWith(".")) name = "." + name;
} return new File(tv(), name);
public static File download() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
public static String rootPath() {
return root().getAbsolutePath();
}
public static File root(String name) {
return new File(root(), name);
}
public static File root(String child, String name) {
return new File(check(new File(root(), child)), name);
}
public static File cache(String name) {
return new File(cache(), name);
}
public static File files(String name) {
return new File(files(), name);
}
public static String asset(String fileName) {
try {
return read(Init.context().getAssets().open(fileName));
} catch (Exception e) {
return "";
}
} }
public static String read(File file) { public static String read(File file) {