Skip to content

Commit aa048b4

Browse files
committed
fix: 加入bvuid3和bvuid4可能解决无法解析的问题
1 parent f70694c commit aa048b4

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

DownKyi.Core/BiliApi/Users/UserInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static class UserInfo
4949
var query = WbiSign.ParametersToQuery(WbiSign.EncodeWbi(parameters));
5050
var url = $"https://api.bilibili.com/x/space/wbi/acc/info?{query}";
5151
const string referer = "https://www.bilibili.com";
52-
var response = WebClient.RequestWeb(url, referer, needRandomBvuid3: true);
52+
var response = WebClient.RequestWeb(url, referer);
5353

5454
try
5555
{

DownKyi.Core/BiliApi/VideoStream/VideoStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public static PlayUrl GetCheesePlayUrl(long avid, string bvid, long cid, long ep
232232
/// <returns></returns>
233233
private static PlayUrl GetPlayUrl(string url)
234234
{
235-
const string referer = "https://m.bilibili.com";
235+
const string referer = "https://www.bilibili.com";
236236
var response = WebClient.RequestWeb(url, referer);
237237

238238
try
@@ -271,7 +271,7 @@ private static PlayUrl GetPlayUrl(string url)
271271
private static PlayUrl? GetPlayUrlWebPage(string url)
272272
{
273273
const string referer = "https://www.bilibili.com";
274-
var response = WebClient.RequestWeb(url, referer, needRandomBvuid3: true);
274+
var response = WebClient.RequestWeb(url, referer);
275275

276276
try
277277
{

DownKyi.Core/BiliApi/WebClient.cs

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,49 @@
44
using DownKyi.Core.BiliApi.Login;
55
using DownKyi.Core.Logging;
66
using DownKyi.Core.Settings;
7+
using Newtonsoft.Json;
78

89
namespace DownKyi.Core.BiliApi;
910

1011
internal static class WebClient
1112
{
12-
private static string GetRandomBuvid3()
13+
internal class SpiOrigin
1314
{
14-
// 随机生成10位字符串
15-
const string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
16-
var random = new Random();
17-
var result = new StringBuilder();
18-
for (var i = 0; i < 10; i++)
19-
{
20-
result.Append(str[random.Next(str.Length)]);
21-
}
15+
[JsonProperty("data")] public Spi? Data { get; set; }
16+
public int Code { get; set; }
17+
public string? Message { get; set; }
18+
}
19+
20+
internal class Spi
21+
{
22+
[JsonProperty("b_3")] public string? Bvuid3 { get; set; }
23+
[JsonProperty("b_4")] public string? Bvuid4 { get; set; }
24+
}
2225

23-
return result.ToString();
26+
private static string? _bvuid3 = string.Empty;
27+
private static string? _bvuid4 = string.Empty;
28+
29+
// private static string GetRandomBuvid3()
30+
// {
31+
// // 随机生成10位字符串
32+
// const string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
33+
// var random = new Random();
34+
// var result = new StringBuilder();
35+
// for (var i = 0; i < 10; i++)
36+
// {
37+
// result.Append(str[random.Next(str.Length)]);
38+
// }
39+
//
40+
// return result.ToString();
41+
// }
42+
43+
private static void GetBuvid()
44+
{
45+
const string url = "https://api.bilibili.com/x/frontend/finger/spi";
46+
var response = RequestWeb(url);
47+
var spi = JsonConvert.DeserializeObject<SpiOrigin>(response);
48+
_bvuid3 = spi?.Data?.Bvuid3;
49+
_bvuid4 = spi?.Data?.Bvuid4;
2450
}
2551

2652
/// <summary>
@@ -32,8 +58,7 @@ private static string GetRandomBuvid3()
3258
/// <param name="parameters"></param>
3359
/// <param name="retry"></param>
3460
/// <returns></returns>
35-
public static string RequestWeb(string url, string? referer = null, string method = "GET",
36-
Dictionary<string, string>? parameters = null, int retry = 3, bool needRandomBvuid3 = false)
61+
public static string RequestWeb(string url, string? referer = null, string method = "GET", Dictionary<string, string>? parameters = null, int retry = 3)
3762
{
3863
// 重试次数
3964
if (retry <= 0)
@@ -62,6 +87,11 @@ public static string RequestWeb(string url, string? referer = null, string metho
6287

6388
try
6489
{
90+
if (string.IsNullOrEmpty(_bvuid3) && url != "https://api.bilibili.com/x/frontend/finger/spi")
91+
{
92+
GetBuvid();
93+
}
94+
6595
var request = (HttpWebRequest)WebRequest.Create(url);
6696
request.Method = method;
6797
request.Timeout = 30 * 1000;
@@ -81,20 +111,19 @@ public static string RequestWeb(string url, string? referer = null, string metho
81111
// 构造cookie
82112
if (!url.Contains("getLogin"))
83113
{
84-
request.Headers["origin"] = "https://m.bilibili.com";
114+
request.Headers["origin"] = "https://www.bilibili.com";
85115

86116
var cookies = LoginHelper.GetLoginInfoCookies();
87-
if (cookies != null)
117+
request.CookieContainer = cookies ?? new CookieContainer();
118+
119+
if (!string.IsNullOrEmpty(_bvuid3))
88120
{
89-
request.CookieContainer = cookies;
121+
request.CookieContainer.Add(new Cookie("buvid3", _bvuid3, "/", ".bilibili.com"));
90122
}
91-
else
123+
124+
if (!string.IsNullOrEmpty(_bvuid4))
92125
{
93-
request.CookieContainer = new CookieContainer();
94-
if (needRandomBvuid3)
95-
{
96-
request.CookieContainer.Add(new Cookie("buvid3", GetRandomBuvid3(), "/", ".bilibili.com"));
97-
}
126+
request.CookieContainer.Add(new Cookie("buvid4", _bvuid4, "/", ".bilibili.com"));
98127
}
99128
}
100129

0 commit comments

Comments
 (0)