From 5f435157910c0fc0642e41782a29a482acfb6b36 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Sun, 13 Nov 2022 17:37:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=9B=B4=E6=92=AD=E4=B8=AD?= =?UTF-8?q?=E5=AF=B9=E9=87=8D=E5=AE=9A=E5=90=91=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extractor/DASHExtractor2.cs | 14 +++++++++++- .../Extractor/HLSExtractor.cs | 22 +++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/N_m3u8DL-RE.Parser/Extractor/DASHExtractor2.cs b/src/N_m3u8DL-RE.Parser/Extractor/DASHExtractor2.cs index 25f1158..77970d5 100644 --- a/src/N_m3u8DL-RE.Parser/Extractor/DASHExtractor2.cs +++ b/src/N_m3u8DL-RE.Parser/Extractor/DASHExtractor2.cs @@ -488,9 +488,21 @@ namespace N_m3u8DL_RE.Parser.Extractor public async Task RefreshPlayListAsync(List streamSpecs) { if (streamSpecs.Count == 0) return; - var (rawText, url) = await HTTPUtil.GetWebSourceAndNewUrlAsync(ParserConfig.OriginalUrl, ParserConfig.Headers); + + var (rawText, url) = ("", ParserConfig.Url); + try + { + (rawText, url) = await HTTPUtil.GetWebSourceAndNewUrlAsync(ParserConfig.Url, ParserConfig.Headers); + } + catch (HttpRequestException) when (ParserConfig.Url!= ParserConfig.OriginalUrl) + { + //当URL无法访问时,再请求原始URL + (rawText, url) = await HTTPUtil.GetWebSourceAndNewUrlAsync(ParserConfig.OriginalUrl, ParserConfig.Headers); + } + ParserConfig.Url = url; SetInitUrl(); + var newStreams = await ExtractStreamsAsync(rawText); foreach (var streamSpec in streamSpecs) { diff --git a/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs b/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs index 014da4e..b33e1d0 100644 --- a/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs +++ b/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs @@ -506,7 +506,15 @@ namespace N_m3u8DL_RE.Parser.Extractor } else if (url.StartsWith("http")) { - (this.M3u8Content, url) = await HTTPUtil.GetWebSourceAndNewUrlAsync(url, ParserConfig.Headers); + try + { + (this.M3u8Content, url) = await HTTPUtil.GetWebSourceAndNewUrlAsync(url, ParserConfig.Headers); + } + catch (HttpRequestException) when (url != ParserConfig.OriginalUrl) + { + //当URL无法访问时,再请求原始URL + (this.M3u8Content, url) = await HTTPUtil.GetWebSourceAndNewUrlAsync(ParserConfig.OriginalUrl, ParserConfig.Headers); + } } this.M3u8Url = url; @@ -516,10 +524,10 @@ namespace N_m3u8DL_RE.Parser.Extractor public async Task FetchPlayListAsync(List lists) { - if (MasterM3u8Flag && !FirstRefreshFlag) + if (MasterM3u8Flag) { //重新加载master m3u8, 刷新选中流的URL - await LoadM3u8FromUrlAsync(ParserConfig.OriginalUrl); + await LoadM3u8FromUrlAsync(ParserConfig.Url); var newStreams = await ParseMasterListAsync(); newStreams = newStreams.DistinctBy(p => p.Url).ToList(); foreach (var l in lists) @@ -532,11 +540,6 @@ namespace N_m3u8DL_RE.Parser.Extractor } } } - else if (!MasterM3u8Flag && lists.Count == 1 && ParserConfig.OriginalUrl != lists[0].Url) - { - //单m3u8, 但是发生了重定向, 则应从原始URL开始解析 - lists[0].Url = ParserConfig.OriginalUrl; - } for (int i = 0; i < lists.Count; i++) { @@ -555,9 +558,6 @@ namespace N_m3u8DL_RE.Parser.Extractor lists[i].Extension = lists[i].Playlist!.MediaInit != null ? "m4s" : "ts"; } } - - //首次刷新已结束,后续重新加载时应该从master开始重新加载 - FirstRefreshFlag = false; } public async Task RefreshPlayListAsync(List streamSpecs)