diff --git a/src/N_m3u8DL-RE.Parser/Config/ParserConfig.cs b/src/N_m3u8DL-RE.Parser/Config/ParserConfig.cs index 5bacc9e..e377345 100644 --- a/src/N_m3u8DL-RE.Parser/Config/ParserConfig.cs +++ b/src/N_m3u8DL-RE.Parser/Config/ParserConfig.cs @@ -57,5 +57,10 @@ namespace N_m3u8DL_RE.Parser.Config /// 此参数将会传递给URL Processor中 /// public string? UrlProcessorArgs { get; set; } + + /// + /// KEY重试次数 + /// + public int KeyRetryCount { get; set; } = 3; } } diff --git a/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs b/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs index 128b5b8..68d72f8 100644 --- a/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs +++ b/src/N_m3u8DL-RE.Parser/Processor/HLS/DefaultHLSKeyProcessor.cs @@ -5,6 +5,7 @@ using N_m3u8DL_RE.Common.Resource; using N_m3u8DL_RE.Common.Util; using N_m3u8DL_RE.Parser.Config; using N_m3u8DL_RE.Parser.Util; +using Spectre.Console; using System; using System.Collections.Generic; using System.Linq; @@ -62,9 +63,21 @@ namespace N_m3u8DL_RE.Parser.Processor.HLS } else if (!string.IsNullOrEmpty(uri)) { - var segUrl = PreProcessUrl(ParserUtil.CombineURL(m3u8Url, uri), parserConfig); - var bytes = HTTPUtil.GetBytesAsync(segUrl, parserConfig.Headers).Result; - encryptInfo.Key = bytes; + var retryCount = parserConfig.KeyRetryCount; + getHttpKey: + try + { + var segUrl = PreProcessUrl(ParserUtil.CombineURL(m3u8Url, uri), parserConfig); + var bytes = HTTPUtil.GetBytesAsync(segUrl, parserConfig.Headers).Result; + encryptInfo.Key = bytes; + } + catch (Exception _ex) + { + Logger.WarnMarkUp($"[grey]{_ex.Message.EscapeMarkup()} retryCount: {retryCount}[/]"); + Thread.Sleep(1000); + if (retryCount > 0) goto getHttpKey; + else throw; + } } } catch (Exception ex)