mirror of
https://github.com/yaobiao131/downkyicore.git
synced 2025-08-10 00:52:31 +00:00
fix: 加入bvuid3和bvuid4可能解决无法解析的问题
This commit is contained in:
@@ -49,7 +49,7 @@ public static class UserInfo
|
||||
var query = WbiSign.ParametersToQuery(WbiSign.EncodeWbi(parameters));
|
||||
var url = $"https://api.bilibili.com/x/space/wbi/acc/info?{query}";
|
||||
const string referer = "https://www.bilibili.com";
|
||||
var response = WebClient.RequestWeb(url, referer, needRandomBvuid3: true);
|
||||
var response = WebClient.RequestWeb(url, referer);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -232,7 +232,7 @@ public static class VideoStream
|
||||
/// <returns></returns>
|
||||
private static PlayUrl GetPlayUrl(string url)
|
||||
{
|
||||
const string referer = "https://m.bilibili.com";
|
||||
const string referer = "https://www.bilibili.com";
|
||||
var response = WebClient.RequestWeb(url, referer);
|
||||
|
||||
try
|
||||
@@ -271,7 +271,7 @@ public static class VideoStream
|
||||
private static PlayUrl? GetPlayUrlWebPage(string url)
|
||||
{
|
||||
const string referer = "https://www.bilibili.com";
|
||||
var response = WebClient.RequestWeb(url, referer, needRandomBvuid3: true);
|
||||
var response = WebClient.RequestWeb(url, referer);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -4,23 +4,49 @@ using System.Text;
|
||||
using DownKyi.Core.BiliApi.Login;
|
||||
using DownKyi.Core.Logging;
|
||||
using DownKyi.Core.Settings;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DownKyi.Core.BiliApi;
|
||||
|
||||
internal static class WebClient
|
||||
{
|
||||
private static string GetRandomBuvid3()
|
||||
internal class SpiOrigin
|
||||
{
|
||||
// 随机生成10位字符串
|
||||
const string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
var random = new Random();
|
||||
var result = new StringBuilder();
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
result.Append(str[random.Next(str.Length)]);
|
||||
}
|
||||
[JsonProperty("data")] public Spi? Data { get; set; }
|
||||
public int Code { get; set; }
|
||||
public string? Message { get; set; }
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
internal class Spi
|
||||
{
|
||||
[JsonProperty("b_3")] public string? Bvuid3 { get; set; }
|
||||
[JsonProperty("b_4")] public string? Bvuid4 { get; set; }
|
||||
}
|
||||
|
||||
private static string? _bvuid3 = string.Empty;
|
||||
private static string? _bvuid4 = string.Empty;
|
||||
|
||||
// private static string GetRandomBuvid3()
|
||||
// {
|
||||
// // 随机生成10位字符串
|
||||
// const string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
// var random = new Random();
|
||||
// var result = new StringBuilder();
|
||||
// for (var i = 0; i < 10; i++)
|
||||
// {
|
||||
// result.Append(str[random.Next(str.Length)]);
|
||||
// }
|
||||
//
|
||||
// return result.ToString();
|
||||
// }
|
||||
|
||||
private static void GetBuvid()
|
||||
{
|
||||
const string url = "https://api.bilibili.com/x/frontend/finger/spi";
|
||||
var response = RequestWeb(url);
|
||||
var spi = JsonConvert.DeserializeObject<SpiOrigin>(response);
|
||||
_bvuid3 = spi?.Data?.Bvuid3;
|
||||
_bvuid4 = spi?.Data?.Bvuid4;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -32,8 +58,7 @@ internal static class WebClient
|
||||
/// <param name="parameters"></param>
|
||||
/// <param name="retry"></param>
|
||||
/// <returns></returns>
|
||||
public static string RequestWeb(string url, string? referer = null, string method = "GET",
|
||||
Dictionary<string, string>? parameters = null, int retry = 3, bool needRandomBvuid3 = false)
|
||||
public static string RequestWeb(string url, string? referer = null, string method = "GET", Dictionary<string, string>? parameters = null, int retry = 3)
|
||||
{
|
||||
// 重试次数
|
||||
if (retry <= 0)
|
||||
@@ -62,6 +87,11 @@ internal static class WebClient
|
||||
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(_bvuid3) && url != "https://api.bilibili.com/x/frontend/finger/spi")
|
||||
{
|
||||
GetBuvid();
|
||||
}
|
||||
|
||||
var request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.Method = method;
|
||||
request.Timeout = 30 * 1000;
|
||||
@@ -81,20 +111,19 @@ internal static class WebClient
|
||||
// 构造cookie
|
||||
if (!url.Contains("getLogin"))
|
||||
{
|
||||
request.Headers["origin"] = "https://m.bilibili.com";
|
||||
request.Headers["origin"] = "https://www.bilibili.com";
|
||||
|
||||
var cookies = LoginHelper.GetLoginInfoCookies();
|
||||
if (cookies != null)
|
||||
request.CookieContainer = cookies ?? new CookieContainer();
|
||||
|
||||
if (!string.IsNullOrEmpty(_bvuid3))
|
||||
{
|
||||
request.CookieContainer = cookies;
|
||||
request.CookieContainer.Add(new Cookie("buvid3", _bvuid3, "/", ".bilibili.com"));
|
||||
}
|
||||
else
|
||||
|
||||
if (!string.IsNullOrEmpty(_bvuid4))
|
||||
{
|
||||
request.CookieContainer = new CookieContainer();
|
||||
if (needRandomBvuid3)
|
||||
{
|
||||
request.CookieContainer.Add(new Cookie("buvid3", GetRandomBuvid3(), "/", ".bilibili.com"));
|
||||
}
|
||||
request.CookieContainer.Add(new Cookie("buvid4", _bvuid4, "/", ".bilibili.com"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user