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 ce445b65..0443070c 100644 --- a/app/src/main/java/com/github/catvod/ali/API.java +++ b/app/src/main/java/com/github/catvod/ali/API.java @@ -17,11 +17,7 @@ import com.github.catvod.BuildConfig; import com.github.catvod.bean.Result; import com.github.catvod.bean.Sub; import com.github.catvod.bean.Vod; -import com.github.catvod.bean.ali.Code; -import com.github.catvod.bean.ali.Data; -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.bean.ali.*; import com.github.catvod.crawler.SpiderDebug; import com.github.catvod.net.OkHttp; import com.github.catvod.net.OkResult; @@ -58,8 +54,10 @@ public class API { private String refreshToken; private String shareToken; private String shareId; + private String driveId; private OAuth oauth; private User user; + private Drive drive; private static class Loader { static volatile API INSTANCE = new API(); @@ -77,10 +75,15 @@ public class API { return FileUtil.getCacheFile("aliyundrive_oauth"); } + public File getDriveCache() { + return FileUtil.getCacheFile("aliyundrive_drive"); + } + private API() { tempIds = new ArrayList<>(); oauth = OAuth.objectFrom(FileUtil.read(getOAuthCache())); user = User.objectFrom(FileUtil.read(getUserCache())); + drive = Drive.objectFrom(FileUtil.read(getUserCache())); quality = new HashMap<>(); quality.put("4K", "UHD"); quality.put("2k", "QHD"); @@ -105,6 +108,7 @@ public class API { public void setShareId(String shareId) { if (!getOAuthCache().exists()) oauth.clean().save(); if (!getUserCache().exists()) user.clean().save(); + if (!getDriveCache().exists()) drive.clean().save(); this.shareId = shareId; refreshShareToken(); } @@ -153,7 +157,8 @@ public class API { url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url; OkResult result = OkHttp.postJson(url, json, getHeaderAuth()); SpiderDebug.log(result.getCode() + "," + url + "," + result.getBody()); - if (retry && (result.getCode() == 400 || result.getCode() == 401) && refreshAccessToken()) return auth(url, json, false); + if (retry && (result.getCode() == 400 || result.getCode() == 401) && refreshAccessToken()) + return auth(url, json, false); if (retry && result.getCode() == 429) return auth(url, json, false); return result.getBody(); } @@ -162,7 +167,8 @@ public class API { url = url.startsWith("https") ? url : "https://open.aliyundrive.com/adrive/v1.0/" + url; OkResult result = OkHttp.postJson(url, json, getHeaderOpen()); SpiderDebug.log(result.getCode() + "," + url + "," + result.getBody()); - if (retry && (result.getCode() == 400 || result.getCode() == 401) && refreshOpenToken()) return oauth(url, json, false); + if (retry && (result.getCode() == 400 || result.getCode() == 401) && refreshOpenToken()) + return oauth(url, json, false); return result.getBody(); } @@ -217,6 +223,20 @@ public class API { } } + private boolean getDriveId() { + try { + SpiderDebug.log("Obtain drive id..."); + String result = auth("https://user.aliyundrive.com/v2/user/get", "{}", false); + drive = Drive.objectFrom(result).save(); + driveId = drive.getResourceDriveId().isEmpty() ? drive.getDriveId() : drive.getResourceDriveId(); + return true; + } catch (Exception e) { + e.printStackTrace(); + drive.clean().save(); + return false; + } + } + private boolean oauthRequest() { try { SpiderDebug.log("OAuth Request..."); @@ -272,7 +292,8 @@ public class API { List playFrom = Arrays.asList("原畫", "超清", "高清"); List episode = new ArrayList<>(); List playUrl = new ArrayList<>(); - for (Item file : files) episode.add(file.getDisplayName() + "$" + file.getFileId() + findSubs(file.getName(), subs)); + for (Item file : files) + episode.add(file.getDisplayName() + "$" + file.getFileId() + findSubs(file.getName(), subs)); for (int i = 0; i < playFrom.size(); i++) playUrl.add(TextUtils.join("#", episode)); Vod vod = new Vod(); vod.setVodId(url); @@ -338,7 +359,8 @@ public class API { pair(Utils.removeExt(name1).toLowerCase(), items, subs); if (subs.isEmpty()) subs.addAll(items); StringBuilder sb = new StringBuilder(); - for (Item sub : subs) sb.append("+").append(Utils.removeExt(sub.getName())).append("@@@").append(sub.getExt()).append("@@@").append(sub.getFileId()); + for (Item sub : subs) + sb.append("+").append(Utils.removeExt(sub.getName())).append("@@@").append(sub.getExt()).append("@@@").append(sub.getFileId()); return sb.toString(); } @@ -358,10 +380,11 @@ public class API { public String getDownloadUrl(String fileId) { try { SpiderDebug.log("getDownloadUrl..." + fileId); + if (!getDriveId()) throw new Exception("unable obtain drive id"); tempIds.add(0, copy(fileId)); JSONObject body = new JSONObject(); body.put("file_id", tempIds.get(0)); - body.put("drive_id", user.getDriveId()); + body.put("drive_id", driveId); String json = oauth("openFile/getDownloadUrl", body.toString(), true); return new JSONObject(json).getString("url"); } catch (Exception e) { @@ -375,10 +398,11 @@ public class API { public JSONObject getVideoPreviewPlayInfo(String fileId) { try { SpiderDebug.log("getVideoPreviewPlayInfo..." + fileId); + if (!getDriveId()) throw new Exception("unable obtain drive id"); tempIds.add(0, copy(fileId)); JSONObject body = new JSONObject(); body.put("file_id", tempIds.get(0)); - body.put("drive_id", user.getDriveId()); + body.put("drive_id", driveId); body.put("category", "live_transcoding"); body.put("url_expire_sec", "14400"); String json = oauth("openFile/getVideoPreviewPlayInfo", body.toString(), true); @@ -436,7 +460,7 @@ public class API { 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\"}"; - json = String.format(json, fileId, shareId, user.getDriveId()); + json = String.format(json, fileId, shareId, driveId); String result = auth("adrive/v2/batch", json, true); if (result.contains("ForbiddenNoPermission.File")) return copy(fileId); return new JSONObject(result).getJSONArray("responses").getJSONObject(0).getJSONObject("body").getString("file_id"); @@ -454,7 +478,7 @@ public class API { try { SpiderDebug.log("Delete..." + fileId); String json = "{\"requests\":[{\"body\":{\"drive_id\":\"%s\",\"file_id\":\"%s\"},\"headers\":{\"Content-Type\":\"application/json\"},\"id\":\"%s\",\"method\":\"POST\",\"url\":\"/file/delete\"}],\"resource\":\"file\"}"; - json = String.format(json, user.getDriveId(), fileId, fileId); + json = String.format(json, driveId, fileId, fileId); String result = auth("adrive/v2/batch", json, true); return result.length() == 211; } catch (Exception ignored) { diff --git a/app/src/main/java/com/github/catvod/bean/ali/Drive.java b/app/src/main/java/com/github/catvod/bean/ali/Drive.java new file mode 100644 index 00000000..7ed6b9c6 --- /dev/null +++ b/app/src/main/java/com/github/catvod/bean/ali/Drive.java @@ -0,0 +1,64 @@ +package com.github.catvod.bean.ali; + +import android.text.TextUtils; + +import com.github.catvod.ali.API; +import com.github.catvod.utils.FileUtil; +import com.google.gson.Gson; +import com.google.gson.annotations.SerializedName; + +public class Drive { + + @SerializedName("default_drive_id") + private String driveId; + @SerializedName("user_id") + private String userId; + @SerializedName("backup_drive_id") + private String backupDriveId; + @SerializedName("resource_drive_id") + private String resourceDriveId; + @SerializedName("sbox_drive_id") + private String sboxDriveId; + + public static Drive objectFrom(String str) { + Drive item = new Gson().fromJson(str, Drive.class); + return item == null ? new Drive() : item; + } + + public String getDriveId() { + return TextUtils.isEmpty(driveId) ? "" : driveId; + } + + public String getUserId() { + return TextUtils.isEmpty(userId) ? "" : userId; + } + + public String getBackupDriveId() { + return TextUtils.isEmpty(backupDriveId) ? "" : backupDriveId; + } + + public String getResourceDriveId() { + return TextUtils.isEmpty(resourceDriveId) ? "" : resourceDriveId; + } + + public String getSboxDriveId() { + return TextUtils.isEmpty(sboxDriveId) ? "" : sboxDriveId; + } + + + public Drive clean() { + this.driveId = ""; + this.resourceDriveId = ""; + return this; + } + + public Drive save() { + FileUtil.write(API.get().getDriveCache(), toString()); + return this; + } + + @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 9b66f3f5..3897fedc 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 @@ -48,7 +48,9 @@ public class User { public void setRefreshToken(String refreshToken) { this.refreshToken = refreshToken; } - + public void setDriveId(String driveId) { + this.driveId = driveId; + } public String getAuthorization() { return getTokenType() + " " + getAccessToken(); }