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 0fc252a0..172809ac 100644 --- a/app/src/main/java/com/github/catvod/ali/API.java +++ b/app/src/main/java/com/github/catvod/ali/API.java @@ -23,6 +23,7 @@ import com.github.catvod.bean.ali.Sorter; import com.github.catvod.bean.ali.User; import com.github.catvod.crawler.SpiderDebug; import com.github.catvod.net.OkHttp; +import com.github.catvod.net.OkResult; import com.github.catvod.spider.Init; import com.github.catvod.spider.Proxy; import com.github.catvod.utils.Prefers; @@ -115,42 +116,31 @@ public class API { return headers; } + private String alist(String url, JSONObject body) throws Exception { + url = "https://api.nn.ci/alist/ali_open/" + url; + OkResult result = OkHttp.postJson(url, body.toString(), getHeader()); + if (isManyRequest(result.getBody())) return ""; + if (result.getCode() == 200) return result.getBody(); + throw new Exception(result.getBody()); + } + private String post(String url, JSONObject body) { url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url; - return OkHttp.postJson(url, body.toString(), getHeader()); + return OkHttp.postJson(url, body.toString(), getHeader()).getBody(); } private String auth(String url, String json, boolean retry) { url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url; - String result = OkHttp.postJson(url, json, getHeaderAuth()); - if (retry && checkAuth(result)) return auth(url, json, false); - return result; + OkResult result = OkHttp.postJson(url, json, getHeaderAuth()); + if (retry && result.getCode() != 200 && refreshAccessToken()) return auth(url, json, false); + return result.getBody(); } private String oauth(String url, String json, boolean retry) { url = url.startsWith("https") ? url : "https://open.aliyundrive.com/adrive/v1.0/" + url; - String result = OkHttp.postJson(url, json, getHeaderOpen()); - if (retry && checkOpen(result)) return oauth(url, json, false); - return result; - } - - private boolean checkAuth(String result) { - if (result.contains("AccessTokenInvalid")) return refreshAccessToken(); - if (result.contains("ShareLinkTokenInvalid") || result.contains("InvalidParameterNotMatch")) return refreshShareToken(); - return checkQuotaExhausted(result); - } - - private boolean checkOpen(String result) { - if (result.contains("AccessTokenInvalid")) return refreshOpenToken(); - return false; - } - - private boolean checkQuotaExhausted(String result) { - if (!result.contains("QuotaExhausted")) return false; - Init.show("容量不夠拉。"); - user.clean().save(); - refreshAccessToken(); - return false; + OkResult result = OkHttp.postJson(url, json, getHeaderOpen()); + if (retry && result.getCode() != 200 && refreshOpenToken()) return oauth(url, json, false); + return result.getBody(); } private boolean isManyRequest(String result) { @@ -161,13 +151,6 @@ public class API { 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(); } @@ -190,7 +173,6 @@ public class API { } catch (Exception e) { e.printStackTrace(); user.clean().save(); - SpiderDebug.log(e); stopService(); getQRCode(); return true; @@ -209,7 +191,7 @@ public class API { String result = auth(url, body.toString(), true); oauthRedirect(Code.objectFrom(result).getCode()); } catch (Exception e) { - SpiderDebug.log(e); + e.printStackTrace(); } } @@ -219,12 +201,10 @@ public class API { JSONObject body = new JSONObject(); body.put("code", code); body.put("grant_type", "authorization_code"); - String result = post("https://api.nn.ci/alist/ali_open/code", body); - if (isManyRequest(result)) return; - oauth = OAuth.objectFrom(result).save(); - SpiderDebug.log(oauth.toString()); + oauth = OAuth.objectFrom(alist("code", body)).save(); } catch (Exception e) { - SpiderDebug.log(e); + e.printStackTrace(); + oauth.clean().save(); } } @@ -234,39 +214,35 @@ public class API { JSONObject body = new JSONObject(); 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 (isManyRequest(result)) return false; - if (isInvalidOpenToken(result)) return true; - oauth = OAuth.objectFrom(result).save(); - SpiderDebug.log(oauth.toString()); + oauth = OAuth.objectFrom(alist("token", body)).save(); return true; } catch (Exception e) { - SpiderDebug.log(e); - return false; + e.printStackTrace(); + oauth.clean().save(); + oauthRequest(); + return true; } } - public boolean refreshShareToken() { + private void refreshShareToken() { try { SpiderDebug.log("refreshShareToken..."); JSONObject body = new JSONObject(); body.put("share_id", shareId); body.put("share_pwd", ""); - JSONObject object = new JSONObject(post("v2/share_link/get_share_token", body)); - shareToken = object.getString("share_token"); - return true; + String result = post("v2/share_link/get_share_token", body); + shareToken = new JSONObject(result).getString("share_token"); } catch (Exception e) { - Init.show("來晚啦,該分享已失效。"); e.printStackTrace(); - return false; + Init.show("來晚啦,該分享已失效。"); } } public Vod getVod(String url, String fileId) throws Exception { JSONObject body = new JSONObject(); body.put("share_id", shareId); - String json = post("adrive/v3/share_link/get_share_by_anonymous", body); - JSONObject object = new JSONObject(json); + String result = post("adrive/v3/share_link/get_share_by_anonymous", body); + JSONObject object = new JSONObject(result); List files = new ArrayList<>(); LinkedHashMap> subMap = new LinkedHashMap<>(); listFiles(new Item(getParentFileId(fileId, object)), files, subMap); diff --git a/app/src/main/java/com/github/catvod/net/Cronet.java b/app/src/main/java/com/github/catvod/net/Cronet.java index faade1c3..d13f471c 100644 --- a/app/src/main/java/com/github/catvod/net/Cronet.java +++ b/app/src/main/java/com/github/catvod/net/Cronet.java @@ -68,7 +68,7 @@ public class Cronet { } public static String string(OkHttpClient client, String method, String url, String tag, Map params, Map header, Map> respHeader) { - return new OkRequest(method, url, params, header, respHeader).tag(tag).execute(client); + return new OkRequest(method, url, params, header, respHeader).tag(tag).execute(client).getBody(); } public static String string(String url) { @@ -107,11 +107,11 @@ public class Cronet { return string(client(), POST, url, null, params, header, respHeader); } - public static String postJson(String url, String json) { + public static OkResult postJson(String url, String json) { return postJson(url, json, null); } - public static String postJson(String url, String json, Map header) { + public static OkResult postJson(String url, String json, Map header) { return new OkRequest(POST, url, json, header).execute(client()); } diff --git a/app/src/main/java/com/github/catvod/net/OkHttp.java b/app/src/main/java/com/github/catvod/net/OkHttp.java index f4feeac0..970a4273 100644 --- a/app/src/main/java/com/github/catvod/net/OkHttp.java +++ b/app/src/main/java/com/github/catvod/net/OkHttp.java @@ -64,7 +64,7 @@ public class OkHttp { } public static String string(OkHttpClient client, String method, String url, String tag, Map params, Map header, Map> respHeader) { - return new OkRequest(method, url, params, header, respHeader).tag(tag).execute(client); + return new OkRequest(method, url, params, header, respHeader).tag(tag).execute(client).getBody(); } public static String string(String url) { @@ -103,11 +103,11 @@ public class OkHttp { return string(client(), POST, url, null, params, header, respHeader); } - public static String postJson(String url, String json) { + public static OkResult postJson(String url, String json) { return postJson(url, json, null); } - public static String postJson(String url, String json, Map header) { + public static OkResult postJson(String url, String json, Map header) { return new OkRequest(POST, url, json, header).execute(client()); } diff --git a/app/src/main/java/com/github/catvod/net/OkRequest.java b/app/src/main/java/com/github/catvod/net/OkRequest.java index c1ddf792..4e080443 100644 --- a/app/src/main/java/com/github/catvod/net/OkRequest.java +++ b/app/src/main/java/com/github/catvod/net/OkRequest.java @@ -71,14 +71,14 @@ class OkRequest { url = Utils.substring(url); } - public String execute(OkHttpClient client) { + public OkResult execute(OkHttpClient client) { try { Response response = client.newCall(request).execute(); if (respHeader != null) respHeader.clear(); if (respHeader != null) respHeader.putAll(response.headers().toMultimap()); - return response.body().string(); + return new OkResult(response.code(), response.body().string()); } catch (IOException e) { - return ""; + return new OkResult(); } } } diff --git a/app/src/main/java/com/github/catvod/net/OkResult.java b/app/src/main/java/com/github/catvod/net/OkResult.java new file mode 100644 index 00000000..8e26a5af --- /dev/null +++ b/app/src/main/java/com/github/catvod/net/OkResult.java @@ -0,0 +1,27 @@ +package com.github.catvod.net; + +import android.text.TextUtils; + +public class OkResult { + + private final int code; + private final String body; + + public OkResult() { + this.code = 500; + this.body = ""; + } + + public OkResult(int code, String body) { + this.code = code; + this.body = body; + } + + public int getCode() { + return code; + } + + public String getBody() { + return TextUtils.isEmpty(body) ? "" : body; + } +} diff --git a/app/src/main/java/com/github/catvod/spider/AList.java b/app/src/main/java/com/github/catvod/spider/AList.java index 961c61b3..9bcf917a 100644 --- a/app/src/main/java/com/github/catvod/spider/AList.java +++ b/app/src/main/java/com/github/catvod/spider/AList.java @@ -140,7 +140,7 @@ public class AList extends Spider { JSONObject params = new JSONObject(); params.put("password", drive.getPassword()); params.put("path", path.startsWith(drive.getPath()) ? path : drive.getPath() + path); - String response = OkHttp.postJson(drive.getApi(), params.toString()); + String response = OkHttp.postJson(drive.getApi(), params.toString()).getBody(); return Item.objectFrom(getDetailJson(drive.isNew(), response)); } catch (Exception e) { return new Item(); @@ -155,7 +155,7 @@ public class AList extends Spider { JSONObject params = new JSONObject(); params.put("password", drive.getPassword()); params.put("path", path.startsWith(drive.getPath()) ? path : drive.getPath() + path); - String response = OkHttp.postJson(drive.listApi(), params.toString()); + String response = OkHttp.postJson(drive.listApi(), params.toString()).getBody(); List items = Item.arrayFrom(getListJson(drive.isNew(), response)); Iterator iterator = items.iterator(); if (filter) while (iterator.hasNext()) if (iterator.next().ignore(drive.isNew())) iterator.remove(); @@ -167,7 +167,7 @@ public class AList extends Spider { private void search(CountDownLatch cd, List list, Drive drive, String keyword) { try { - String response = OkHttp.postJson(drive.searchApi(), drive.params(keyword)); + String response = OkHttp.postJson(drive.searchApi(), drive.params(keyword)).getBody(); List items = Item.arrayFrom(getSearchJson(drive.isNew(), response)); for (Item item : items) if (!item.ignore(drive.isNew())) list.add(item.getVod(drive, vodPic)); } catch (Exception ignored) { diff --git a/jar/custom_spider.jar b/jar/custom_spider.jar index dbd75b01..06032ed9 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 8eb48076..e2c2f910 100644 --- a/jar/custom_spider.jar.md5 +++ b/jar/custom_spider.jar.md5 @@ -1 +1 @@ -73d83409ac27b38de28c7eb6ddcc1c0f +1cad1d2b01a603e950109d009a884da0