Skip to content

Commit

Permalink
修复 #21 当下载链接失效时,使用备用链接
Browse files Browse the repository at this point in the history
  • Loading branch information
nICEnnnnnnnLee committed Jan 29, 2020
1 parent 1b9f65d commit d93c2fc
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 15 deletions.
44 changes: 38 additions & 6 deletions src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ private LinkedHashMap<Long, ClipInfo> storyList2Map(String avId, int videoFormat
clip.setPicPreview(viInfo.getVideoPreview());
clip.setUpName(viInfo.getAuthor());
clip.setUpId(viInfo.getAuthorId());



LinkedHashMap<Integer, String> links = new LinkedHashMap<Integer, String>();
try {
int qnList[] = getVideoQNList(avId, String.valueOf(clip.getcId()));
Expand Down Expand Up @@ -412,14 +411,46 @@ String getVideoM4sLink(String avId, String cid, int qn) {
for (int i = 0; i < videos.length(); i++) {
JSONObject video = videos.getJSONObject(i);
if (video.getInt("id") == linkQN) {
link.append(video.getString("baseUrl")).append("#");
break;
// 测试baseUrl有效性,如果无效404,使用backupUrl
String video_url = video.getString("baseUrl");
if (util.checkValid(video_url, headers.getBiliWwwM4sHeaders(avId), null)) {
link.append(video_url).append("#");
break;
}else {
JSONArray backup_urls = video.getJSONArray("backupUrl");
boolean findValidUrl = false;
for (int j = 0; j < backup_urls.length(); j++) {
video_url = backup_urls.getString(j);
if (util.checkValid(video_url, headers.getBiliWwwM4sHeaders(avId), null)) {
findValidUrl = true;
break;
}
}
if(findValidUrl) {
link.append(video_url).append("#");
break;
}
}

}
}
// 获取音频链接(默认第一个)
JSONArray audios = jObj.getJSONObject("dash").optJSONArray("audio");
if(audios != null) {
link.append(audios.getJSONObject(0).getString("baseUrl"));
if (audios != null) {
JSONObject audio = audios.getJSONObject(0);
String audio_url = audio.getString("baseUrl");
if (util.checkValid(audio_url, headers.getBiliWwwM4sHeaders(avId), null)) {
link.append(audio_url);
} else {
JSONArray backup_urls = audio.getJSONArray("backupUrl");
for (int j = 0; j < backup_urls.length(); j++) {
audio_url = backup_urls.getString(j);
if (util.checkValid(audio_url, headers.getBiliWwwM4sHeaders(avId), null)) {
link.append(audio_url);
break;
}
}
}
}
return link.toString();
} catch (Exception e) {
Expand All @@ -431,6 +462,7 @@ String getVideoM4sLink(String avId, String cid, int qn) {

/**
* 将avId currentStory故事线的所有子结局全部放入List
*
* @param avIdNum
* @param node
* @param graph_version
Expand Down
52 changes: 44 additions & 8 deletions src/nicelee/bilibili/util/HttpRequestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public boolean download(String url, String fileName, HashMap<String, String> hea
String urlNameString = url;
HttpURLConnection conn = connect(headers, urlNameString, null);
conn.connect();

if (conn.getResponseCode() == 403) {
Logger.println("403被拒,尝试更换Headers(可能由于app版权的原因)");
conn.disconnect();
Expand Down Expand Up @@ -172,7 +172,7 @@ public boolean download(String url, String fileName, HashMap<String, String> hea
reader.close();
throw e;
}
if(buffer == null)
if (buffer == null)
buffer = new byte[1024 * 1024];
int lenRead = inn.read(buffer);
downloadedFileSize = offset + lenRead;
Expand Down Expand Up @@ -234,7 +234,7 @@ private HttpURLConnection connect(HashMap<String, String> headers, String url, L
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
if(headers != null) {
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
// System.out.println(entry.getKey() + ":" + entry.getValue());
Expand All @@ -253,7 +253,7 @@ private HttpURLConnection connect(HashMap<String, String> headers, String url, L
// System.out.println(cookie);
conn.setRequestProperty("Cookie", cookie);
}
//conn.connect();
// conn.connect();
return conn;
}

Expand Down Expand Up @@ -298,17 +298,17 @@ public String getContent(String url, HashMap<String, String> headers, List<HttpC
try {
HttpURLConnection conn = connect(headers, url, listCookie);
conn.connect();

String encoding = conn.getContentEncoding();
InputStream ism = conn.getInputStream();
if (encoding != null && encoding.contains("gzip")) {// 首先判断服务器返回的数据是否支持gzip压缩,
// System.out.println(encoding);
// 如果支持则应该使用GZIPInputStream解压,否则会出现乱码无效数据
ism = new GZIPInputStream(ism);
}else if(encoding != null && encoding.contains("deflate")) {
} else if (encoding != null && encoding.contains("deflate")) {
ism = new InflaterInputStream(ism, new Inflater(true));
}

in = new BufferedReader(new InputStreamReader(ism, "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
Expand Down Expand Up @@ -363,7 +363,7 @@ public String postContent(String url, HashMap<String, String> headers, String pa
conn.setUseCaches(false); // 不允许缓存
conn.setRequestMethod("POST"); // 设置POST方式连接
conn.connect();

// 建立输入流,向指向的URL传入参数
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(param);
Expand Down Expand Up @@ -398,6 +398,42 @@ public String postContent(String url, HashMap<String, String> headers, String pa
return result.toString();
}

/**
* 测试视频链接url的有效性
*
* @param url
* @param headers
* @param listCookie
* @return
*/
public boolean checkValid(String url, HashMap<String, String> headers, List<HttpCookie> listCookie) {
BufferedReader in = null;
try {
HttpURLConnection conn = connect(headers, url, listCookie);
// 设置参数
// conn.setRequestMethod("OPTIONS"); // 设置OPTIONS方式连接
headers.put("range", "bytes=0-100");
conn.connect();

// System.out.println(url);
// System.out.println(conn.getResponseCode());
return conn.getResponseCode() != 404;
// printCookie(manager.getCookieStore());
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
// e.printStackTrace();
return false;
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

private File getFile(String dst) {
File folder = new File(savePath);
if (!folder.exists()) {
Expand Down
15 changes: 14 additions & 1 deletion src/nicelee/test/junit/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.Test;

import nicelee.bilibili.util.CmdUtil;
import nicelee.bilibili.util.HttpHeaders;
import nicelee.bilibili.util.HttpRequestUtil;
import nicelee.ui.Global;

public class UtilTest {
Expand Down Expand Up @@ -169,10 +171,21 @@ public void testFFmpeg() {
/**
* 测试 环境变量是否有影响
*/
@Test
//@Test
public void testRunCommandWithEnv() {
String[] cmd = {"git", "--version"};
CmdUtil.run(cmd);
}

/**
* 测试 视频链接的有效性
*/
@Test
public void testValidCheck() {
HttpRequestUtil util = new HttpRequestUtil();
String url = "http://cn-hbyc3-dx-v-06.acgvideo.com/upgcxcode/50/99/142109950/142109950-1-30280.m4s?expires=1580287500&platform=pc&ssig=yOiiVa3VgyAIS6mF3EvXzQ&oi=2936109567&trid=f9c8ce470a1841d6bdee1059378f3f58u&nfc=1&nfb=maPYqpoel5MI3qOUX6YpRA==&mid=0";
boolean result = util.checkValid(url, new HttpHeaders().getBiliWwwM4sHeaders("av83071175"), null);
System.out.println(result);
}

}

0 comments on commit d93c2fc

Please sign in to comment.