Clean code

This commit is contained in:
FongMi 2023-11-20 17:15:18 +08:00
parent aba1d18b78
commit 65a8859471
8 changed files with 88 additions and 100 deletions

View File

@ -17,6 +17,7 @@ import com.github.catvod.BuildConfig;
import com.github.catvod.bean.Result;
import com.github.catvod.bean.Sub;
import com.github.catvod.bean.Vod;
import com.github.catvod.bean.ali.Cache;
import com.github.catvod.bean.ali.Code;
import com.github.catvod.bean.ali.Data;
import com.github.catvod.bean.ali.Download;
@ -60,9 +61,7 @@ public class AliYun {
private AlertDialog dialog;
private String refreshToken;
private Share share;
private OAuth oauth;
private Drive drive;
private User user;
private Cache cache;
private static class Loader {
static volatile AliYun INSTANCE = new AliYun();
@ -72,24 +71,14 @@ public class AliYun {
return Loader.INSTANCE;
}
public File getUserCache() {
return Path.tv("aliyundrive_user");
}
public File getOAuthCache() {
return Path.tv("aliyundrive_oauth");
}
public File getDriveCache() {
return Path.tv("aliyundrive_drive");
public File getCache() {
return Path.tv("aliyun");
}
private AliYun() {
Init.checkPermission();
tempIds = new ArrayList<>();
user = User.objectFrom(Path.read(getUserCache()));
oauth = OAuth.objectFrom(Path.read(getOAuthCache()));
drive = Drive.objectFrom(Path.read(getDriveCache()));
cache = Cache.objectFrom(Path.read(getCache()));
}
public void setRefreshToken(String token) {
@ -100,7 +89,7 @@ public class AliYun {
Object[] result = new Object[3];
result[0] = 200;
result[1] = "text/plain";
result[2] = new ByteArrayInputStream(user.getRefreshToken().getBytes());
result[2] = new ByteArrayInputStream(cache.getUser().getRefreshToken().getBytes());
return result;
}
@ -115,13 +104,13 @@ public class AliYun {
HashMap<String, String> headers = getHeader();
headers.put("x-share-token", share.getShareToken());
headers.put("X-Canary", "client=Android,app=adrive,version=v4.3.1");
if (user.isAuthed()) headers.put("authorization", user.getAuthorization());
if (cache.getUser().isAuthed()) headers.put("authorization", cache.getUser().getAuthorization());
return headers;
}
private HashMap<String, String> getHeaderOpen() {
HashMap<String, String> headers = getHeader();
headers.put("authorization", oauth.getAuthorization());
headers.put("authorization", cache.getOAuth().getAuthorization());
return headers;
}
@ -130,7 +119,7 @@ public class AliYun {
OkResult result = OkHttp.post(api, param.toString(), getHeader());
SpiderDebug.log(result.getCode() + "," + api + "," + result.getBody());
if (isManyRequest(result.getBody())) return false;
oauth = OAuth.objectFrom(result.getBody()).save();
cache.setOAuth(OAuth.objectFrom(result.getBody()));
return true;
}
@ -161,7 +150,7 @@ public class AliYun {
private boolean isManyRequest(String result) {
if (!result.contains("Too Many Requests")) return false;
Utils.notify("洗洗睡吧Too Many Requests。");
oauth.clean().save();
cache.getOAuth().clean();
return true;
}
@ -185,31 +174,31 @@ public class AliYun {
try {
SpiderDebug.log("refreshAccessToken...");
JsonObject param = new JsonObject();
String token = user.getRefreshToken();
String token = cache.getUser().getRefreshToken();
if (token.isEmpty()) token = refreshToken;
if (token != null && token.startsWith("http")) token = OkHttp.string(token).trim();
param.addProperty("refresh_token", token);
param.addProperty("grant_type", "refresh_token");
String json = post("https://auth.aliyundrive.com/v2/account/token", param);
user = User.objectFrom(json).save();
if (user.getAccessToken().isEmpty()) throw new Exception(json);
cache.setUser(User.objectFrom(json));
if (cache.getUser().getAccessToken().isEmpty()) throw new Exception(json);
return true;
} catch (Exception e) {
if (e instanceof TimeoutException) return onTimeout();
cache.getUser().clean();
e.printStackTrace();
user.clean().save();
stopService();
startFlow();
return true;
} finally {
while (user.getAccessToken().isEmpty()) SystemClock.sleep(250);
while (cache.getUser().getAccessToken().isEmpty()) SystemClock.sleep(250);
}
}
private void getDriveId() {
SpiderDebug.log("Get Drive Id...");
String json = auth("https://user.aliyundrive.com/v2/user/get", "{}", true);
drive = Drive.objectFrom(json).save();
cache.setDrive(Drive.objectFrom(json));
}
private boolean oauthRequest() {
@ -231,11 +220,11 @@ public class AliYun {
}
private boolean refreshOpenToken() {
if (oauth.getRefreshToken().isEmpty()) return oauthRequest();
if (cache.getOAuth().getRefreshToken().isEmpty()) return oauthRequest();
SpiderDebug.log("refreshOpenToken...");
JsonObject param = new JsonObject();
param.addProperty("grant_type", "refresh_token");
param.addProperty("refresh_token", oauth.getRefreshToken());
param.addProperty("refresh_token", cache.getOAuth().getRefreshToken());
return alist("token", param);
}
@ -338,7 +327,7 @@ public class AliYun {
tempIds.add(0, copy(shareId, fileId));
JsonObject param = new JsonObject();
param.addProperty("file_id", tempIds.get(0));
param.addProperty("drive_id", drive.getDriveId());
param.addProperty("drive_id", cache.getDrive().getDriveId());
String json = oauth("openFile/getDownloadUrl", param.toString(), true);
return Download.objectFrom(json).getUrl();
} catch (Exception e) {
@ -356,7 +345,7 @@ public class AliYun {
tempIds.add(0, copy(shareId, fileId));
JsonObject param = new JsonObject();
param.addProperty("file_id", tempIds.get(0));
param.addProperty("drive_id", drive.getDriveId());
param.addProperty("drive_id", cache.getDrive().getDriveId());
param.addProperty("category", "live_transcoding");
param.addProperty("url_expire_sec", "14400");
String json = oauth("openFile/getVideoPreviewPlayInfo", param.toString(), true);
@ -399,10 +388,10 @@ public class AliYun {
}
private String copy(String shareId, String fileId) {
if (drive.getDriveId().isEmpty()) getDriveId();
if (cache.getDrive().getDriveId().isEmpty()) getDriveId();
SpiderDebug.log("Copy..." + fileId);
String json = "{\"requests\":[{\"body\":{\"file_id\":\"%s\",\"share_id\":\"%s\",\"auto_rename\":true,\"to_parent_file_id\":\"root\",\"to_drive_id\":\"%s\"},\"headers\":{\"Content-Type\":\"application/json\"},\"id\":\"0\",\"method\":\"POST\",\"url\":\"/file/copy\"}],\"resource\":\"file\"}";
json = String.format(json, fileId, shareId, drive.getDriveId());
json = String.format(json, fileId, shareId, cache.getDrive().getDriveId());
Resp resp = Resp.objectFrom(auth("adrive/v2/batch", json, true));
return resp.getResponse().getBody().getFileId();
}
@ -418,7 +407,7 @@ public class AliYun {
private boolean delete(String fileId) {
SpiderDebug.log("Delete..." + fileId);
String json = "{\"requests\":[{\"body\":{\"drive_id\":\"%s\",\"file_id\":\"%s\"},\"headers\":{\"Content-Type\":\"application/json\"},\"id\":\"%s\",\"method\":\"POST\",\"url\":\"/file/delete\"}],\"resource\":\"file\"}";
json = String.format(json, drive.getDriveId(), fileId, fileId);
json = String.format(json, cache.getDrive().getDriveId(), fileId, fileId);
Resp resp = Resp.objectFrom(auth("adrive/v2/batch", json, true));
return resp.getResponse().getStatus() == 404;
}
@ -516,9 +505,9 @@ public class AliYun {
}
private void setToken(String value) {
cache.getUser().setRefreshToken(value);
SpiderDebug.log("Token:" + value);
Utils.notify("Token:" + value);
user.setRefreshToken(value);
refreshAccessToken();
stopService();
}

View File

@ -0,0 +1,58 @@
package com.github.catvod.bean.ali;
import com.github.catvod.api.AliYun;
import com.github.catvod.spider.Init;
import com.github.catvod.utils.Path;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
public class Cache {
@SerializedName("user")
private User user;
@SerializedName("oauth")
private OAuth oauth;
@SerializedName("drive")
private Drive drive;
public static Cache objectFrom(String str) {
Cache item = new Gson().fromJson(str, Cache.class);
return item == null ? new Cache() : item;
}
public User getUser() {
return user == null ? new User() : user;
}
public void setUser(User user) {
this.user = user;
this.save();
}
public OAuth getOAuth() {
return oauth == null ? new OAuth() : oauth;
}
public void setOAuth(OAuth oauth) {
this.oauth = oauth;
this.save();
}
public Drive getDrive() {
return drive == null ? new Drive() : drive;
}
public void setDrive(Drive drive) {
this.drive = drive;
this.save();
}
public void save() {
Init.execute(() -> Path.write(AliYun.get().getCache(), toString()));
}
@Override
public String toString() {
return new Gson().toJson(this);
}
}

View File

@ -2,9 +2,6 @@ package com.github.catvod.bean.ali;
import android.text.TextUtils;
import com.github.catvod.api.AliYun;
import com.github.catvod.utils.FileUtil;
import com.github.catvod.utils.Path;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
@ -31,20 +28,4 @@ public class Drive {
public String getDriveId() {
return getResourceDriveId().isEmpty() ? getDefaultDriveId() : getResourceDriveId();
}
public Drive clean() {
this.defaultDriveId = "";
this.resourceDriveId = "";
return this;
}
public Drive save() {
Path.write(AliYun.get().getDriveCache(), toString());
return this;
}
@Override
public String toString() {
return new Gson().toJson(this);
}
}

View File

@ -2,9 +2,6 @@ package com.github.catvod.bean.ali;
import android.text.TextUtils;
import com.github.catvod.api.AliYun;
import com.github.catvod.utils.FileUtil;
import com.github.catvod.utils.Path;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
@ -38,19 +35,8 @@ public class OAuth {
return getTokenType() + " " + getAccessToken();
}
public OAuth clean() {
public void clean() {
this.refreshToken = "";
this.accessToken = "";
return this;
}
public OAuth save() {
Path.write(AliYun.get().getOAuthCache(), toString());
return this;
}
@Override
public String toString() {
return new Gson().toJson(this);
}
}

View File

@ -2,18 +2,11 @@ package com.github.catvod.bean.ali;
import android.text.TextUtils;
import com.github.catvod.api.AliYun;
import com.github.catvod.utils.FileUtil;
import com.github.catvod.utils.Path;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
public class User {
@SerializedName("default_drive_id")
private String driveId;
@SerializedName("user_id")
private String userId;
@SerializedName("token_type")
private String tokenType;
@SerializedName("access_token")
@ -26,14 +19,6 @@ public class User {
return item == null ? new User() : item;
}
public String getDriveId() {
return TextUtils.isEmpty(driveId) ? "" : driveId;
}
public String getUserId() {
return TextUtils.isEmpty(userId) ? "" : userId;
}
public String getTokenType() {
return TextUtils.isEmpty(tokenType) ? "" : tokenType;
}
@ -58,19 +43,8 @@ public class User {
return getTokenType().length() > 0 && getAccessToken().length() > 0;
}
public User clean() {
public void clean() {
this.refreshToken = "";
this.accessToken = "";
return this;
}
public User save() {
Path.write(AliYun.get().getUserCache(), toString());
return this;
}
@Override
public String toString() {
return new Gson().toJson(this);
}
}

View File

@ -66,7 +66,7 @@ public class Bili extends Spider {
private void setCookie() {
cookie = extend.get("cookie").getAsString();
if (cookie.startsWith("http")) cookie = OkHttp.string(cookie).trim();
if (TextUtils.isEmpty(cookie)) cookie = Path.read(getUserCache());
if (TextUtils.isEmpty(cookie)) cookie = Path.read(getCache());
if (TextUtils.isEmpty(cookie)) cookie = COOKIE;
}
@ -77,8 +77,8 @@ public class Bili extends Spider {
return items;
}
private File getUserCache() {
return Path.tv("bilibili_user");
private File getCache() {
return Path.tv("bilibili");
}
@Override

Binary file not shown.

View File

@ -1 +1 @@
83ec3be65a651c0687d039c44d1fa7fe
ff30a37c63ac1f063ee42f1fb9393c5c