Clean code and fix ali

This commit is contained in:
FongMi 2023-08-28 15:06:29 +08:00
parent a0133eba1e
commit 15eeea6aeb
5 changed files with 130 additions and 155 deletions

View File

@ -60,10 +60,9 @@ public class AliYun {
private String refreshToken; private String refreshToken;
private String shareToken; private String shareToken;
private String shareId; private String shareId;
private String driveId;
private OAuth oauth; private OAuth oauth;
private User user;
private Drive drive; private Drive drive;
private User user;
private static class Loader { private static class Loader {
static volatile AliYun INSTANCE = new AliYun(); static volatile AliYun INSTANCE = new AliYun();
@ -87,8 +86,8 @@ public class AliYun {
private AliYun() { private AliYun() {
tempIds = new ArrayList<>(); tempIds = new ArrayList<>();
oauth = OAuth.objectFrom(FileUtil.read(getOAuthCache()));
user = User.objectFrom(FileUtil.read(getUserCache())); user = User.objectFrom(FileUtil.read(getUserCache()));
oauth = OAuth.objectFrom(FileUtil.read(getOAuthCache()));
drive = Drive.objectFrom(FileUtil.read(getDriveCache())); drive = Drive.objectFrom(FileUtil.read(getDriveCache()));
} }
@ -105,9 +104,10 @@ public class AliYun {
} }
public void setShareId(String shareId) { public void setShareId(String shareId) {
if (!getOAuthCache().exists()) oauth.clean().save();
if (!getUserCache().exists()) user.clean().save(); if (!getUserCache().exists()) user.clean().save();
if (!getOAuthCache().exists()) oauth.clean().save();
if (!getDriveCache().exists()) drive.clean().save(); if (!getDriveCache().exists()) drive.clean().save();
if (shareId.equals(this.shareId)) return;
this.shareId = shareId; this.shareId = shareId;
refreshShareToken(); refreshShareToken();
} }
@ -194,7 +194,7 @@ public class AliYun {
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
String token = user.getRefreshToken(); String token = user.getRefreshToken();
if (token.isEmpty()) token = refreshToken; if (token.isEmpty()) token = refreshToken;
if (token.startsWith("http")) token = OkHttp.string(token).trim(); if (token != null && token.startsWith("http")) token = OkHttp.string(token).trim();
param.addProperty("refresh_token", token); param.addProperty("refresh_token", token);
param.addProperty("grant_type", "refresh_token"); param.addProperty("grant_type", "refresh_token");
String json = post("https://auth.aliyundrive.com/v2/account/token", param); String json = post("https://auth.aliyundrive.com/v2/account/token", param);
@ -214,10 +214,9 @@ public class AliYun {
} }
private void getDriveId() { private void getDriveId() {
SpiderDebug.log("Obtain drive id..."); SpiderDebug.log("Get Drive Id...");
String result = auth("https://user.aliyundrive.com/v2/user/get", "{}", false); String json = auth("https://user.aliyundrive.com/v2/user/get", "{}", true);
drive = Drive.objectFrom(result).save(); drive = Drive.objectFrom(json).save();
driveId = drive.getResourceDriveId().isEmpty() ? drive.getDriveId() : drive.getResourceDriveId();
} }
private boolean oauthRequest() { private boolean oauthRequest() {
@ -340,12 +339,11 @@ public class AliYun {
public String getDownloadUrl(String fileId) { public String getDownloadUrl(String fileId) {
try { try {
getDriveId();
SpiderDebug.log("getDownloadUrl..." + fileId); SpiderDebug.log("getDownloadUrl..." + fileId);
tempIds.add(0, copy(fileId, true)); tempIds.add(0, copy(fileId));
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("file_id", tempIds.get(0)); param.addProperty("file_id", tempIds.get(0));
param.addProperty("drive_id", driveId); param.addProperty("drive_id", drive.getDriveId());
String json = oauth("openFile/getDownloadUrl", param.toString(), true); String json = oauth("openFile/getDownloadUrl", param.toString(), true);
return Download.objectFrom(json).getUrl(); return Download.objectFrom(json).getUrl();
} catch (Exception e) { } catch (Exception e) {
@ -358,12 +356,11 @@ public class AliYun {
public Preview.Info getVideoPreviewPlayInfo(String fileId) { public Preview.Info getVideoPreviewPlayInfo(String fileId) {
try { try {
getDriveId();
SpiderDebug.log("getVideoPreviewPlayInfo..." + fileId); SpiderDebug.log("getVideoPreviewPlayInfo..." + fileId);
tempIds.add(0, copy(fileId, true)); tempIds.add(0, copy(fileId));
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("file_id", tempIds.get(0)); param.addProperty("file_id", tempIds.get(0));
param.addProperty("drive_id", driveId); param.addProperty("drive_id", drive.getDriveId());
param.addProperty("category", "live_transcoding"); param.addProperty("category", "live_transcoding");
param.addProperty("url_expire_sec", "14400"); param.addProperty("url_expire_sec", "14400");
String json = oauth("openFile/getVideoPreviewPlayInfo", param.toString(), true); String json = oauth("openFile/getVideoPreviewPlayInfo", param.toString(), true);
@ -405,14 +402,12 @@ public class AliYun {
return subs; return subs;
} }
private String copy(String fileId, boolean retry) throws Exception { private String copy(String fileId) {
SpiderDebug.log("Copy..." + fileId); if (drive.getDriveId().isEmpty()) getDriveId();
SpiderDebug.log("Copy..." + fileId + "," + shareId + "," + drive.getDriveId());
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\"}"; 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, driveId); json = String.format(json, fileId, shareId, drive.getDriveId());
Res res = Res.objectFrom(auth("adrive/v2/batch", json, true)); Res res = Res.objectFrom(auth("adrive/v2/batch", json, true));
if (res.getResponse().getStatus() == 403 && retry) refreshShareToken();
if (res.getResponse().getStatus() == 403 && retry) copy(fileId, false);
if (res.getResponse().getStatus() == 403 && !retry) throw new Exception(res.getResponse().getBody().getMessage());
return res.getResponse().getBody().getFileId(); return res.getResponse().getBody().getFileId();
} }
@ -427,7 +422,7 @@ public class AliYun {
private boolean delete(String fileId) { private boolean delete(String fileId) {
SpiderDebug.log("Delete..." + 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\"}"; 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, driveId, fileId, fileId); json = String.format(json, drive.getDriveId(), fileId, fileId);
Res res = Res.objectFrom(auth("adrive/v2/batch", json, true)); Res res = Res.objectFrom(auth("adrive/v2/batch", json, true));
return res.getResponse().getStatus() == 404; return res.getResponse().getStatus() == 404;
} }

View File

@ -10,43 +10,29 @@ import com.google.gson.annotations.SerializedName;
public class Drive { public class Drive {
@SerializedName("default_drive_id") @SerializedName("default_drive_id")
private String driveId; private String defaultDriveId;
@SerializedName("user_id")
private String userId;
@SerializedName("backup_drive_id")
private String backupDriveId;
@SerializedName("resource_drive_id") @SerializedName("resource_drive_id")
private String resourceDriveId; private String resourceDriveId;
@SerializedName("sbox_drive_id")
private String sboxDriveId;
public static Drive objectFrom(String str) { public static Drive objectFrom(String str) {
Drive item = new Gson().fromJson(str, Drive.class); Drive item = new Gson().fromJson(str, Drive.class);
return item == null ? new Drive() : item; return item == null ? new Drive() : item;
} }
public String getDriveId() { private String getDefaultDriveId() {
return TextUtils.isEmpty(driveId) ? "" : driveId; return TextUtils.isEmpty(defaultDriveId) ? "" : defaultDriveId;
} }
public String getUserId() { private String getResourceDriveId() {
return TextUtils.isEmpty(userId) ? "" : userId;
}
public String getBackupDriveId() {
return TextUtils.isEmpty(backupDriveId) ? "" : backupDriveId;
}
public String getResourceDriveId() {
return TextUtils.isEmpty(resourceDriveId) ? "" : resourceDriveId; return TextUtils.isEmpty(resourceDriveId) ? "" : resourceDriveId;
} }
public String getSboxDriveId() { public String getDriveId() {
return TextUtils.isEmpty(sboxDriveId) ? "" : sboxDriveId; return getResourceDriveId().isEmpty() ? getDefaultDriveId() : getResourceDriveId();
} }
public Drive clean() { public Drive clean() {
this.driveId = ""; this.defaultDriveId = "";
this.resourceDriveId = ""; this.resourceDriveId = "";
return this; return this;
} }

View File

@ -1,6 +1,7 @@
package com.github.catvod.bean.ali; package com.github.catvod.bean.ali;
import android.text.TextUtils; import android.text.TextUtils;
import com.github.catvod.utils.ChineseComparator; import com.github.catvod.utils.ChineseComparator;
import com.github.catvod.utils.Utils; import com.github.catvod.utils.Utils;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -85,7 +86,7 @@ public class Item implements Comparable<Item> {
public String getDisplayName() { public String getDisplayName() {
return TextUtils.join(" ", Arrays.asList(getParent(), getName(), getSize())).trim(); return TextUtils.join(" ", Arrays.asList(getParent(), getName(), getSize())).trim();
} }
@Override @Override
public int compareTo(Item item) { public int compareTo(Item item) {
return ChineseComparator.compare(this.getDisplayName(), item.getDisplayName()); return ChineseComparator.compare(this.getDisplayName(), item.getDisplayName());

View File

@ -56,7 +56,7 @@ public class Ali extends Spider {
*/ */
public String detailContentVodPlayFrom(List<String> ids) { public String detailContentVodPlayFrom(List<String> ids) {
List<String> playFrom = new ArrayList<>(); List<String> playFrom = new ArrayList<>();
if (ids.size() < 1) return TextUtils.join("$$$", Arrays.asList("原畫", "普畫")); if (ids.size() < 2) return TextUtils.join("$$$", Arrays.asList("原畫", "普畫"));
for (int i = 1; i <= ids.size(); i++) { for (int i = 1; i <= ids.size(); i++) {
playFrom.add(String.format(Locale.getDefault(), "原畫#%02d", i)); playFrom.add(String.format(Locale.getDefault(), "原畫#%02d", i));
playFrom.add(String.format(Locale.getDefault(), "普畫#%02d", i)); playFrom.add(String.format(Locale.getDefault(), "普畫#%02d", i));

View File

@ -11,114 +11,107 @@ import java.util.Locale;
* @author Oiltea * @author Oiltea
*/ */
public class ChineseComparator { public class ChineseComparator {
/** private static final Integer TYPE_NUMBER = 0;
* 数字类型 private static final Integer TYPE_CHARACTER = 1;
*/
private static final Integer TYPE_NUMBER = 0; public static int compare(String o1, String o2) {
// 根据字符数组生成带分类的字符列表
/** // List<Object>的第一位为该字符的类型(TYPE_NUMBER, TYPE_CHARACTER)
* 字符类型非数字 // List<Object>的第二位为该字符的内容(一位数字, 一位非数字, 多位数字)
*/ List<List<Object>> o1CharList = getCharList(o1);
private static final Integer TYPE_CHARACTER = 1; List<List<Object>> o2CharList = getCharList(o2);
public static int compare(String o1, String o2) { // 统一CharList的长度
// 根据字符数组生成带分类的字符列表 int max = Math.max(o1CharList.size(), o2CharList.size());
// List<Object>的第一位为该字符的类型(TYPE_NUMBER, TYPE_CHARACTER) while (o1CharList.size() < max) {
// List<Object>的第二位为该字符的内容(一位数字, 一位非数字, 多位数字) o1CharList.add(new ArrayList<>());
List<List<Object>> o1CharList = getCharList(o1); }
List<List<Object>> o2CharList = getCharList(o2); while (o2CharList.size() < max) {
o2CharList.add(new ArrayList<>());
// 统一CharList的长度 }
int max = Math.max(o1CharList.size(), o2CharList.size());
while (o1CharList.size() < max) { // 开始比较
o1CharList.add(new ArrayList<>()); int compare = 0;
} for (int i = 0; i < max; i++) {
while (o2CharList.size() < max) { List<Object> o1list = o1CharList.get(i);
o2CharList.add(new ArrayList<>()); List<Object> o2list = o2CharList.get(i);
}
// CharList短的排在前面
// 开始比较 if (o1list.size() == 0) {
int compare = 0; compare = -1;
for (int i = 0; i < max; i++) { break;
List<Object> o1list = o1CharList.get(i); }
List<Object> o2list = o2CharList.get(i); if (o2list.size() == 0) {
compare = 1;
// CharList短的排在前面 break;
if (o1list.size() == 0) { }
compare = -1;
break; // 先比较类型
} Integer o1Type = (Integer) o1list.get(0);
if (o2list.size() == 0) { Integer o2Type = (Integer) o2list.get(0);
compare = 1; int typeCompare = Integer.compare(o1Type, o2Type);
break; if (typeCompare != 0) {
} // 类型不同则数字在前非数字在后
compare = typeCompare;
// 先比较类型 break;
Integer o1Type = (Integer) o1list.get(0); } else {
Integer o2Type = (Integer) o2list.get(0); // 类型相同则比较内容
int typeCompare = Integer.compare(o1Type, o2Type); if (TYPE_NUMBER.equals(o1Type)) {
if (typeCompare != 0) { // 比较数字
// 类型不同则数字在前非数字在后 int o1Content = Integer.parseInt(o1list.get(1).toString());
compare = typeCompare; int o2Content = Integer.parseInt(o2list.get(1).toString());
break; compare = Integer.compare(o1Content, o2Content);
} else { } else if (TYPE_CHARACTER.equals(o1Type)) {
// 类型相同则比较内容 // 比较非数字
if (TYPE_NUMBER.equals(o1Type)) { String o1Content = (String) o1list.get(1);
// 比较数字 String o2Content = (String) o2list.get(1);
int o1Content = Integer.parseInt(o1list.get(1).toString()); compare = Collator.getInstance(Locale.CHINESE).compare(o1Content, o2Content);
int o2Content = Integer.parseInt(o2list.get(1).toString()); }
compare = Integer.compare(o1Content, o2Content); // 如果不相等则退出比较
} else if (TYPE_CHARACTER.equals(o1Type)) { if (compare != 0) {
// 比较非数字 break;
String o1Content = (String) o1list.get(1); }
String o2Content = (String) o2list.get(1); }
compare = Collator.getInstance(Locale.CHINESE).compare(o1Content, o2Content); }
} return compare;
// 如果不相等则退出比较 }
if (compare != 0) {
break; /**
} * 根据字符数组生成带分类的字符列表
} *
} * @param text 字符串
return compare; * @return 带分类的字符列表List<Object>的第一位为该字符的类型(TYPE_NUMBER, TYPE_CHARACTER)第二位为该字符的内容
} */
private static List<List<Object>> getCharList(String text) {
/** char[] chars = text.toCharArray();
* 根据字符数组生成带分类的字符列表 List<List<Object>> charList = new ArrayList<>();
* for (int i = 0; i < chars.length; i++) {
* @param text 字符串 List<Object> list = new ArrayList<>();
* @return 带分类的字符列表List<Object>的第一位为该字符的类型(TYPE_NUMBER, TYPE_CHARACTER)第二位为该字符的内容 // 是否为数字
*/ char c = chars[i];
private static List<List<Object>> getCharList(String text) { if ((int) c >= '0' && (int) c <= '9') {
char[] chars = text.toCharArray(); StringBuilder str = new StringBuilder();
List<List<Object>> charList = new ArrayList<>(); // 下一位是否为数字如果为数字则组成多位数
for (int i = 0; i < chars.length; i++) { do {
List<Object> list = new ArrayList<>(); str.append(c);
// 是否为数字 if (i + 1 < chars.length) {
char c = chars[i]; c = chars[++i];
if ((int) c >= '0' && (int) c <= '9') { } else {
StringBuilder str = new StringBuilder(); break;
// 下一位是否为数字如果为数字则组成多位数 }
do { } while ((int) c >= '0' && (int) c <= '9');
str.append(c); if (!(i + 1 == chars.length) || !((int) c >= '0' && (int) c <= '9')) {
if (i + 1 < chars.length) { i--;
c = chars[++i]; }
} else { list.add(TYPE_NUMBER);
break; list.add(str.toString());
} } else {
} while ((int) c >= '0' && (int) c <= '9'); list.add(TYPE_CHARACTER);
if (!(i + 1 == chars.length) || !((int) c >= '0' && (int) c <= '9')) { list.add(String.valueOf(c));
i--; }
} charList.add(list);
list.add(TYPE_NUMBER); }
list.add(str.toString()); return charList;
} else { }
list.add(TYPE_CHARACTER);
list.add(String.valueOf(c));
}
charList.add(list);
}
return charList;
}
} }