From cc6f8a30dbc44985c4ad23c2fd97e53eb3f7174f Mon Sep 17 00:00:00 2001 From: lijia Date: Sat, 2 Nov 2024 20:04:47 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#232,=20=E6=9B=B4=E6=96=B0=E5=AD=97?= =?UTF-8?q?=E5=B9=95api=E7=9A=84=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exceptions/NoSubtitleException.java | 14 +++++++++++ .../parsers/impl/AbstractBaseParser.java | 23 ++++++++++--------- 2 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 src/nicelee/bilibili/exceptions/NoSubtitleException.java diff --git a/src/nicelee/bilibili/exceptions/NoSubtitleException.java b/src/nicelee/bilibili/exceptions/NoSubtitleException.java new file mode 100644 index 00000000..485fe8fb --- /dev/null +++ b/src/nicelee/bilibili/exceptions/NoSubtitleException.java @@ -0,0 +1,14 @@ +package nicelee.bilibili.exceptions; + +public class NoSubtitleException extends BilibiliError{ + + private static final long serialVersionUID = -154799775624722132L; + + public NoSubtitleException(String message) { + super(message); + } + + public NoSubtitleException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java b/src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java index e982dedc..b9374edc 100644 --- a/src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java +++ b/src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java @@ -7,7 +7,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.json.JSONArray; import org.json.JSONObject; @@ -15,6 +14,7 @@ import nicelee.bilibili.API; import nicelee.bilibili.enums.DownloadModeEnum; import nicelee.bilibili.exceptions.ApiLinkQueryParseError; +import nicelee.bilibili.exceptions.NoSubtitleException; import nicelee.bilibili.exceptions.QualityTooLowException; import nicelee.bilibili.model.ClipInfo; import nicelee.bilibili.model.StoryClipInfo; @@ -249,15 +249,15 @@ protected String getAudioLink(String auId, String _auId, int qn) { } protected String getVideoSubtitleLink(String bvId, String cid, int qn) { - String url = String.format("https://api.bilibili.com/x/player.so?id=cid:%s&bvid=%s", cid, bvId); + String url = String.format("https://api.bilibili.com/x/player/wbi/v2?bvid=%s&cid=%s&isGaiaAvoided=false", bvId, cid); + url += API.genDmImgParams(); + url = API.encWbi(url); Logger.println(url); HashMap headers_json = new HttpHeaders().getBiliJsonAPIHeaders(bvId); - String xml = util.getContent(url, headers_json, HttpCookies.globalCookiesWithFingerprint()); - Pattern p = Pattern.compile("(.*?)"); - Matcher matcher = p.matcher(xml); - if (matcher.find()) { - paramSetter.setRealQN(qn); - JSONArray subList = new JSONObject(matcher.group(1)).getJSONArray("subtitles"); + String result = util.getContent(url, headers_json, HttpCookies.globalCookiesWithFingerprint()); + paramSetter.setRealQN(qn); + try { + JSONArray subList = new JSONObject(result).getJSONObject("data").getJSONObject("subtitle").getJSONArray("subtitles"); for (int i = 0; i < subList.length(); i++) { JSONObject sub = subList.getJSONObject(i); String subLang = sub.getString("lan"); @@ -265,11 +265,12 @@ protected String getVideoSubtitleLink(String bvId, String cid, int qn) { return "https:" + sub.getString("subtitle_url"); } } - + return "https:" + subList.getJSONObject(0).getString("subtitle_url"); + } catch (Exception e) { + String tips = Global.isLogin? "未知错误" : "未能找到字幕,这可能是没有登录造成的"; + throw new NoSubtitleException(tips, e); } - - return null; } /**