优化URL参数拼接

This commit is contained in:
nilaoda 2022-11-30 22:45:48 +08:00
parent 456f1fe69e
commit 33a40d2f78
2 changed files with 17 additions and 7 deletions

View File

@ -546,7 +546,7 @@ namespace N_m3u8DL_RE.Parser.Extractor
{ {
//重新加载m3u8 //重新加载m3u8
await LoadM3u8FromUrlAsync(lists[i].Url!); await LoadM3u8FromUrlAsync(lists[i].Url!);
lists[i].Playlist = await ParseListAsync(); lists[i].Playlist!.MediaParts = (await ParseListAsync()).MediaParts; //不更新init
if (lists[i].MediaType == MediaType.SUBTITLES) if (lists[i].MediaType == MediaType.SUBTITLES)
{ {
var a = lists[i].Playlist!.MediaParts.Any(p => p.MediaSegments.Any(m => m.Url.Contains(".ttml"))); var a = lists[i].Playlist!.MediaParts.Any(p => p.MediaSegments.Any(m => m.Url.Contains(".ttml")));

View File

@ -7,25 +7,35 @@ using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web;
namespace N_m3u8DL_RE.Parser.Processor namespace N_m3u8DL_RE.Parser.Processor
{ {
public class DefaultUrlProcessor : UrlProcessor public class DefaultUrlProcessor : UrlProcessor
{ {
public override bool CanProcess(ExtractorType extractorType, string oriUrl, ParserConfig paserConfig) => true; public override bool CanProcess(ExtractorType extractorType, string oriUrl, ParserConfig paserConfig) => paserConfig.AppendUrlParams;
public override string Process(string oriUrl, ParserConfig paserConfig) public override string Process(string oriUrl, ParserConfig paserConfig)
{ {
if (paserConfig.AppendUrlParams && oriUrl.StartsWith("http")) if (oriUrl.StartsWith("http"))
{ {
var uriFromConfig = new Uri(paserConfig.Url); var uriFromConfig = new Uri(paserConfig.Url);
var uriFromConfigQuery = HttpUtility.ParseQueryString(uriFromConfig.Query);
var oldUri = new Uri(oriUrl); var oldUri = new Uri(oriUrl);
var newQuery = (oldUri.Query.TrimStart('?') + "&" + uriFromConfig.Query.TrimStart('?')).Trim('&'); var newQuery = HttpUtility.ParseQueryString(oldUri.Query);
var sameLeft = oldUri.GetLeftPart(UriPartial.Path) == uriFromConfig.GetLeftPart(UriPartial.Path); foreach (var item in uriFromConfigQuery.AllKeys)
if (sameLeft && !oriUrl.Contains(uriFromConfig.Query)) {
if (newQuery.AllKeys.Contains(item))
newQuery.Set(item, uriFromConfigQuery.Get(item));
else
newQuery.Add(item, uriFromConfigQuery.Get(item));
}
if (!string.IsNullOrEmpty(newQuery.ToString()))
{ {
Logger.Debug("Before: " + oriUrl); Logger.Debug("Before: " + oriUrl);
oriUrl = (oldUri.GetLeftPart(UriPartial.Path) + "?" + newQuery).TrimEnd('?'); oriUrl = (oldUri.GetLeftPart(UriPartial.Path) + "?" + newQuery.ToString()).TrimEnd('?');
Logger.Debug("After: " + oriUrl); Logger.Debug("After: " + oriUrl);
} }
} }