From 4d469343a3e1f4a0b8122a12ee4b7e041e0bbb91 Mon Sep 17 00:00:00 2001 From: My-Responsitories Date: Thu, 17 Aug 2023 11:34:50 +0800 Subject: [PATCH] update av parsing --- BBDown.Core/Util/HTTPUtil.cs | 16 ++++++++ BBDown/BBDownUtil.cs | 76 +++++++----------------------------- 2 files changed, 30 insertions(+), 62 deletions(-) diff --git a/BBDown.Core/Util/HTTPUtil.cs b/BBDown.Core/Util/HTTPUtil.cs index 4eb2f5ab3..c80e953b2 100644 --- a/BBDown.Core/Util/HTTPUtil.cs +++ b/BBDown.Core/Util/HTTPUtil.cs @@ -54,6 +54,22 @@ public static async Task GetWebSourceAsync(string url) return htmlCode; } + // 重写重定向处理, 自动跟随多次重定向 + public static async Task GetWebLocationAsync(string url) + { + using var webRequest = new HttpRequestMessage(HttpMethod.Head, url); + webRequest.Headers.TryAddWithoutValidation("User-Agent", UserAgent); + webRequest.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate"); + webRequest.Headers.CacheControl = CacheControlHeaderValue.Parse("no-cache"); + webRequest.Headers.Connection.Clear(); + + LogDebug("获取网页重定向地址: Url: {0}, Headers: {1}", url, webRequest.Headers); + var webResponse = (await AppHttpClient.SendAsync(webRequest, HttpCompletionOption.ResponseHeadersRead)).EnsureSuccessStatusCode(); + string location = webResponse.RequestMessage.RequestUri.AbsoluteUri; + LogDebug("Location: {0}", location); + return location; + } + public static async Task GetPostResponseAsync(string Url, byte[] postData) { LogDebug("Post to: {0}, data: {1}", Url, Convert.ToBase64String(postData)); diff --git a/BBDown/BBDownUtil.cs b/BBDown/BBDownUtil.cs index 6ed69f64f..1341ddae2 100644 --- a/BBDown/BBDownUtil.cs +++ b/BBDown/BBDownUtil.cs @@ -28,7 +28,7 @@ public static async Task CheckUpdateAsync() { var ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version!; string nowVer = $"{ver.Major}.{ver.Minor}.{ver.Build}"; - string redirctUrl = await Get302("https://github.com/nilaoda/BBDown/releases/latest"); + string redirctUrl = await GetWebLocationAsync("https://github.com/nilaoda/BBDown/releases/latest"); string latestVer = redirctUrl.Replace("https://github.com/nilaoda/BBDown/releases/tag/", ""); if (nowVer != latestVer && !latestVer.StartsWith("https")) { @@ -47,7 +47,11 @@ public static async Task GetAvIdAsync(string input) if (input.StartsWith("http")) { if (input.Contains("b23.tv")) - input = await Get302(input); + { + string tmp = await GetWebLocationAsync(input); + if (tmp == input) throw new Exception("无限重定向"); + input = tmp; + } Match match; if (input.Contains("video/av")) { @@ -55,7 +59,7 @@ public static async Task GetAvIdAsync(string input) } else if (input.ToLower().Contains("video/bv")) { - avid = await GetAidByBVAsync(BVRegex().Match(input).Groups[1].Value); + avid = GetAidByBV(BVRegex().Match(input).Groups[1].Value); } else if (input.Contains("/cheese/")) { @@ -138,7 +142,7 @@ public static async Task GetAvIdAsync(string input) } else if (input.ToLower().StartsWith("bv")) { - avid = await GetAidByBVAsync(input[2..]); + avid = GetAidByBV(input[2..]); } else if (input.ToLower().StartsWith("av")) //av { @@ -197,40 +201,15 @@ public static async Task FixAvidAsync(string avid) { if (!NumRegex().IsMatch(avid)) return avid; - string api = $"https://api.bilibili.com/x/web-interface/archive/stat?aid={avid}"; - string json = await GetWebSourceAsync(api); - using var jDoc = JsonDocument.Parse(json); - bool copyRight = jDoc.RootElement.GetProperty("data").GetProperty("copyright").GetInt32() == 2; - if (copyRight) - { - api = $"https://api.bilibili.com/x/web-interface/view?aid={avid}"; - json = await GetWebSourceAsync(api); - using var infoJson = JsonDocument.Parse(json); - var data = infoJson.RootElement.GetProperty("data"); - if (data.TryGetProperty("redirect_url", out _) && data.GetProperty("redirect_url").ToString().Contains("bangumi")) - { - var epId = RedirectRegex().Match(data.GetProperty("redirect_url").ToString()).Groups[1].Value; - return $"ep:{epId}"; - } - } - return avid; + string api = $"https://www.bilibili.com/video/av{avid}/"; + string location = await GetWebLocationAsync(api); + return location.Contains("/video/") ? avid : $"ep:{RedirectRegex().Match(location).Groups[1].Value}"; } - public static async Task GetAidByBVAsync(string bv) + public static string GetAidByBV(string bv) { - if (bv.Length == 10) - { - // 能在本地就在本地 - return Core.Util.BilibiliBvConverter.Decode(bv).ToString(); - } - else - { - string api = $"https://api.bilibili.com/x/web-interface/archive/stat?bvid={bv}"; - string json = await GetWebSourceAsync(api); - using var jDoc = JsonDocument.Parse(json); - string aid = jDoc.RootElement.GetProperty("data").GetProperty("aid").ToString(); - return aid; - } + // 能在本地就在本地 + return Core.Util.BilibiliBvConverter.Decode(bv).ToString(); } public static async Task GetEpidBySSIdAsync(string ssid) @@ -513,33 +492,6 @@ private static async Task GetFileSizeAsync(string url) return totalSizeBytes; } - //重定向 - public static async Task Get302(string url) - { - //this allows you to set the settings so that we can get the redirect url - var handler = new HttpClientHandler() - { - AllowAutoRedirect = false - }; - string redirectedUrl = ""; - using (HttpClient client = new(handler)) - using (HttpResponseMessage response = await client.GetAsync(url)) - using (HttpContent content = response.Content) - { - // ... Read the response to see if we have the redirected url - if (response.StatusCode == System.Net.HttpStatusCode.Found) - { - HttpResponseHeaders headers = response.Headers; - if (headers != null && headers.Location != null) - { - redirectedUrl = headers.Location.AbsoluteUri; - } - } - } - - return redirectedUrl; - } - private static char[] InvalidChars = "34,60,62,124,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,58,42,63,92,47" .Split(',').Select(s => (char)int.Parse(s)).ToArray();