From 34fc09d73b6418b6a0b83fb773729666ddc9957b Mon Sep 17 00:00:00 2001 From: nilaoda Date: Wed, 20 Jul 2022 14:21:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96BaseUrl=E7=9A=84=E9=80=89?= =?UTF-8?q?=E6=8B=A9(=E8=87=AA=E5=8A=A8=E5=A4=84=E7=90=86302)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Resource/ResString.Designer.cs | 18 ++++++ .../Resource/ResString.resx | 6 ++ .../Resource/ResString.zh-Hans.resx | 6 ++ .../Resource/ResString.zh-Hant.resx | 6 ++ src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs | 63 ++++++++----------- .../Extractor/HLSExtractor.cs | 17 +++-- src/N_m3u8DL-RE.Parser/StreamExtractor.cs | 2 +- 7 files changed, 74 insertions(+), 44 deletions(-) diff --git a/src/N_m3u8DL-RE.Common/Resource/ResString.Designer.cs b/src/N_m3u8DL-RE.Common/Resource/ResString.Designer.cs index e491cb7..2370ebd 100644 --- a/src/N_m3u8DL-RE.Common/Resource/ResString.Designer.cs +++ b/src/N_m3u8DL-RE.Common/Resource/ResString.Designer.cs @@ -87,6 +87,15 @@ namespace N_m3u8DL_RE.Common.Resource { } } + /// + /// 查找类似 Add Params of input Url to segments, useful for some websites, such as kakao.com 的本地化字符串。 + /// + public static string cmd_appendUrlParams { + get { + return ResourceManager.GetString("cmd_appendUrlParams", resourceCulture); + } + } + /// /// 查找类似 Automatically selects the best tracks of all types 的本地化字符串。 /// @@ -241,6 +250,15 @@ namespace N_m3u8DL_RE.Common.Resource { } } + /// + /// 查找类似 Write meta json after parsed 的本地化字符串。 + /// + public static string cmd_writeMetaJson { + get { + return ResourceManager.GetString("cmd_writeMetaJson", resourceCulture); + } + } + /// /// 查找类似 Fetch: 的本地化字符串。 /// diff --git a/src/N_m3u8DL-RE.Common/Resource/ResString.resx b/src/N_m3u8DL-RE.Common/Resource/ResString.resx index 69ef495..ad6a70f 100644 --- a/src/N_m3u8DL-RE.Common/Resource/ResString.resx +++ b/src/N_m3u8DL-RE.Common/Resource/ResString.resx @@ -221,4 +221,10 @@ Skip download + + Write meta json after parsed + + + Add Params of input Url to segments, useful for some websites, such as kakao.com + \ No newline at end of file diff --git a/src/N_m3u8DL-RE.Common/Resource/ResString.zh-Hans.resx b/src/N_m3u8DL-RE.Common/Resource/ResString.zh-Hans.resx index 52094e4..d278da2 100644 --- a/src/N_m3u8DL-RE.Common/Resource/ResString.zh-Hans.resx +++ b/src/N_m3u8DL-RE.Common/Resource/ResString.zh-Hans.resx @@ -241,4 +241,10 @@ 跳过下载 + + 解析后的信息是否输出json文件 + + + 将输入Url的Params添加至分片, 对某些网站很有用, 例如 kakao.com + \ No newline at end of file diff --git a/src/N_m3u8DL-RE.Common/Resource/ResString.zh-Hant.resx b/src/N_m3u8DL-RE.Common/Resource/ResString.zh-Hant.resx index 7952de7..d69c885 100644 --- a/src/N_m3u8DL-RE.Common/Resource/ResString.zh-Hant.resx +++ b/src/N_m3u8DL-RE.Common/Resource/ResString.zh-Hant.resx @@ -221,4 +221,10 @@ 跳過下載 + + 解析後的信息是否輸出json文件 + + + 將輸入Url的Params添加至分片, 對某些網站很有用, 例如 kakao.com + \ No newline at end of file diff --git a/src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs b/src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs index 80522dc..203e957 100644 --- a/src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs +++ b/src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs @@ -49,58 +49,24 @@ namespace N_m3u8DL_RE.Common.Util } } Logger.Debug(webRequest.Headers.ToString()); + //手动处理跳转,以免自定义Headers丢失 var webResponse = await AppHttpClient.SendAsync(webRequest, HttpCompletionOption.ResponseHeadersRead); if (webResponse.StatusCode == HttpStatusCode.Found || webResponse.StatusCode == HttpStatusCode.Moved) { HttpResponseHeaders respHeaders = webResponse.Headers; Logger.Debug(respHeaders.ToString()); - if (respHeaders != null && respHeaders.Location != null) + if (respHeaders != null && respHeaders.Location != null && respHeaders.Location.AbsoluteUri != url) { var redirectedUrl = respHeaders.Location.AbsoluteUri; return await DoGetAsync(redirectedUrl, headers); } } + //手动将跳转后的URL设置进去, 用于后续取用 + webResponse.Headers.Location = new Uri(url); webResponse.EnsureSuccessStatusCode(); return webResponse; } - //重定向 - public static async Task Get302Async(string url, Dictionary? headers = null) - { - Logger.Debug(ResString.fetch + url); - var handler = new HttpClientHandler() - { - AllowAutoRedirect = false - }; - string redirectedUrl = url; - - using (HttpClient client = new HttpClient(handler)) - { - if (headers != null) - { - foreach (var item in headers) - { - client.DefaultRequestHeaders.TryAddWithoutValidation(item.Key, item.Value); - } - } - using (HttpResponseMessage response = await client.GetAsync(url)) - using (HttpContent content = response.Content) - { - Logger.Debug(ResString.fetch + response.Headers); - if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved) - { - HttpResponseHeaders respHeaders = response.Headers; - if (respHeaders != null && respHeaders.Location != null) - { - redirectedUrl = respHeaders.Location.AbsoluteUri; - } - } - } - } - - return redirectedUrl; - } - public static async Task GetBytesAsync(string url, Dictionary? headers = null) { byte[] bytes = new byte[0]; @@ -110,6 +76,12 @@ namespace N_m3u8DL_RE.Common.Util return bytes; } + /// + /// 获取网页源码 + /// + /// + /// + /// public static async Task GetWebSourceAsync(string url, Dictionary? headers = null) { string htmlCode = string.Empty; @@ -119,6 +91,21 @@ namespace N_m3u8DL_RE.Common.Util return htmlCode; } + /// + /// 获取网页源码和跳转后的URL + /// + /// + /// + /// (Source Code, RedirectedUrl) + public static async Task<(string, string)> GetWebSourceAndNewUrlAsync(string url, Dictionary? headers = null) + { + string htmlCode = string.Empty; + var webResponse = await DoGetAsync(url, headers); + htmlCode = await webResponse.Content.ReadAsStringAsync(); + Logger.Debug(htmlCode); + return (htmlCode, webResponse.Headers.Location != null ? webResponse.Headers.Location.AbsoluteUri : url); + } + public static async Task GetPostResponseAsync(string Url, byte[] postData) { string htmlCode = string.Empty; diff --git a/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs b/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs index cd79c01..19a6f02 100644 --- a/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs +++ b/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs @@ -31,13 +31,18 @@ namespace N_m3u8DL_RE.Parser.Extractor { this.ParserConfig = parserConfig; this.M3u8Url = parserConfig.Url ?? string.Empty; - if (!string.IsNullOrEmpty(parserConfig.BaseUrl)) + this.SetBaseUrl(); + } + + private void SetBaseUrl() + { + if (!string.IsNullOrEmpty(ParserConfig.BaseUrl)) { - this.BaseUrl = parserConfig.BaseUrl; + this.BaseUrl = ParserConfig.BaseUrl; } else { - this.BaseUrl = parserConfig.BaseUrl = this.M3u8Url; + this.BaseUrl = ParserConfig.BaseUrl = this.M3u8Url; } } @@ -499,9 +504,11 @@ namespace N_m3u8DL_RE.Parser.Extractor } else if (url.StartsWith("http")) { - this.M3u8Content = await HTTPUtil.GetWebSourceAsync(url, ParserConfig.Headers); + (this.M3u8Content, url) = await HTTPUtil.GetWebSourceAndNewUrlAsync(url, ParserConfig.Headers); } - this.M3u8Url = this.BaseUrl = url; + + this.M3u8Url = url; + this.SetBaseUrl(); this.PreProcessContent(); } diff --git a/src/N_m3u8DL-RE.Parser/StreamExtractor.cs b/src/N_m3u8DL-RE.Parser/StreamExtractor.cs index 0bd3004..86c995f 100644 --- a/src/N_m3u8DL-RE.Parser/StreamExtractor.cs +++ b/src/N_m3u8DL-RE.Parser/StreamExtractor.cs @@ -42,7 +42,7 @@ namespace N_m3u8DL_RE.Parser } else if (url.StartsWith("http")) { - this.rawText = HTTPUtil.GetWebSourceAsync(url, parserConfig.Headers).Result; + (this.rawText, url) = HTTPUtil.GetWebSourceAndNewUrlAsync(url, parserConfig.Headers).Result; parserConfig.Url = url; } else if (File.Exists(url))