Update market
This commit is contained in:
parent
8993c950f9
commit
dc4904859f
|
|
@ -16,6 +16,7 @@ import javax.net.ssl.SSLContext;
|
||||||
import javax.net.ssl.TrustManager;
|
import javax.net.ssl.TrustManager;
|
||||||
import javax.net.ssl.X509TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
import okhttp3.Dns;
|
import okhttp3.Dns;
|
||||||
import okhttp3.Headers;
|
import okhttp3.Headers;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
|
@ -39,8 +40,8 @@ public class OkHttp {
|
||||||
return Loader.INSTANCE;
|
return Loader.INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Response newCall(String url) throws IOException {
|
public static Response newCall(String url, String tag) throws IOException {
|
||||||
return client().newCall(new Request.Builder().url(url).build()).execute();
|
return client().newCall(new Request.Builder().url(url).tag(tag).build()).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String string(String url) {
|
public static String string(String url) {
|
||||||
|
|
@ -90,6 +91,23 @@ public class OkHttp {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void cancel(String tag) {
|
||||||
|
cancel(client(), tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void cancel(OkHttpClient client, String tag) {
|
||||||
|
for (Call call : client.dispatcher().queuedCalls()) if (tag.equals(call.request().tag())) call.cancel();
|
||||||
|
for (Call call : client.dispatcher().runningCalls()) if (tag.equals(call.request().tag())) call.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void cancelAll() {
|
||||||
|
cancelAll(client());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void cancelAll(OkHttpClient client) {
|
||||||
|
client.dispatcher().cancelAll();
|
||||||
|
}
|
||||||
|
|
||||||
private static OkHttpClient build() {
|
private static OkHttpClient build() {
|
||||||
if (get().client != null) return get().client;
|
if (get().client != null) return get().client;
|
||||||
return get().client = getBuilder().build();
|
return get().client = getBuilder().build();
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,10 @@
|
||||||
package com.github.catvod.spider;
|
package com.github.catvod.spider;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
import com.github.catvod.crawler.SpiderDebug;
|
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
|
@ -19,7 +12,6 @@ public class Init {
|
||||||
|
|
||||||
private final ExecutorService executor;
|
private final ExecutorService executor;
|
||||||
private final Handler handler;
|
private final Handler handler;
|
||||||
private Activity activity;
|
|
||||||
private Application app;
|
private Application app;
|
||||||
|
|
||||||
private static class Loader {
|
private static class Loader {
|
||||||
|
|
@ -39,18 +31,8 @@ public class Init {
|
||||||
return get().app;
|
return get().app;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Activity activity() {
|
|
||||||
return get().activity;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setActivity(Activity activity) {
|
|
||||||
this.activity = activity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void init(Context context) {
|
public static void init(Context context) {
|
||||||
get().app = ((Application) context);
|
get().app = ((Application) context);
|
||||||
SpiderDebug.log("自定義爬蟲代碼載入成功!");
|
|
||||||
registerActivityLifecycleCallbacks();
|
|
||||||
Proxy.init();
|
Proxy.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,42 +47,4 @@ public class Init {
|
||||||
public static void post(Runnable runnable, int delay) {
|
public static void post(Runnable runnable, int delay) {
|
||||||
get().handler.postDelayed(runnable, delay);
|
get().handler.postDelayed(runnable, delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void registerActivityLifecycleCallbacks() {
|
|
||||||
get().app.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
|
|
||||||
@Override
|
|
||||||
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
|
|
||||||
if (activity != activity()) get().setActivity(activity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityStarted(@NonNull Activity activity) {
|
|
||||||
if (activity != activity()) get().setActivity(activity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityResumed(@NonNull Activity activity) {
|
|
||||||
if (activity != activity()) get().setActivity(activity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityPaused(@NonNull Activity activity) {
|
|
||||||
if (activity == activity()) get().setActivity(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityStopped(@NonNull Activity activity) {
|
|
||||||
if (activity == activity()) get().setActivity(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityDestroyed(@NonNull Activity activity) {
|
|
||||||
if (activity == activity()) get().setActivity(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package com.github.catvod.spider;
|
package com.github.catvod.spider;
|
||||||
|
|
||||||
import android.app.ProgressDialog;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
|
||||||
|
|
@ -11,6 +10,7 @@ import com.github.catvod.bean.market.Item;
|
||||||
import com.github.catvod.crawler.Spider;
|
import com.github.catvod.crawler.Spider;
|
||||||
import com.github.catvod.net.OkHttp;
|
import com.github.catvod.net.OkHttp;
|
||||||
import com.github.catvod.utils.FileUtil;
|
import com.github.catvod.utils.FileUtil;
|
||||||
|
import com.github.catvod.utils.Notify;
|
||||||
import com.github.catvod.utils.Path;
|
import com.github.catvod.utils.Path;
|
||||||
import com.github.catvod.utils.Util;
|
import com.github.catvod.utils.Util;
|
||||||
|
|
||||||
|
|
@ -26,17 +26,8 @@ import okhttp3.Response;
|
||||||
|
|
||||||
public class Market extends Spider {
|
public class Market extends Spider {
|
||||||
|
|
||||||
private ProgressDialog dialog;
|
private static final String TAG = Market.class.getSimpleName();
|
||||||
private List<Data> datas;
|
private List<Data> datas;
|
||||||
private boolean busy;
|
|
||||||
|
|
||||||
public boolean isBusy() {
|
|
||||||
return busy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBusy(boolean busy) {
|
|
||||||
this.busy = busy;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(Context context, String extend) throws Exception {
|
public void init(Context context, String extend) throws Exception {
|
||||||
|
|
@ -60,34 +51,28 @@ public class Market extends Spider {
|
||||||
@Override
|
@Override
|
||||||
public String action(String action) {
|
public String action(String action) {
|
||||||
try {
|
try {
|
||||||
if (isBusy()) return "";
|
OkHttp.cancel(TAG);
|
||||||
setBusy(true);
|
String name = Uri.parse(action).getLastPathSegment();
|
||||||
Init.post(this::setDialog, 500);
|
Notify.show("正在下載..." + name);
|
||||||
Response response = OkHttp.newCall(action);
|
Response response = OkHttp.newCall(action, TAG);
|
||||||
File file = Path.create(new File(Path.download(), Uri.parse(action).getLastPathSegment()));
|
File file = Path.create(new File(Path.download(), name));
|
||||||
download(file, response.body().byteStream(), Double.parseDouble(response.header("Content-Length", "1")));
|
download(file, response.body().byteStream());
|
||||||
if (file.getName().endsWith(".zip")) FileUtil.unzip(file, Path.download());
|
if (file.getName().endsWith(".zip")) FileUtil.unzip(file, Path.download());
|
||||||
if (file.getName().endsWith(".apk")) FileUtil.openFile(file);
|
if (file.getName().endsWith(".apk")) FileUtil.openFile(file);
|
||||||
else Result.notify("下載完成");
|
|
||||||
checkCopy(action);
|
checkCopy(action);
|
||||||
response.close();
|
response.close();
|
||||||
dismiss();
|
return Result.notify("下載完成");
|
||||||
return "";
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
dismiss();
|
|
||||||
return Result.notify(e.getMessage());
|
return Result.notify(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void download(File file, InputStream is, double length) throws Exception {
|
private void download(File file, InputStream is) throws Exception {
|
||||||
try (BufferedInputStream input = new BufferedInputStream(is); FileOutputStream os = new FileOutputStream(file)) {
|
try (BufferedInputStream input = new BufferedInputStream(is); FileOutputStream os = new FileOutputStream(file)) {
|
||||||
byte[] buffer = new byte[4096];
|
byte[] buffer = new byte[16384];
|
||||||
int readBytes;
|
int readBytes;
|
||||||
long totalBytes = 0;
|
|
||||||
while ((readBytes = input.read(buffer)) != -1) {
|
while ((readBytes = input.read(buffer)) != -1) {
|
||||||
totalBytes += readBytes;
|
|
||||||
os.write(buffer, 0, readBytes);
|
os.write(buffer, 0, readBytes);
|
||||||
setProgress((int) (totalBytes / length * 100.0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -102,37 +87,8 @@ public class Market extends Spider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setDialog() {
|
@Override
|
||||||
Init.post(() -> {
|
public void destroy() {
|
||||||
try {
|
OkHttp.cancel(TAG);
|
||||||
dialog = new ProgressDialog(Init.activity());
|
|
||||||
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
|
||||||
dialog.setCancelable(false);
|
|
||||||
if (isBusy()) dialog.show();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void dismiss() {
|
|
||||||
Init.post(() -> {
|
|
||||||
try {
|
|
||||||
setBusy(false);
|
|
||||||
if (dialog != null) dialog.dismiss();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setProgress(int value) {
|
|
||||||
Init.post(() -> {
|
|
||||||
try {
|
|
||||||
if (dialog != null) dialog.setProgress(value);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1 +1 @@
|
||||||
fe1c0645034f7063eceec49e9c0f5c72
|
c77efde41ebab1daf37308a1153d3f85
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue