From 889c8ba003a3d8d98933c9f21a737fd54e1b96ac Mon Sep 17 00:00:00 2001 From: Apin Date: Sat, 2 Jan 2021 20:33:33 +0700 Subject: [PATCH] adding header when getting file info --- .../com/jeffmony/downloader/VideoDownloadManager.java | 2 +- .../jeffmony/downloader/VideoInfoParserManager.java | 6 +++--- .../java/com/jeffmony/downloader/m3u8/M3U8Utils.java | 11 +++++++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/downloader/src/main/java/com/jeffmony/downloader/VideoDownloadManager.java b/downloader/src/main/java/com/jeffmony/downloader/VideoDownloadManager.java index 7064783..8a5de0c 100644 --- a/downloader/src/main/java/com/jeffmony/downloader/VideoDownloadManager.java +++ b/downloader/src/main/java/com/jeffmony/downloader/VideoDownloadManager.java @@ -152,7 +152,7 @@ private void parseExistVideoDownloadInfo(final VideoTaskItem taskItem, final Has startBaseVideoDownloadTask(taskItem, headers); } else if (taskItem.isHlsType()) { VideoInfoParserManager.getInstance() - .parseM3U8File(taskItem, new IVideoInfoParseListener() { + .parseM3U8File(taskItem, headers, new IVideoInfoParseListener() { @Override public void onM3U8FileParseSuccess(VideoTaskItem info, M3U8 m3u8) { diff --git a/downloader/src/main/java/com/jeffmony/downloader/VideoInfoParserManager.java b/downloader/src/main/java/com/jeffmony/downloader/VideoInfoParserManager.java index 00035da..80bcf31 100644 --- a/downloader/src/main/java/com/jeffmony/downloader/VideoInfoParserManager.java +++ b/downloader/src/main/java/com/jeffmony/downloader/VideoInfoParserManager.java @@ -153,7 +153,7 @@ private void parseM3U8Info(VideoTaskItem taskItem, IVideoInfoListener listener, HashMap headers) { try { M3U8 m3u8 = - M3U8Utils.parseM3U8Info(mConfig, taskItem.getUrl(), false, null); + M3U8Utils.parseM3U8Info(mConfig, taskItem.getUrl(), false, null, headers); // HLS LIVE video cannot be proxy cached. if (m3u8.hasEndList()) { String saveName = VideoDownloadUtils.computeMD5(taskItem.getUrl()); @@ -176,7 +176,7 @@ private void parseM3U8Info(VideoTaskItem taskItem, IVideoInfoListener listener, } } - public void parseM3U8File(VideoTaskItem taskItem, + public void parseM3U8File(VideoTaskItem taskItem, final HashMap headers, IVideoInfoParseListener callback) { File remoteM3U8File = new File(taskItem.getSaveDir(), VideoDownloadUtils.REMOTE_M3U8); if (!remoteM3U8File.exists()) { @@ -186,7 +186,7 @@ public void parseM3U8File(VideoTaskItem taskItem, } try { M3U8 m3u8 = M3U8Utils.parseM3U8Info(mConfig, taskItem.getUrl(), true, - remoteM3U8File); + remoteM3U8File, headers); callback.onM3U8FileParseSuccess(taskItem, m3u8); } catch (Exception e) { e.printStackTrace(); diff --git a/downloader/src/main/java/com/jeffmony/downloader/m3u8/M3U8Utils.java b/downloader/src/main/java/com/jeffmony/downloader/m3u8/M3U8Utils.java index 27a5fb5..c602fde 100644 --- a/downloader/src/main/java/com/jeffmony/downloader/m3u8/M3U8Utils.java +++ b/downloader/src/main/java/com/jeffmony/downloader/m3u8/M3U8Utils.java @@ -17,6 +17,8 @@ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -35,7 +37,7 @@ public class M3U8Utils { */ public static M3U8 parseM3U8Info(VideoDownloadConfig config, String videoUrl, boolean isLocalFile, - File m3u8File) throws IOException { + File m3u8File, final HashMap headers) throws IOException { URL url = new URL(videoUrl); InputStreamReader inputStreamReader = null; BufferedReader bufferedReader; @@ -44,6 +46,11 @@ public static M3U8 parseM3U8Info(VideoDownloadConfig config, bufferedReader = new BufferedReader(inputStreamReader); } else { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + if (headers != null) { + for (Map.Entry item : headers.entrySet()) { + connection.setRequestProperty(item.getKey(), item.getValue()); + } + } if (config.shouldIgnoreCertErrors() && connection instanceof HttpsURLConnection) { HttpUtils.trustAllCert((HttpsURLConnection) connection); @@ -126,7 +133,7 @@ public static M3U8 parseM3U8Info(VideoDownloadConfig config, } // It has '#EXT-X-STREAM-INF' tag; if (hasStreamInfo) { - return parseM3U8Info(config, getFinalUrl(videoUrl, line), isLocalFile, m3u8File); + return parseM3U8Info(config, getFinalUrl(videoUrl, line), isLocalFile, m3u8File, headers); } M3U8Ts ts = new M3U8Ts(); if (isLocalFile) {