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 7a06d513..df305c9d 100644 --- a/app/src/main/java/com/github/catvod/ali/API.java +++ b/app/src/main/java/com/github/catvod/ali/API.java @@ -45,6 +45,7 @@ public class API { private final Map quality; private ScheduledExecutorService service; + private final List tempIds; private AlertDialog dialog; private String refreshToken; private String shareToken; @@ -61,6 +62,7 @@ public class API { } private API() { + tempIds = new ArrayList<>(); oauth = OAuth.objectFrom(Prefers.getString("aliyundrive_oauth")); user = User.objectFrom(Prefers.getString("aliyundrive_user")); quality = new HashMap<>(); @@ -140,12 +142,19 @@ public class API { return false; } - private boolean checkManyRequest(String result) { + private boolean isManyRequest(String result) { if (!result.contains("Too Many Requests")) return false; Init.show("洗洗睡吧,Too Many Requests。"); 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(); } @@ -156,13 +165,14 @@ public class API { JSONObject body = new JSONObject(); 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).trim(); body.put("refresh_token", token); body.put("grant_type", "refresh_token"); String result = post("https://auth.aliyundrive.com/v2/account/token", body); user = User.objectFrom(result).save(); + SpiderDebug.log(user.toString()); if (user.getAccessToken().isEmpty()) throw new Exception(result); - oauthRequest(); + if (oauth.getAccessToken().isEmpty()) oauthRequest(); return true; } catch (Exception e) { user.clean().save(); @@ -196,8 +206,9 @@ public class API { body.put("code", code); body.put("grant_type", "authorization_code"); 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(); + SpiderDebug.log(oauth.toString()); } catch (Exception e) { SpiderDebug.log(e); } @@ -210,8 +221,10 @@ public class API { 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 (checkManyRequest(result)) return false; + if (isManyRequest(result)) return false; + if (isInvalidOpenToken(result)) return true; oauth = OAuth.objectFrom(result).save(); + SpiderDebug.log(oauth.toString()); return true; } catch (Exception e) { SpiderDebug.log(e); @@ -331,27 +344,25 @@ public class API { } public String getDownloadUrl(String fileId) { - String tempId = null; try { - tempId = copy(fileId); + tempIds.add(0, copy(fileId)); JSONObject body = new JSONObject(); - body.put("file_id", tempId); + body.put("file_id", tempIds.get(0)); body.put("drive_id", user.getDriveId()); return new JSONObject(oauth("openFile/getDownloadUrl", body.toString(), true)).getString("url"); } catch (Exception e) { e.printStackTrace(); return ""; } finally { - if (tempId != null) delete(tempId); + deleteAll(); } } public String getPreviewUrl(String fileId, String flag) { - String tempId = null; try { - tempId = copy(fileId); + tempIds.add(0, copy(fileId)); JSONObject body = new JSONObject(); - body.put("file_id", tempId); + body.put("file_id", tempIds.get(0)); body.put("drive_id", user.getDriveId()); body.put("category", "live_transcoding"); body.put("url_expire_sec", "14400"); @@ -362,7 +373,7 @@ public class API { e.printStackTrace(); return ""; } 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"); } + private void deleteAll() { + for (String tempId : tempIds) if (!TextUtils.isEmpty(tempId)) delete(tempId); + } + private void delete(String fileId) { try { SpiderDebug.log("Delete..." + fileId); JSONObject body = new JSONObject(); body.put("file_id", fileId); 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) { e.printStackTrace(); } diff --git a/app/src/main/java/com/github/catvod/bean/ali/OAuth.java b/app/src/main/java/com/github/catvod/bean/ali/OAuth.java index 901728ed..17712b27 100644 --- a/app/src/main/java/com/github/catvod/bean/ali/OAuth.java +++ b/app/src/main/java/com/github/catvod/bean/ali/OAuth.java @@ -2,6 +2,8 @@ package com.github.catvod.bean.ali; import android.text.TextUtils; +import androidx.annotation.NonNull; + import com.github.catvod.utils.Prefers; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; @@ -36,8 +38,20 @@ public class OAuth { return getTokenType() + " " + getAccessToken(); } - public OAuth save() { - Prefers.put("aliyundrive_oauth", new Gson().toJson(this)); + public OAuth clean() { + this.refreshToken = ""; + this.accessToken = ""; return this; } + + public OAuth save() { + Prefers.put("aliyundrive_oauth", toString()); + return this; + } + + @NonNull + @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 1e6e935b..3d2a6afd 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 @@ -2,6 +2,8 @@ package com.github.catvod.bean.ali; import android.text.TextUtils; +import androidx.annotation.NonNull; + import com.github.catvod.utils.Prefers; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; @@ -55,7 +57,13 @@ public class User { } public User save() { - Prefers.put("aliyundrive_user", new Gson().toJson(this)); + Prefers.put("aliyundrive_user", toString()); return this; } + + @NonNull + @Override + public String toString() { + return new Gson().toJson(this); + } } diff --git a/app/src/main/java/com/github/catvod/spider/Bili.java b/app/src/main/java/com/github/catvod/spider/Bili.java index 52ba0364..f45cbe79 100644 --- a/app/src/main/java/com/github/catvod/spider/Bili.java +++ b/app/src/main/java/com/github/catvod/spider/Bili.java @@ -1,12 +1,12 @@ package com.github.catvod.spider; -import android.annotation.TargetApi; +import android.app.AlertDialog; 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.view.Gravity; -import android.webkit.CookieManager; import android.widget.FrameLayout; import android.widget.ImageView; @@ -17,11 +17,10 @@ import com.github.catvod.crawler.Spider; import com.github.catvod.net.OkHttp; import com.github.catvod.utils.Prefers; import com.github.catvod.utils.QRCode; -import com.github.catvod.utils.Utils; import com.github.catvod.utils.Trans; +import com.github.catvod.utils.Utils; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.jsoup.Jsoup; @@ -29,6 +28,7 @@ import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -39,49 +39,39 @@ import java.util.concurrent.TimeUnit; */ public class Bili extends Spider { - private static final String url = "https://www.bilibili.com"; - private JSONObject ext; private ScheduledExecutorService service; - private ImageView view; - private static boolean login; - private static String finalUrl = ""; - private static boolean FirstTime = false; - private static String cookie = ""; + private HashMap header; + private AlertDialog dialog; + private JSONObject ext; + private String extend; + private boolean login; - private void getCookie() { - cookie = Prefers.getString("BiliCookie", ""); - HashMap> respHeaderMap = new HashMap<>(); - OkHttp.string(url, getHeaders(), respHeaderMap); - if (cookie.isEmpty()) { - for (String kk : Objects.requireNonNull(respHeaderMap.get("set-cookie"))) { - cookie += kk.split(";")[0] + ";"; - } - } + private String getCookie(String cookie) { + String cache = Prefers.getString("BiliCookie"); + if (!TextUtils.isEmpty(cache)) return cache; + if (cookie.startsWith("http")) return OkHttp.string(cookie).replace("\n", "").trim(); + return cookie.isEmpty() ? "buvid3=84B0395D-C9F2-C490-E92E-A09AB48FE26E71636infoc" : cookie; } - private static HashMap getHeaders() { - HashMap headers = new HashMap<>(); - headers.put("User-Agent", Utils.CHROME); - headers.put("Referer", url); - if (!cookie.isEmpty()) headers.put("cookie", cookie); - return headers; + private void setHeader() { + header.put("cookie", getCookie(ext.optString("cookie"))); + header.put("Referer", "https://www.bilibili.com"); + header.put("User-Agent", Utils.CHROME); + } + + 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 public void init(Context context, String extend) { try { - if (extend.startsWith("http")) { - extend = OkHttp.string(extend); - } - 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"); + this.extend = extend; + this.header = new HashMap<>(); + fetchRule(); } catch (Exception e) { e.printStackTrace(); } @@ -89,11 +79,13 @@ public class Bili extends Spider { @Override public String homeContent(boolean filter) throws Exception { + fetchRule(); return Result.string(Class.arrayFrom(ext.getJSONArray("classes").toString()), ext.getJSONObject("filter")); } @Override public String homeVideoContent() throws Exception { + fetchRule(); return categoryContent("窗 白噪音", "1", true, new HashMap<>()); } @@ -102,7 +94,7 @@ public class Bili extends Spider { String duration = extend.containsKey("duration") ? extend.get("duration") : "0"; 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; - JSONObject resp = new JSONObject(OkHttp.string(url, getHeaders())); + JSONObject resp = new JSONObject(OkHttp.string(url, header)); JSONArray result = resp.getJSONObject("data").getJSONArray("result"); List list = new ArrayList<>(); for (int i = 0; i < result.length(); ++i) { @@ -120,12 +112,13 @@ public class Bili extends Spider { @Override public String detailContent(List ids) throws Exception { + if (!login) checkLogin(); String bvid = ids.get(0); 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 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"); List playlist = new ArrayList<>(); JSONArray pages = detailData.getJSONArray("pages"); @@ -153,76 +146,86 @@ public class Bili extends Spider { @Override public String playerContent(String flag, String id, List vipFlags) throws Exception { - if (!login && !FirstTime) { - FirstTime = true; - checkService(); - getQRCode(); - } String[] ids = id.split("\\+"); String aid = ids[0]; String cid = ids[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"); - return Result.get().url(url).header(getHeaders()).string(); + return Result.get().url(url).header(header).string(); } - private void checkService() { - if (service != null) service.shutdownNow(); - if (view != null) Init.run(() -> Utils.removeView(view)); + private void checkLogin() throws Exception { + login = new JSONObject(OkHttp.string("https://api.bilibili.com/x/web-interface/nav", header)).getJSONObject("data").getBoolean("isLogin"); + boolean qrCode = Prefers.getBoolean("BiliQRCode", true); + if (!login && qrCode) getQRCode(); } - private void getQRCode() throws JSONException { - JSONObject data = new JSONObject(OkHttp.string("https://passport.bilibili.com/x/passport-login/web/qrcode/generate?source=main-mini", null)); - String qrcode = data.getJSONObject("data").getString("url"); - Init.run(() -> showCode(qrcode)); + private void getQRCode() { + try { + String json = OkHttp.string("https://passport.bilibili.com/x/passport-login/web/qrcode/generate?source=main-mini", null); + 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.scheduleAtFixedRate(() -> { 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 content = OkHttp.string(url, getHeaders()); - finalUrl = new JSONObject(content).getJSONObject("data").getString("url"); - } catch (JSONException e) { - e.printStackTrace(); + String json = OkHttp.string(url, header); + url = new JSONObject(json).getJSONObject("data").getString("url"); + if (!TextUtils.isEmpty(url)) setCookie(url); + } catch (Exception ignored) { } - if (!finalUrl.isEmpty()) setCookie(finalUrl); }, 1, 1, TimeUnit.SECONDS); } - private void setCookie(String value) { - finalUrl = value; - loadWebView(); - Init.show("请重新进入播放页"); - checkService(); + private void setCookie(String url) { + Map> respHeader = new HashMap<>(); + OkHttp.stringNoRedirect(url, header, respHeader); + StringBuilder sb = new StringBuilder(); + 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 loadWebView() { - CookieManager cookieManager = CookieManager.getInstance(); - cookieManager.flush(); - cookieManager.removeAllCookies(null); - cookieManager.removeSessionCookies(null); - HashMap> respHeaderMap = new HashMap<>(); - OkHttp.stringNoRedirect(finalUrl, getHeaders(), respHeaderMap); - cookie = ""; - for (String kk : Objects.requireNonNull(respHeaderMap.get("set-cookie"))) { - cookie = cookie.concat(kk.split(";")[0] + ";"); + private void stopService() { + if (service != null) service.shutdownNow(); + Init.run(this::dismiss); + } + + private void dismiss(DialogInterface dialog) { + Prefers.put("BiliQRCode", false); + stopService(); + } + + 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; } } diff --git a/jar/custom_spider.jar b/jar/custom_spider.jar index 46d678b3..b2934d84 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 70f0d30b..3bec5586 100644 --- a/jar/custom_spider.jar.md5 +++ b/jar/custom_spider.jar.md5 @@ -1 +1 @@ -78c395c04b5a9e444afbeec1b1681196 +be4d70b1bec4f2c930972aa6685ffe19 diff --git a/json/adult.json b/json/adult.json index d954ce55..bae1b768 100644 --- a/json/adult.json +++ b/json/adult.json @@ -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", "sites": [ { diff --git a/json/config.json b/json/config.json index c288c0c6..4a7a41bc 100644 --- a/json/config.json +++ b/json/config.json @@ -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", "sites": [ {