Optimize ali delete

pull/69/head
FongMi 3 years ago
parent e2428fce7c
commit 74fd0e4505
  1. 45
      app/src/main/java/com/github/catvod/ali/API.java
  2. 16
      app/src/main/java/com/github/catvod/bean/ali/OAuth.java
  3. 10
      app/src/main/java/com/github/catvod/bean/ali/User.java
  4. BIN
      jar/custom_spider.jar
  5. 2
      jar/custom_spider.jar.md5

@ -45,6 +45,7 @@ public class API {
private final Map<String, String> quality; private final Map<String, String> quality;
private ScheduledExecutorService service; private ScheduledExecutorService service;
private final List<String> tempIds;
private AlertDialog dialog; private AlertDialog dialog;
private String refreshToken; private String refreshToken;
private String shareToken; private String shareToken;
@ -61,6 +62,7 @@ public class API {
} }
private API() { private API() {
tempIds = new ArrayList<>();
oauth = OAuth.objectFrom(Prefers.getString("aliyundrive_oauth")); oauth = OAuth.objectFrom(Prefers.getString("aliyundrive_oauth"));
user = User.objectFrom(Prefers.getString("aliyundrive_user")); user = User.objectFrom(Prefers.getString("aliyundrive_user"));
quality = new HashMap<>(); quality = new HashMap<>();
@ -140,12 +142,19 @@ public class API {
return false; return false;
} }
private boolean checkManyRequest(String result) { private boolean isManyRequest(String result) {
if (!result.contains("Too Many Requests")) return false; if (!result.contains("Too Many Requests")) return false;
Init.show("洗洗睡吧,Too Many Requests。"); Init.show("洗洗睡吧,Too Many Requests。");
return true; return true;
} }
private boolean isInvalidOpenToken(String result) {
if (!result.contains("invalid refresh_token")) return false;
oauth.clean().save();
oauthRequest();
return true;
}
public void checkAccessToken() { public void checkAccessToken() {
if (user.getAccessToken().isEmpty()) refreshAccessToken(); if (user.getAccessToken().isEmpty()) refreshAccessToken();
} }
@ -156,13 +165,14 @@ public class API {
JSONObject body = new JSONObject(); JSONObject body = 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); if (token.startsWith("http")) token = OkHttp.string(token).trim();
body.put("refresh_token", token); body.put("refresh_token", token);
body.put("grant_type", "refresh_token"); body.put("grant_type", "refresh_token");
String result = post("https://auth.aliyundrive.com/v2/account/token", body); String result = post("https://auth.aliyundrive.com/v2/account/token", body);
user = User.objectFrom(result).save(); user = User.objectFrom(result).save();
SpiderDebug.log(user.toString());
if (user.getAccessToken().isEmpty()) throw new Exception(result); if (user.getAccessToken().isEmpty()) throw new Exception(result);
oauthRequest(); if (oauth.getAccessToken().isEmpty()) oauthRequest();
return true; return true;
} catch (Exception e) { } catch (Exception e) {
user.clean().save(); user.clean().save();
@ -196,8 +206,9 @@ public class API {
body.put("code", code); body.put("code", code);
body.put("grant_type", "authorization_code"); body.put("grant_type", "authorization_code");
String result = post("https://api.nn.ci/alist/ali_open/code", body); 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(); oauth = OAuth.objectFrom(result).save();
SpiderDebug.log(oauth.toString());
} catch (Exception e) { } catch (Exception e) {
SpiderDebug.log(e); SpiderDebug.log(e);
} }
@ -210,8 +221,10 @@ public class API {
body.put("grant_type", "refresh_token"); body.put("grant_type", "refresh_token");
body.put("refresh_token", oauth.getRefreshToken()); body.put("refresh_token", oauth.getRefreshToken());
String result = post("https://api.nn.ci/alist/ali_open/token", body); 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(); oauth = OAuth.objectFrom(result).save();
SpiderDebug.log(oauth.toString());
return true; return true;
} catch (Exception e) { } catch (Exception e) {
SpiderDebug.log(e); SpiderDebug.log(e);
@ -331,27 +344,25 @@ public class API {
} }
public String getDownloadUrl(String fileId) { public String getDownloadUrl(String fileId) {
String tempId = null;
try { try {
tempId = copy(fileId); tempIds.add(0, copy(fileId));
JSONObject body = new JSONObject(); JSONObject body = new JSONObject();
body.put("file_id", tempId); body.put("file_id", tempIds.get(0));
body.put("drive_id", user.getDriveId()); body.put("drive_id", user.getDriveId());
return new JSONObject(oauth("openFile/getDownloadUrl", body.toString(), true)).getString("url"); return new JSONObject(oauth("openFile/getDownloadUrl", body.toString(), true)).getString("url");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return ""; return "";
} finally { } finally {
if (tempId != null) delete(tempId); deleteAll();
} }
} }
public String getPreviewUrl(String fileId, String flag) { public String getPreviewUrl(String fileId, String flag) {
String tempId = null;
try { try {
tempId = copy(fileId); tempIds.add(0, copy(fileId));
JSONObject body = new JSONObject(); JSONObject body = new JSONObject();
body.put("file_id", tempId); body.put("file_id", tempIds.get(0));
body.put("drive_id", user.getDriveId()); body.put("drive_id", user.getDriveId());
body.put("category", "live_transcoding"); body.put("category", "live_transcoding");
body.put("url_expire_sec", "14400"); body.put("url_expire_sec", "14400");
@ -362,7 +373,7 @@ public class API {
e.printStackTrace(); e.printStackTrace();
return ""; return "";
} finally { } 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"); 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) { private void delete(String fileId) {
try { try {
SpiderDebug.log("Delete..." + fileId); SpiderDebug.log("Delete..." + fileId);
JSONObject body = new JSONObject(); JSONObject body = new JSONObject();
body.put("file_id", fileId); body.put("file_id", fileId);
body.put("drive_id", user.getDriveId()); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

@ -2,6 +2,8 @@ package com.github.catvod.bean.ali;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.github.catvod.utils.Prefers; import com.github.catvod.utils.Prefers;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
@ -36,8 +38,20 @@ public class OAuth {
return getTokenType() + " " + getAccessToken(); return getTokenType() + " " + getAccessToken();
} }
public OAuth clean() {
this.refreshToken = "";
this.accessToken = "";
return this;
}
public OAuth save() { public OAuth save() {
Prefers.put("aliyundrive_oauth", new Gson().toJson(this)); Prefers.put("aliyundrive_oauth", toString());
return this; return this;
} }
@NonNull
@Override
public String toString() {
return new Gson().toJson(this);
}
} }

@ -2,6 +2,8 @@ package com.github.catvod.bean.ali;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.github.catvod.utils.Prefers; import com.github.catvod.utils.Prefers;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
@ -55,7 +57,13 @@ public class User {
} }
public User save() { public User save() {
Prefers.put("aliyundrive_user", new Gson().toJson(this)); Prefers.put("aliyundrive_user", toString());
return this; return this;
} }
@NonNull
@Override
public String toString() {
return new Gson().toJson(this);
}
} }

Binary file not shown.

@ -1 +1 @@
78c395c04b5a9e444afbeec1b1681196 d482287618104e6ba6a883c9b81c449d

Loading…
Cancel
Save