Update ali

This commit is contained in:
FongMi 2023-04-06 14:52:02 +08:00
parent 99c562afa9
commit 25526b38a6
8 changed files with 175 additions and 166 deletions

View File

@ -6,7 +6,6 @@ import android.graphics.Color;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.os.SystemClock; import android.os.SystemClock;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
@ -14,9 +13,11 @@ import android.widget.ImageView;
import com.github.catvod.BuildConfig; import com.github.catvod.BuildConfig;
import com.github.catvod.bean.Sub; import com.github.catvod.bean.Sub;
import com.github.catvod.bean.Vod; import com.github.catvod.bean.Vod;
import com.github.catvod.bean.ali.Auth; import com.github.catvod.bean.ali.Code;
import com.github.catvod.bean.ali.Data; import com.github.catvod.bean.ali.Data;
import com.github.catvod.bean.ali.Item; import com.github.catvod.bean.ali.Item;
import com.github.catvod.bean.ali.OAuth;
import com.github.catvod.bean.ali.User;
import com.github.catvod.crawler.SpiderDebug; import com.github.catvod.crawler.SpiderDebug;
import com.github.catvod.net.OkHttp; import com.github.catvod.net.OkHttp;
import com.github.catvod.spider.Init; import com.github.catvod.spider.Init;
@ -30,15 +31,12 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -48,9 +46,11 @@ public class API {
private final Map<String, String> quality; private final Map<String, String> quality;
private ScheduledExecutorService service; private ScheduledExecutorService service;
private AlertDialog dialog; private AlertDialog dialog;
private String refreshToken;
private String shareToken; private String shareToken;
private final Auth auth;
private String shareId; private String shareId;
private OAuth oauth;
private User user;
private static class Loader { private static class Loader {
static volatile API INSTANCE = new API(); static volatile API INSTANCE = new API();
@ -61,7 +61,8 @@ public class API {
} }
private API() { private API() {
auth = Auth.objectFrom(Prefers.getString("aliyundrive")); oauth = OAuth.objectFrom(Prefers.getString("aliyundrive_oauth"));
user = User.objectFrom(Prefers.getString("aliyundrive_user"));
quality = new HashMap<>(); quality = new HashMap<>();
quality.put("4K", "UHD"); quality.put("4K", "UHD");
quality.put("2k", "QHD"); quality.put("2k", "QHD");
@ -72,20 +73,13 @@ public class API {
} }
public void setRefreshToken(String token) { public void setRefreshToken(String token) {
try { this.refreshToken = token;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault());
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
long expireTime = sdf.parse(auth.getExpireTime()).getTime();
boolean expired = expireTime < System.currentTimeMillis();
if (expired) auth.setRefreshToken(token);
} catch (Exception e) {
auth.setRefreshToken(token);
}
} }
public void setShareId(String shareId) { public void setShareId(String shareId) {
this.shareId = shareId; this.shareId = shareId;
refreshShareToken(); refreshShareToken();
checkAccessToken();
} }
public HashMap<String, String> getHeader() { public HashMap<String, String> getHeader() {
@ -97,15 +91,14 @@ public class API {
private HashMap<String, String> getHeaderAuth() { private HashMap<String, String> getHeaderAuth() {
HashMap<String, String> headers = getHeader(); HashMap<String, String> headers = getHeader();
headers.put("authorization", auth.getAccessToken()); headers.put("authorization", user.getAuthorization());
headers.put("x-canary", "client=web,app=share,version=v2.3.1");
headers.put("x-share-token", shareToken); headers.put("x-share-token", shareToken);
return headers; return headers;
} }
private HashMap<String, String> getHeaderOpen() { private HashMap<String, String> getHeaderOpen() {
HashMap<String, String> headers = getHeader(); HashMap<String, String> headers = getHeader();
headers.put("authorization", auth.getAccessTokenOpen()); headers.put("authorization", oauth.getAuthorization());
return headers; return headers;
} }
@ -114,14 +107,9 @@ public class API {
return OkHttp.postJson(url, body.toString(), getHeader()); return OkHttp.postJson(url, body.toString(), getHeader());
} }
private String auth(String url, JSONObject body, boolean retry) {
return auth(url, body.toString(), retry);
}
private String auth(String url, String json, boolean retry) { private String auth(String url, String json, boolean retry) {
url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url; url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url;
String result = OkHttp.postJson(url, json, getHeaderAuth()); String result = OkHttp.postJson(url, json, getHeaderAuth());
Log.e("auth", result);
if (retry && checkAuth(result)) return auth(url, json, false); if (retry && checkAuth(result)) return auth(url, json, false);
return result; return result;
} }
@ -129,7 +117,6 @@ public class API {
private String oauth(String url, String json, boolean retry) { private String oauth(String url, String json, boolean retry) {
url = url.startsWith("https") ? url : "https://open.aliyundrive.com/adrive/v1.0/" + url; url = url.startsWith("https") ? url : "https://open.aliyundrive.com/adrive/v1.0/" + url;
String result = OkHttp.postJson(url, json, getHeaderOpen()); String result = OkHttp.postJson(url, json, getHeaderOpen());
Log.e("oauth", result);
if (retry && checkOpen(result)) return oauth(url, json, false); if (retry && checkOpen(result)) return oauth(url, json, false);
return result; return result;
} }
@ -148,7 +135,7 @@ public class API {
private boolean checkQuotaExhausted(String result) { private boolean checkQuotaExhausted(String result) {
if (!result.contains("QuotaExhausted")) return false; if (!result.contains("QuotaExhausted")) return false;
Init.show("容量不夠拉。"); Init.show("容量不夠拉。");
auth.setRefreshToken(""); user.clean().save();
refreshAccessToken(); refreshAccessToken();
return false; return false;
} }
@ -160,44 +147,46 @@ public class API {
} }
public void checkAccessToken() { public void checkAccessToken() {
if (auth.getAccessToken().isEmpty()) refreshAccessToken(); if (user.getAccessToken().isEmpty()) refreshAccessToken();
} }
private boolean refreshAccessToken() { private boolean refreshAccessToken() {
try { try {
SpiderDebug.log("refreshAccessToken..."); SpiderDebug.log("refreshAccessToken...");
JSONObject body = new JSONObject(); JSONObject body = new JSONObject();
String token = auth.getRefreshToken(); 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);
body.put("refresh_token", token); body.put("refresh_token", token);
body.put("grant_type", "refresh_token"); body.put("grant_type", "refresh_token");
JSONObject object = new JSONObject(post("https://auth.aliyundrive.com/v2/account/token", body)); String result = post("https://auth.aliyundrive.com/v2/account/token", body);
Log.e("DDD", object.toString()); user = User.objectFrom(result).save();
auth.setUserId(object.getString("user_id")); if (user.getAccessToken().isEmpty()) throw new Exception(result);
auth.setDriveId(object.getString("default_drive_id")); if (oauth.getRefreshToken().isEmpty()) oauthRequest();
auth.setRefreshToken(object.getString("refresh_token"));
auth.setAccessToken(object.getString("token_type") + " " + object.getString("access_token"));
oauthRequest();
return true; return true;
} catch (Exception e) { } catch (Exception e) {
user.clean().save();
SpiderDebug.log(e); SpiderDebug.log(e);
stopService(); stopService();
auth.clean();
getQRCode(); getQRCode();
return true; return true;
} finally { } finally {
while (auth.isEmpty()) SystemClock.sleep(250); while (user.getAccessToken().isEmpty()) SystemClock.sleep(250);
} }
} }
private void oauthRequest() throws Exception { private void oauthRequest() {
try {
SpiderDebug.log("OAuth Request..."); SpiderDebug.log("OAuth Request...");
JSONObject body = new JSONObject(); JSONObject body = new JSONObject();
body.put("authorize", 1); body.put("authorize", 1);
body.put("scope", "user:base,file:all:read,file:all:write"); body.put("scope", "user:base,file:all:read,file:all:write");
JSONObject object = new JSONObject(auth("https://open.aliyundrive.com/oauth/users/authorize?client_id=" + BuildConfig.CLIENT_ID + "&redirect_uri=https://alist.nn.ci/tool/aliyundrive/callback&scope=user:base,file:all:read,file:all:write&state=", body, false)); String url = "https://open.aliyundrive.com/oauth/users/authorize?client_id=" + BuildConfig.CLIENT_ID + "&redirect_uri=https://alist.nn.ci/tool/aliyundrive/callback&scope=user:base,file:all:read,file:all:write&state=";
Log.e("DDD", object.toString()); String result = auth(url, body.toString(), true);
oauthRedirect(object.getString("redirectUri").split("code=")[1]); oauthRedirect(Code.objectFrom(result).getCode());
} catch (Exception e) {
SpiderDebug.log(e);
}
} }
private void oauthRedirect(String code) { private void oauthRedirect(String code) {
@ -207,12 +196,8 @@ 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);
Log.e("DDD", result);
if (checkManyRequest(result)) return; if (checkManyRequest(result)) return;
JSONObject object = new JSONObject(result); oauth = OAuth.objectFrom(result).save();
auth.setRefreshTokenOpen(object.getString("refresh_token"));
auth.setAccessTokenOpen(object.optString("token_type") + " " + object.optString("access_token"));
auth.save();
} catch (Exception e) { } catch (Exception e) {
SpiderDebug.log(e); SpiderDebug.log(e);
} }
@ -220,17 +205,13 @@ public class API {
private boolean refreshOpenToken() { private boolean refreshOpenToken() {
try { try {
SpiderDebug.log("refreshAccessTokenOpen..."); SpiderDebug.log("refreshOpenToken...");
JSONObject body = new JSONObject(); JSONObject body = new JSONObject();
body.put("grant_type", "refresh_token"); body.put("grant_type", "refresh_token");
body.put("refresh_token", auth.getRefreshTokenOpen()); 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);
Log.e("DDD", result);
if (checkManyRequest(result)) return false; if (checkManyRequest(result)) return false;
JSONObject object = new JSONObject(result); oauth = OAuth.objectFrom(result).save();
auth.setRefreshTokenOpen(object.optString("refresh_token"));
auth.setAccessTokenOpen(object.optString("token_type") + " " + object.optString("access_token"));
auth.save();
return true; return true;
} catch (Exception e) { } catch (Exception e) {
SpiderDebug.log(e); SpiderDebug.log(e);
@ -291,7 +272,7 @@ public class API {
body.put("order_by", "name"); body.put("order_by", "name");
body.put("order_direction", "ASC"); body.put("order_direction", "ASC");
if (marker.length() > 0) body.put("marker", marker); if (marker.length() > 0) body.put("marker", marker);
Item item = Item.objectFrom(auth("adrive/v3/file/list", body, true)); Item item = Item.objectFrom(auth("adrive/v3/file/list", body.toString(), true));
for (Item file : item.getItems()) { for (Item file : item.getItems()) {
if (file.getType().equals("folder")) { if (file.getType().equals("folder")) {
folders.add(file); folders.add(file);
@ -355,7 +336,7 @@ public class API {
tempId = copy(fileId); tempId = copy(fileId);
JSONObject body = new JSONObject(); JSONObject body = new JSONObject();
body.put("file_id", tempId); body.put("file_id", tempId);
body.put("drive_id", auth.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();
@ -371,7 +352,7 @@ public class API {
tempId = copy(fileId); tempId = copy(fileId);
JSONObject body = new JSONObject(); JSONObject body = new JSONObject();
body.put("file_id", tempId); body.put("file_id", tempId);
body.put("drive_id", auth.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");
String json = oauth("openFile/getVideoPreviewPlayInfo", body.toString(), true); String json = oauth("openFile/getVideoPreviewPlayInfo", body.toString(), true);
@ -396,17 +377,19 @@ public class API {
} }
private String copy(String fileId) throws Exception { private String copy(String fileId) throws Exception {
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, shareId, auth.getDriveId()); json = String.format(json, fileId, shareId, user.getDriveId());
String result = auth("adrive/v2/batch", json, true); String result = auth("adrive/v2/batch", json, true);
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 delete(String fileId) { private void delete(String fileId) {
try { try {
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", auth.getDriveId()); body.put("drive_id", user.getDriveId());
oauth("openFile/delete", body.toString(), true); oauth("openFile/delete", body.toString(), true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -455,7 +438,7 @@ public class API {
private void setToken(String value) { private void setToken(String value) {
Init.show("請重新進入播放頁"); Init.show("請重新進入播放頁");
auth.setRefreshToken(value); this.refreshToken = value;
refreshAccessToken(); refreshAccessToken();
stopService(); stopService();
} }

View File

@ -1,101 +0,0 @@
package com.github.catvod.bean.ali;
import android.text.TextUtils;
import com.github.catvod.utils.Prefers;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
public class Auth {
@SerializedName("refreshToken")
private String refreshToken;
@SerializedName("refreshTokenOpen")
private String refreshTokenOpen;
@SerializedName("accessToken")
private String accessToken;
@SerializedName("accessTokenOpen")
private String accessTokenOpen;
@SerializedName("userId")
private String userId;
@SerializedName("driveId")
private String driveId;
@SerializedName("expire_time")
private String expireTime;
public static Auth objectFrom(String str) {
Auth item = new Gson().fromJson(str, Auth.class);
return item == null ? new Auth() : item;
}
public String getRefreshToken() {
return TextUtils.isEmpty(refreshToken) ? "" : refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
public String getRefreshTokenOpen() {
return TextUtils.isEmpty(refreshTokenOpen) ? "" : refreshTokenOpen;
}
public void setRefreshTokenOpen(String refreshTokenOpen) {
this.refreshTokenOpen = refreshTokenOpen;
}
public String getAccessToken() {
return TextUtils.isEmpty(accessToken) ? "" : accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getAccessTokenOpen() {
return TextUtils.isEmpty(accessTokenOpen) ? "" : accessTokenOpen;
}
public void setAccessTokenOpen(String accessTokenOpen) {
this.accessTokenOpen = accessTokenOpen;
}
public String getDriveId() {
return TextUtils.isEmpty(driveId) ? "" : driveId;
}
public void setDriveId(String driveId) {
this.driveId = driveId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getExpireTime() {
return TextUtils.isEmpty(expireTime) ? "" : expireTime;
}
public void setExpireTime(String expireTime) {
this.expireTime = expireTime;
}
public boolean isEmpty() {
return getAccessToken().isEmpty();
}
public void clean() {
setRefreshToken("");
setAccessToken("");
setExpireTime("");
save();
}
public void save() {
Prefers.put("aliyundrive", new Gson().toJson(this));
}
}

View File

@ -0,0 +1,24 @@
package com.github.catvod.bean.ali;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
public class Code {
@SerializedName("redirectUri")
private String redirectUri;
public static Code objectFrom(String str) {
return new Gson().fromJson(str, Code.class);
}
public String getRedirectUri() {
return TextUtils.isEmpty(redirectUri) ? "" : redirectUri;
}
public String getCode() {
return getRedirectUri().split("code=")[1];
}
}

View File

@ -0,0 +1,43 @@
package com.github.catvod.bean.ali;
import android.text.TextUtils;
import com.github.catvod.utils.Prefers;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
public class OAuth {
@SerializedName("token_type")
private String tokenType;
@SerializedName("access_token")
private String accessToken;
@SerializedName("refresh_token")
private String refreshToken;
public static OAuth objectFrom(String str) {
OAuth item = new Gson().fromJson(str, OAuth.class);
return item == null ? new OAuth() : item;
}
public String getTokenType() {
return TextUtils.isEmpty(tokenType) ? "" : tokenType;
}
public String getAccessToken() {
return TextUtils.isEmpty(accessToken) ? "" : accessToken;
}
public String getRefreshToken() {
return TextUtils.isEmpty(refreshToken) ? "" : refreshToken;
}
public String getAuthorization() {
return getTokenType() + " " + getAccessToken();
}
public OAuth save() {
Prefers.put("aliyundrive_oauth", new Gson().toJson(this));
return this;
}
}

View File

@ -0,0 +1,61 @@
package com.github.catvod.bean.ali;
import android.text.TextUtils;
import com.github.catvod.utils.Prefers;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
public class User {
@SerializedName("default_drive_id")
private String driveId;
@SerializedName("user_id")
private String userId;
@SerializedName("token_type")
private String tokenType;
@SerializedName("access_token")
private String accessToken;
@SerializedName("refresh_token")
private String refreshToken;
public static User objectFrom(String str) {
User item = new Gson().fromJson(str, User.class);
return item == null ? new User() : item;
}
public String getDriveId() {
return TextUtils.isEmpty(driveId) ? "" : driveId;
}
public String getUserId() {
return TextUtils.isEmpty(userId) ? "" : userId;
}
public String getTokenType() {
return TextUtils.isEmpty(tokenType) ? "" : tokenType;
}
public String getAccessToken() {
return TextUtils.isEmpty(accessToken) ? "" : accessToken;
}
public String getRefreshToken() {
return TextUtils.isEmpty(refreshToken) ? "" : refreshToken;
}
public String getAuthorization() {
return getTokenType() + " " + getAccessToken();
}
public User clean() {
this.refreshToken = "";
this.accessToken = "";
return this;
}
public User save() {
Prefers.put("aliyundrive_user", new Gson().toJson(this));
return this;
}
}

View File

@ -36,7 +36,6 @@ public class Ali extends Spider {
@Override @Override
public String playerContent(String flag, String id, List<String> vipFlags) { public String playerContent(String flag, String id, List<String> vipFlags) {
API.get().checkAccessToken();
String[] ids = id.split("\\+"); String[] ids = id.split("\\+");
String url = flag.equals("原畫") ? API.get().getDownloadUrl(ids[0]) : API.get().getPreviewUrl(ids[0], flag); String url = flag.equals("原畫") ? API.get().getDownloadUrl(ids[0]) : API.get().getPreviewUrl(ids[0], flag);
return Result.get().url(url).subs(API.get().getSub(ids)).header(API.get().getHeader()).parse(0).string(); return Result.get().url(url).subs(API.get().getSub(ids)).header(API.get().getHeader()).parse(0).string();

Binary file not shown.

View File

@ -1 +1 @@
9dad9b968f26436fc4e681d0dc372d55 b43f60b1459104200b5f30007df2b306