|
|
|
|
@ -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<Item> files = new ArrayList<>(); |
|
|
|
|
LinkedHashMap<String, List<String>> subMap = new LinkedHashMap<>(); |
|
|
|
|
listFiles(new Item(getParentFileId(fileId, object)), files, subMap); |
|
|
|
|
|