This commit is contained in:
GH Action - Upstream Sync 2023-04-12 12:31:52 +00:00
commit 0bfe0f5e59
8 changed files with 156 additions and 114 deletions

View File

@ -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();
} }

View File

@ -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 save() { public OAuth clean() {
Prefers.put("aliyundrive_oauth", new Gson().toJson(this)); this.refreshToken = "";
this.accessToken = "";
return this; return this;
} }
public OAuth save() {
Prefers.put("aliyundrive_oauth", toString());
return this;
}
@NonNull
@Override
public String toString() {
return new Gson().toJson(this);
}
} }

View File

@ -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);
}
} }

View File

@ -1,12 +1,12 @@
package com.github.catvod.spider; package com.github.catvod.spider;
import android.annotation.TargetApi; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.Gravity; import android.view.Gravity;
import android.webkit.CookieManager;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
@ -17,11 +17,10 @@ import com.github.catvod.crawler.Spider;
import com.github.catvod.net.OkHttp; import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Prefers; import com.github.catvod.utils.Prefers;
import com.github.catvod.utils.QRCode; import com.github.catvod.utils.QRCode;
import com.github.catvod.utils.Utils;
import com.github.catvod.utils.Trans; import com.github.catvod.utils.Trans;
import com.github.catvod.utils.Utils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
@ -29,6 +28,7 @@ import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -39,49 +39,39 @@ import java.util.concurrent.TimeUnit;
*/ */
public class Bili extends Spider { public class Bili extends Spider {
private static final String url = "https://www.bilibili.com";
private JSONObject ext;
private ScheduledExecutorService service; private ScheduledExecutorService service;
private ImageView view; private HashMap<String, String> header;
private static boolean login; private AlertDialog dialog;
private static String finalUrl = ""; private JSONObject ext;
private static boolean FirstTime = false; private String extend;
private static String cookie = ""; private boolean login;
private void getCookie() { private String getCookie(String cookie) {
cookie = Prefers.getString("BiliCookie", ""); String cache = Prefers.getString("BiliCookie");
HashMap<String, List<String>> respHeaderMap = new HashMap<>(); if (!TextUtils.isEmpty(cache)) return cache;
OkHttp.string(url, getHeaders(), respHeaderMap); if (cookie.startsWith("http")) return OkHttp.string(cookie).replace("\n", "").trim();
if (cookie.isEmpty()) { return cookie.isEmpty() ? "buvid3=84B0395D-C9F2-C490-E92E-A09AB48FE26E71636infoc" : cookie;
for (String kk : Objects.requireNonNull(respHeaderMap.get("set-cookie"))) {
cookie += kk.split(";")[0] + ";";
}
}
} }
private static HashMap<String, String> getHeaders() { private void setHeader() {
HashMap<String, String> headers = new HashMap<>(); header.put("cookie", getCookie(ext.optString("cookie")));
headers.put("User-Agent", Utils.CHROME); header.put("Referer", "https://www.bilibili.com");
headers.put("Referer", url); header.put("User-Agent", Utils.CHROME);
if (!cookie.isEmpty()) headers.put("cookie", cookie); }
return headers;
private void fetchRule() throws Exception {
if (header.containsKey("cookie") && header.get("cookie").length() > 0) return;
if (extend.startsWith("http")) extend = OkHttp.string(extend);
ext = new JSONObject(extend);
setHeader();
} }
@Override @Override
public void init(Context context, String extend) { public void init(Context context, String extend) {
try { try {
if (extend.startsWith("http")) { this.extend = extend;
extend = OkHttp.string(extend); this.header = new HashMap<>();
} fetchRule();
ext = new JSONObject(extend);
if (ext.optString("cookie").length() > 0) {
cookie = ext.optString("cookie");
if (cookie.startsWith("http")) cookie = OkHttp.string(cookie);
} else {
getCookie();
}
String content = OkHttp.string("https://api.bilibili.com/x/web-interface/nav", getHeaders());
login = new JSONObject(content).getJSONObject("data").getBoolean("isLogin");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -89,11 +79,13 @@ public class Bili extends Spider {
@Override @Override
public String homeContent(boolean filter) throws Exception { public String homeContent(boolean filter) throws Exception {
fetchRule();
return Result.string(Class.arrayFrom(ext.getJSONArray("classes").toString()), ext.getJSONObject("filter")); return Result.string(Class.arrayFrom(ext.getJSONArray("classes").toString()), ext.getJSONObject("filter"));
} }
@Override @Override
public String homeVideoContent() throws Exception { public String homeVideoContent() throws Exception {
fetchRule();
return categoryContent("窗 白噪音", "1", true, new HashMap<>()); return categoryContent("窗 白噪音", "1", true, new HashMap<>());
} }
@ -102,7 +94,7 @@ public class Bili extends Spider {
String duration = extend.containsKey("duration") ? extend.get("duration") : "0"; String duration = extend.containsKey("duration") ? extend.get("duration") : "0";
if (extend.containsKey("tid")) tid = tid + " " + extend.get("tid"); if (extend.containsKey("tid")) tid = tid + " " + extend.get("tid");
String url = "https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword=" + URLEncoder.encode(tid) + "&duration=" + duration + "&page=" + pg; String url = "https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword=" + URLEncoder.encode(tid) + "&duration=" + duration + "&page=" + pg;
JSONObject resp = new JSONObject(OkHttp.string(url, getHeaders())); JSONObject resp = new JSONObject(OkHttp.string(url, header));
JSONArray result = resp.getJSONObject("data").getJSONArray("result"); JSONArray result = resp.getJSONObject("data").getJSONArray("result");
List<Vod> list = new ArrayList<>(); List<Vod> list = new ArrayList<>();
for (int i = 0; i < result.length(); ++i) { for (int i = 0; i < result.length(); ++i) {
@ -120,12 +112,13 @@ public class Bili extends Spider {
@Override @Override
public String detailContent(List<String> ids) throws Exception { public String detailContent(List<String> ids) throws Exception {
if (!login) checkLogin();
String bvid = ids.get(0); String bvid = ids.get(0);
String bvid2aidUrl = "https://api.bilibili.com/x/web-interface/archive/stat?bvid=" + bvid; String bvid2aidUrl = "https://api.bilibili.com/x/web-interface/archive/stat?bvid=" + bvid;
JSONObject bvid2aidResp = new JSONObject(OkHttp.string(bvid2aidUrl, getHeaders())); JSONObject bvid2aidResp = new JSONObject(OkHttp.string(bvid2aidUrl, header));
String aid = bvid2aidResp.getJSONObject("data").getLong("aid") + ""; String aid = bvid2aidResp.getJSONObject("data").getLong("aid") + "";
String detailUrl = "https://api.bilibili.com/x/web-interface/view?aid=" + aid; String detailUrl = "https://api.bilibili.com/x/web-interface/view?aid=" + aid;
JSONObject detailResp = new JSONObject(OkHttp.string(detailUrl, getHeaders())); JSONObject detailResp = new JSONObject(OkHttp.string(detailUrl, header));
JSONObject detailData = detailResp.getJSONObject("data"); JSONObject detailData = detailResp.getJSONObject("data");
List<String> playlist = new ArrayList<>(); List<String> playlist = new ArrayList<>();
JSONArray pages = detailData.getJSONArray("pages"); JSONArray pages = detailData.getJSONArray("pages");
@ -153,76 +146,86 @@ public class Bili extends Spider {
@Override @Override
public String playerContent(String flag, String id, List<String> vipFlags) throws Exception { public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
if (!login && !FirstTime) {
FirstTime = true;
checkService();
getQRCode();
}
String[] ids = id.split("\\+"); String[] ids = id.split("\\+");
String aid = ids[0]; String aid = ids[0];
String cid = ids[1]; String cid = ids[1];
String url = "https://api.bilibili.com/x/player/playurl?avid=" + aid + "&cid=" + cid + "&qn=120&fourk=1"; String url = "https://api.bilibili.com/x/player/playurl?avid=" + aid + "&cid=" + cid + "&qn=120&fourk=1";
JSONObject resp = new JSONObject(OkHttp.string(url, getHeaders())); JSONObject resp = new JSONObject(OkHttp.string(url, header));
url = resp.getJSONObject("data").getJSONArray("durl").getJSONObject(0).getString("url"); url = resp.getJSONObject("data").getJSONArray("durl").getJSONObject(0).getString("url");
return Result.get().url(url).header(getHeaders()).string(); return Result.get().url(url).header(header).string();
} }
private void checkService() { private void checkLogin() throws Exception {
if (service != null) service.shutdownNow(); login = new JSONObject(OkHttp.string("https://api.bilibili.com/x/web-interface/nav", header)).getJSONObject("data").getBoolean("isLogin");
if (view != null) Init.run(() -> Utils.removeView(view)); boolean qrCode = Prefers.getBoolean("BiliQRCode", true);
if (!login && qrCode) getQRCode();
} }
private void getQRCode() throws JSONException { private void getQRCode() {
JSONObject data = new JSONObject(OkHttp.string("https://passport.bilibili.com/x/passport-login/web/qrcode/generate?source=main-mini", null)); try {
String qrcode = data.getJSONObject("data").getString("url"); String json = OkHttp.string("https://passport.bilibili.com/x/passport-login/web/qrcode/generate?source=main-mini", null);
Init.run(() -> showCode(qrcode)); JSONObject data = new JSONObject(json).getJSONObject("data");
Init.run(() -> showQRCode(data));
} catch (Exception ignored) {
}
}
private void showQRCode(JSONObject data) {
try {
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Utils.dp2px(240), Utils.dp2px(240));
ImageView image = new ImageView(Init.context());
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setImageBitmap(QRCode.getBitmap(data.getString("url"), 240, 2));
FrameLayout frame = new FrameLayout(Init.context());
params.gravity = Gravity.CENTER;
frame.addView(image, params);
dialog = new AlertDialog.Builder(Init.getActivity()).setView(frame).setOnDismissListener(this::dismiss).show();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Init.execute(() -> startService(data));
Init.show("請使用 BiliBili App 掃描二維碼");
} catch (Exception ignored) {
}
}
private void startService(JSONObject data) {
service = Executors.newScheduledThreadPool(1); service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(() -> { service.scheduleAtFixedRate(() -> {
try { try {
String qr = data.getJSONObject("data").getString("qrcode_key"); String qr = data.getString("qrcode_key");
String url = "https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=" + qr + "&source=main_mini"; String url = "https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=" + qr + "&source=main_mini";
String content = OkHttp.string(url, getHeaders()); String json = OkHttp.string(url, header);
finalUrl = new JSONObject(content).getJSONObject("data").getString("url"); url = new JSONObject(json).getJSONObject("data").getString("url");
} catch (JSONException e) { if (!TextUtils.isEmpty(url)) setCookie(url);
e.printStackTrace(); } catch (Exception ignored) {
} }
if (!finalUrl.isEmpty()) setCookie(finalUrl);
}, 1, 1, TimeUnit.SECONDS); }, 1, 1, TimeUnit.SECONDS);
} }
private void setCookie(String value) { private void setCookie(String url) {
finalUrl = value; Map<String, List<String>> respHeader = new HashMap<>();
loadWebView(); OkHttp.stringNoRedirect(url, header, respHeader);
Init.show("请重新进入播放页"); StringBuilder sb = new StringBuilder();
checkService(); for (String value : Objects.requireNonNull(respHeader.get("set-cookie"))) sb.append(value.split(";")[0]).append(";");
Init.run(() -> Prefers.put("BiliQRCode", true), 5000);
Prefers.put("BiliCookie", sb.toString());
Init.show("請重新進入播放頁");
stopService();
} }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void stopService() {
private void loadWebView() { if (service != null) service.shutdownNow();
CookieManager cookieManager = CookieManager.getInstance(); Init.run(this::dismiss);
cookieManager.flush(); }
cookieManager.removeAllCookies(null);
cookieManager.removeSessionCookies(null); private void dismiss(DialogInterface dialog) {
HashMap<String, List<String>> respHeaderMap = new HashMap<>(); Prefers.put("BiliQRCode", false);
OkHttp.stringNoRedirect(finalUrl, getHeaders(), respHeaderMap); stopService();
cookie = ""; }
for (String kk : Objects.requireNonNull(respHeaderMap.get("set-cookie"))) {
cookie = cookie.concat(kk.split(";")[0] + ";"); private void dismiss() {
try {
if (dialog != null) dialog.dismiss();
} catch (Exception ignored) {
} }
Prefers.put("BiliCookie", cookie);
}
private void showCode(String text) {
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
Utils.addView(view = create(text), params);
Init.show("请使用 BiliBili App 扫描二维码");
}
private ImageView create(String value) {
ImageView view = new ImageView(Init.context());
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setImageBitmap(QRCode.getBitmap(value, 250, 2));
return view;
} }
} }

Binary file not shown.

View File

@ -1 +1 @@
78c395c04b5a9e444afbeec1b1681196 be4d70b1bec4f2c930972aa6685ffe19

View File

@ -1,5 +1,5 @@
{ {
"spider": "https://ghproxy.com/https://raw.githubusercontent.com/FongMi/CatVodSpider/main/jar/custom_spider.jar;md5;78c395c04b5a9e444afbeec1b1681196", "spider": "https://ghproxy.com/https://raw.githubusercontent.com/FongMi/CatVodSpider/main/jar/custom_spider.jar;md5;be4d70b1bec4f2c930972aa6685ffe19",
"wallpaper": "https://gao.chuqiuyu.tk", "wallpaper": "https://gao.chuqiuyu.tk",
"sites": [ "sites": [
{ {

View File

@ -1,5 +1,5 @@
{ {
"spider": "https://ghproxy.com/https://raw.githubusercontent.com/FongMi/CatVodSpider/main/jar/custom_spider.jar;md5;78c395c04b5a9e444afbeec1b1681196", "spider": "https://ghproxy.com/https://raw.githubusercontent.com/FongMi/CatVodSpider/main/jar/custom_spider.jar;md5;be4d70b1bec4f2c930972aa6685ffe19",
"wallpaper": "http://饭太硬.ga/深色壁纸/api.php", "wallpaper": "http://饭太硬.ga/深色壁纸/api.php",
"sites": [ "sites": [
{ {