Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update av parsing #679

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BBDown.Core/Util/HTTPUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ public static async Task<string> GetWebSourceAsync(string url)
return htmlCode;
}

// 重写重定向处理, 自动跟随多次重定向
public static async Task<string> 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<string> GetPostResponseAsync(string Url, byte[] postData)
{
LogDebug("Post to: {0}, data: {1}", Url, Convert.ToBase64String(postData));
Expand Down
76 changes: 14 additions & 62 deletions BBDown/BBDownUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand All @@ -47,15 +47,19 @@ public static async Task<string> 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"))
{
avid = AvRegex().Match(input).Groups[1].Value;
}
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/"))
{
Expand Down Expand Up @@ -138,7 +142,7 @@ public static async Task<string> 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
{
Expand Down Expand Up @@ -197,40 +201,15 @@ public static async Task<string> 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<string> 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<string> GetEpidBySSIdAsync(string ssid)
Expand Down Expand Up @@ -513,33 +492,6 @@ private static async Task<long> GetFileSizeAsync(string url)
return totalSizeBytes;
}

//重定向
public static async Task<string> 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();

Expand Down