From d93c2fc4bd143a1abf759b35c2b38a061018c9fb Mon Sep 17 00:00:00 2001 From: root Date: Wed, 29 Jan 2020 15:33:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20#21=20=E5=BD=93=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E9=93=BE=E6=8E=A5=E5=A4=B1=E6=95=88=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=A4=87=E7=94=A8=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parsers/impl/AbstractBaseParser.java | 44 +++++++++++++--- .../bilibili/util/HttpRequestUtil.java | 52 ++++++++++++++++--- src/nicelee/test/junit/UtilTest.java | 15 +++++- 3 files changed, 96 insertions(+), 15 deletions(-) diff --git a/src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java b/src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java index 1046065f..2e84fb0c 100644 --- a/src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java +++ b/src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java @@ -198,8 +198,7 @@ private LinkedHashMap storyList2Map(String avId, int videoFormat clip.setPicPreview(viInfo.getVideoPreview()); clip.setUpName(viInfo.getAuthor()); clip.setUpId(viInfo.getAuthorId()); - - + LinkedHashMap links = new LinkedHashMap(); try { int qnList[] = getVideoQNList(avId, String.valueOf(clip.getcId())); @@ -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) { @@ -431,6 +462,7 @@ String getVideoM4sLink(String avId, String cid, int qn) { /** * 将avId currentStory故事线的所有子结局全部放入List + * * @param avIdNum * @param node * @param graph_version diff --git a/src/nicelee/bilibili/util/HttpRequestUtil.java b/src/nicelee/bilibili/util/HttpRequestUtil.java index 7b8b5ca7..c3e7d4f9 100644 --- a/src/nicelee/bilibili/util/HttpRequestUtil.java +++ b/src/nicelee/bilibili/util/HttpRequestUtil.java @@ -141,7 +141,7 @@ public boolean download(String url, String fileName, HashMap hea String urlNameString = url; HttpURLConnection conn = connect(headers, urlNameString, null); conn.connect(); - + if (conn.getResponseCode() == 403) { Logger.println("403被拒,尝试更换Headers(可能由于app版权的原因)"); conn.disconnect(); @@ -172,7 +172,7 @@ public boolean download(String url, String fileName, HashMap hea reader.close(); throw e; } - if(buffer == null) + if (buffer == null) buffer = new byte[1024 * 1024]; int lenRead = inn.read(buffer); downloadedFileSize = offset + lenRead; @@ -234,7 +234,7 @@ private HttpURLConnection connect(HashMap headers, String url, L HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); conn.setConnectTimeout(10000); conn.setReadTimeout(10000); - if(headers != null) { + if (headers != null) { for (Map.Entry entry : headers.entrySet()) { conn.setRequestProperty(entry.getKey(), entry.getValue()); // System.out.println(entry.getKey() + ":" + entry.getValue()); @@ -253,7 +253,7 @@ private HttpURLConnection connect(HashMap headers, String url, L // System.out.println(cookie); conn.setRequestProperty("Cookie", cookie); } - //conn.connect(); + // conn.connect(); return conn; } @@ -298,17 +298,17 @@ public String getContent(String url, HashMap headers, List headers, String pa conn.setUseCaches(false); // 不允许缓存 conn.setRequestMethod("POST"); // 设置POST方式连接 conn.connect(); - + // 建立输入流,向指向的URL传入参数 DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(param); @@ -398,6 +398,42 @@ public String postContent(String url, HashMap headers, String pa return result.toString(); } + /** + * 测试视频链接url的有效性 + * + * @param url + * @param headers + * @param listCookie + * @return + */ + public boolean checkValid(String url, HashMap headers, List 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()) { diff --git a/src/nicelee/test/junit/UtilTest.java b/src/nicelee/test/junit/UtilTest.java index 084e5302..b05a696e 100644 --- a/src/nicelee/test/junit/UtilTest.java +++ b/src/nicelee/test/junit/UtilTest.java @@ -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 { @@ -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); + } + }