diff --git a/app/src/main/java/com/github/catvod/ali/API.java b/app/src/main/java/com/github/catvod/ali/API.java index 7a06d51..df305c9 100644 --- a/app/src/main/java/com/github/catvod/ali/API.java +++ b/app/src/main/java/com/github/catvod/ali/API.java @@ -45,6 +45,7 @@ public class API { private final Map quality; private ScheduledExecutorService service; + private final List tempIds; private AlertDialog dialog; private String refreshToken; private String shareToken; @@ -61,6 +62,7 @@ public class API { } private API() { + tempIds = new ArrayList<>(); oauth = OAuth.objectFrom(Prefers.getString("aliyundrive_oauth")); user = User.objectFrom(Prefers.getString("aliyundrive_user")); quality = new HashMap<>(); @@ -140,12 +142,19 @@ public class API { return false; } - private boolean checkManyRequest(String result) { + private boolean isManyRequest(String result) { if (!result.contains("Too Many Requests")) return false; Init.show("洗洗睡吧,Too Many Requests。"); return true; } + private boolean isInvalidOpenToken(String result) { + if (!result.contains("invalid refresh_token")) return false; + oauth.clean().save(); + oauthRequest(); + return true; + } + public void checkAccessToken() { if (user.getAccessToken().isEmpty()) refreshAccessToken(); } @@ -156,13 +165,14 @@ public class API { JSONObject body = new JSONObject(); String token = user.getRefreshToken(); if (token.isEmpty()) token = refreshToken; - if (token.startsWith("http")) token = OkHttp.string(token); + if (token.startsWith("http")) token = OkHttp.string(token).trim(); body.put("refresh_token", token); body.put("grant_type", "refresh_token"); String result = post("https://auth.aliyundrive.com/v2/account/token", body); user = User.objectFrom(result).save(); + SpiderDebug.log(user.toString()); if (user.getAccessToken().isEmpty()) throw new Exception(result); - oauthRequest(); + if (oauth.getAccessToken().isEmpty()) oauthRequest(); return true; } catch (Exception e) { user.clean().save(); @@ -196,8 +206,9 @@ public class API { body.put("code", code); body.put("grant_type", "authorization_code"); String result = post("https://api.nn.ci/alist/ali_open/code", body); - if (checkManyRequest(result)) return; + if (isManyRequest(result)) return; oauth = OAuth.objectFrom(result).save(); + SpiderDebug.log(oauth.toString()); } catch (Exception e) { SpiderDebug.log(e); } @@ -210,8 +221,10 @@ public class API { body.put("grant_type", "refresh_token"); body.put("refresh_token", oauth.getRefreshToken()); String result = post("https://api.nn.ci/alist/ali_open/token", body); - if (checkManyRequest(result)) return false; + if (isManyRequest(result)) return false; + if (isInvalidOpenToken(result)) return true; oauth = OAuth.objectFrom(result).save(); + SpiderDebug.log(oauth.toString()); return true; } catch (Exception e) { SpiderDebug.log(e); @@ -331,27 +344,25 @@ public class API { } public String getDownloadUrl(String fileId) { - String tempId = null; try { - tempId = copy(fileId); + tempIds.add(0, copy(fileId)); JSONObject body = new JSONObject(); - body.put("file_id", tempId); + body.put("file_id", tempIds.get(0)); body.put("drive_id", user.getDriveId()); return new JSONObject(oauth("openFile/getDownloadUrl", body.toString(), true)).getString("url"); } catch (Exception e) { e.printStackTrace(); return ""; } finally { - if (tempId != null) delete(tempId); + deleteAll(); } } public String getPreviewUrl(String fileId, String flag) { - String tempId = null; try { - tempId = copy(fileId); + tempIds.add(0, copy(fileId)); JSONObject body = new JSONObject(); - body.put("file_id", tempId); + body.put("file_id", tempIds.get(0)); body.put("drive_id", user.getDriveId()); body.put("category", "live_transcoding"); body.put("url_expire_sec", "14400"); @@ -362,7 +373,7 @@ public class API { e.printStackTrace(); return ""; } finally { - if (tempId != null) delete(tempId); + deleteAll(); } } @@ -384,13 +395,19 @@ public class API { return new JSONObject(result).getJSONArray("responses").getJSONObject(0).getJSONObject("body").getString("file_id"); } + private void deleteAll() { + for (String tempId : tempIds) if (!TextUtils.isEmpty(tempId)) delete(tempId); + } + private void delete(String fileId) { try { SpiderDebug.log("Delete..." + fileId); JSONObject body = new JSONObject(); body.put("file_id", fileId); body.put("drive_id", user.getDriveId()); - oauth("openFile/delete", body.toString(), true); + String result = oauth("openFile/delete", body.toString(), true); + SpiderDebug.log(result + "," + result.length()); + if (result.length() == 125) tempIds.remove(fileId); } catch (Exception e) { e.printStackTrace(); } diff --git a/app/src/main/java/com/github/catvod/bean/ali/OAuth.java b/app/src/main/java/com/github/catvod/bean/ali/OAuth.java index 901728e..17712b2 100644 --- a/app/src/main/java/com/github/catvod/bean/ali/OAuth.java +++ b/app/src/main/java/com/github/catvod/bean/ali/OAuth.java @@ -2,6 +2,8 @@ package com.github.catvod.bean.ali; import android.text.TextUtils; +import androidx.annotation.NonNull; + import com.github.catvod.utils.Prefers; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; @@ -36,8 +38,20 @@ public class OAuth { return getTokenType() + " " + getAccessToken(); } + public OAuth clean() { + this.refreshToken = ""; + this.accessToken = ""; + return this; + } + public OAuth save() { - Prefers.put("aliyundrive_oauth", new Gson().toJson(this)); + Prefers.put("aliyundrive_oauth", toString()); return this; } + + @NonNull + @Override + public String toString() { + return new Gson().toJson(this); + } } diff --git a/app/src/main/java/com/github/catvod/bean/ali/User.java b/app/src/main/java/com/github/catvod/bean/ali/User.java index 1e6e935..3d2a6af 100644 --- a/app/src/main/java/com/github/catvod/bean/ali/User.java +++ b/app/src/main/java/com/github/catvod/bean/ali/User.java @@ -2,6 +2,8 @@ package com.github.catvod.bean.ali; import android.text.TextUtils; +import androidx.annotation.NonNull; + import com.github.catvod.utils.Prefers; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; @@ -55,7 +57,13 @@ public class User { } public User save() { - Prefers.put("aliyundrive_user", new Gson().toJson(this)); + Prefers.put("aliyundrive_user", toString()); return this; } + + @NonNull + @Override + public String toString() { + return new Gson().toJson(this); + } } diff --git a/jar/custom_spider.jar b/jar/custom_spider.jar index 46d678b..f425936 100644 Binary files a/jar/custom_spider.jar and b/jar/custom_spider.jar differ diff --git a/jar/custom_spider.jar.md5 b/jar/custom_spider.jar.md5 index 70f0d30..f205c02 100644 --- a/jar/custom_spider.jar.md5 +++ b/jar/custom_spider.jar.md5 @@ -1 +1 @@ -78c395c04b5a9e444afbeec1b1681196 +d482287618104e6ba6a883c9b81c449d