Add ali share id check

This commit is contained in:
FongMi 2023-08-29 11:19:32 +08:00
parent ac78447018
commit 7c9697b2bd
4 changed files with 34 additions and 18 deletions

View File

@ -58,8 +58,7 @@ public class AliYun {
private final List<String> tempIds; private final List<String> tempIds;
private AlertDialog dialog; private AlertDialog dialog;
private String refreshToken; private String refreshToken;
private String shareToken; private Share share;
private String shareId;
private OAuth oauth; private OAuth oauth;
private Drive drive; private Drive drive;
private User user; private User user;
@ -112,7 +111,7 @@ public class AliYun {
private HashMap<String, String> getHeaderAuth() { private HashMap<String, String> getHeaderAuth() {
HashMap<String, String> headers = getHeader(); HashMap<String, String> headers = getHeader();
headers.put("x-share-token", shareToken); headers.put("x-share-token", share.getShareToken());
headers.put("X-Canary", "client=Android,app=adrive,version=v4.3.1"); headers.put("X-Canary", "client=Android,app=adrive,version=v4.3.1");
if (user.isAuthed()) headers.put("authorization", user.getAuthorization()); if (user.isAuthed()) headers.put("authorization", user.getAuthorization());
return headers; return headers;
@ -144,7 +143,6 @@ public class AliYun {
url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url; url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url;
OkResult result = OkHttp.postJson(url, json, getHeaderAuth()); OkResult result = OkHttp.postJson(url, json, getHeaderAuth());
SpiderDebug.log(result.getCode() + "," + url + "," + result.getBody()); SpiderDebug.log(result.getCode() + "," + url + "," + result.getBody());
if (retry && result.getBody().contains("InvalidParameterNotMatch.ShareId") && refreshShareToken()) return auth(url, json, false);
if (retry && result.getCode() == 401 && refreshAccessToken()) return auth(url, json, false); if (retry && result.getCode() == 401 && refreshAccessToken()) return auth(url, json, false);
if (retry && result.getCode() == 429) return auth(url, json, false); if (retry && result.getCode() == 429) return auth(url, json, false);
return result.getBody(); return result.getBody();
@ -170,22 +168,15 @@ public class AliYun {
return false; return false;
} }
private String check(String shareId) { private void refreshShareToken(String shareId) {
if (shareId.equals(this.shareId)) return shareId; if (share != null && share.alive(shareId)) return;
this.shareId = shareId;
refreshShareToken();
return shareId;
}
private boolean refreshShareToken() {
SpiderDebug.log("refreshShareToken..."); SpiderDebug.log("refreshShareToken...");
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("share_id", shareId); param.addProperty("share_id", shareId);
param.addProperty("share_pwd", ""); param.addProperty("share_pwd", "");
String json = post("v2/share_link/get_share_token", param); String json = post("v2/share_link/get_share_token", param);
shareToken = Share.objectFrom(json).getShareToken(); share = Share.objectFrom(json).setShareId(shareId).setTime();
if (shareToken.isEmpty()) Utils.notify("來晚啦,該分享已失效。"); if (share.getShareToken().isEmpty()) Utils.notify("來晚啦,該分享已失效。");
return shareToken.length() > 0;
} }
private boolean refreshAccessToken() { private boolean refreshAccessToken() {
@ -247,8 +238,9 @@ public class AliYun {
} }
public Vod getVod(String url, String shareId, String fileId) { public Vod getVod(String url, String shareId, String fileId) {
refreshShareToken(shareId);
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
param.addProperty("share_id", check(shareId)); param.addProperty("share_id", shareId);
Share share = Share.objectFrom(post("adrive/v3/share_link/get_share_by_anonymous", param)); Share share = Share.objectFrom(post("adrive/v3/share_link/get_share_by_anonymous", param));
List<Item> files = new ArrayList<>(); List<Item> files = new ArrayList<>();
List<Item> subs = new ArrayList<>(); List<Item> subs = new ArrayList<>();
@ -339,6 +331,7 @@ public class AliYun {
public String getDownloadUrl(String shareId, String fileId) { public String getDownloadUrl(String shareId, String fileId) {
try { try {
refreshShareToken(shareId);
SpiderDebug.log("getDownloadUrl..." + fileId); SpiderDebug.log("getDownloadUrl..." + fileId);
tempIds.add(0, copy(shareId, fileId)); tempIds.add(0, copy(shareId, fileId));
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
@ -356,6 +349,7 @@ public class AliYun {
public Preview.Info getVideoPreviewPlayInfo(String shareId, String fileId) { public Preview.Info getVideoPreviewPlayInfo(String shareId, String fileId) {
try { try {
refreshShareToken(shareId);
SpiderDebug.log("getVideoPreviewPlayInfo..." + fileId); SpiderDebug.log("getVideoPreviewPlayInfo..." + fileId);
tempIds.add(0, copy(shareId, fileId)); tempIds.add(0, copy(shareId, fileId));
JsonObject param = new JsonObject(); JsonObject param = new JsonObject();
@ -406,7 +400,7 @@ public class AliYun {
if (drive.getDriveId().isEmpty()) getDriveId(); if (drive.getDriveId().isEmpty()) getDriveId();
SpiderDebug.log("Copy..." + fileId); 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\"}"; 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, check(shareId), drive.getDriveId()); 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));
return res.getResponse().getBody().getFileId(); return res.getResponse().getBody().getFileId();
} }

View File

@ -10,6 +10,8 @@ import java.util.List;
public class Share { public class Share {
@SerializedName("share_id")
private String shareId;
@SerializedName("share_token") @SerializedName("share_token")
private String shareToken; private String shareToken;
@SerializedName("expire_time") @SerializedName("expire_time")
@ -42,10 +44,16 @@ public class Share {
@SerializedName("file_infos") @SerializedName("file_infos")
private List<Item> fileInfos; private List<Item> fileInfos;
private long time;
public static Share objectFrom(String str) { public static Share objectFrom(String str) {
return new Gson().fromJson(str, Share.class); return new Gson().fromJson(str, Share.class);
} }
public String getShareId() {
return TextUtils.isEmpty(shareId) ? "" : shareId;
}
public String getShareToken() { public String getShareToken() {
return TextUtils.isEmpty(shareToken) ? "" : shareToken; return TextUtils.isEmpty(shareToken) ? "" : shareToken;
} }
@ -105,4 +113,18 @@ public class Share {
public List<Item> getFileInfos() { public List<Item> getFileInfos() {
return fileInfos == null ? Collections.emptyList() : fileInfos; return fileInfos == null ? Collections.emptyList() : fileInfos;
} }
public Share setTime() {
this.time = System.currentTimeMillis() + 60 * 60 * 1000;
return this;
}
public Share setShareId(String shareId) {
this.shareId = shareId;
return this;
}
public boolean alive(String shareId) {
return getShareId().equals(shareId) && System.currentTimeMillis() <= time;
}
} }

Binary file not shown.

View File

@ -1 +1 @@
d6c739a2667fdb4ca2c014821641a001 a296bd2bc81da9960738fa6edd394246