Skip to content

Commit

Permalink
fix #232, 更新字幕api的解析
Browse files Browse the repository at this point in the history
  • Loading branch information
nICEnnnnnnnLee committed Nov 2, 2024
1 parent 1e3ab54 commit cc6f8a3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/nicelee/bilibili/exceptions/NoSubtitleException.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 12 additions & 11 deletions src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
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;

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;
Expand Down Expand Up @@ -249,27 +249,28 @@ 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<String, String> headers_json = new HttpHeaders().getBiliJsonAPIHeaders(bvId);
String xml = util.getContent(url, headers_json, HttpCookies.globalCookiesWithFingerprint());
Pattern p = Pattern.compile("<subtitle>(.*?)</subtitle>");
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");
if (Global.cc_lang.equals(subLang)) {
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;
}

/**
Expand Down

0 comments on commit cc6f8a3

Please sign in to comment.