支持wbi口令

pull/896/head
leiurayer 1 year ago
parent eaa962aca1
commit a3579fbb28

@ -0,0 +1,120 @@
using DownKyi.Core.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace DownKyi.Core.BiliApi.Sign
{
public static class WbiSign
{
/// <summary>
/// 打乱重排实时口令
/// </summary>
/// <param name="origin"></param>
/// <returns></returns>
private static string GetMixinKey(string origin)
{
int[] mixinKeyEncTab = new int[]
{
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49,
33, 9, 42, 19, 29, 28, 14, 39,12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40,
61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11,
36, 20, 34, 44, 52
};
var temp = new StringBuilder();
foreach (var i in mixinKeyEncTab)
{
temp.Append(origin[i]);
}
return temp.ToString().Substring(0, 32);
}
/// <summary>
/// 将字典参数转为字符串
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
public static string ParametersToQuery(Dictionary<string, object> parameters)
{
var keys = parameters.Keys.ToList();
var queryList = new List<string>();
foreach (var item in keys)
{
var value = parameters[item];
queryList.Add($"{item}={value}");
}
return string.Join("&", queryList);
}
/// <summary>
/// Wbi签名返回所有参数字典
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
public static Dictionary<string, object> EncodeWbi(Dictionary<string, object> parameters)
{
return EncodeWbi(parameters, GetKey().Item1, GetKey().Item2);
}
/// <summary>
/// Wbi签名返回所有参数字典
/// </summary>
/// <param name="parameters"></param>
/// <param name="imgKey"></param>
/// <param name="subKey"></param>
/// <returns></returns>
public static Dictionary<string, object> EncodeWbi(Dictionary<string, object> parameters, string imgKey, string subKey)
{
var mixinKey = GetMixinKey(imgKey + subKey);
var chrFilter = new Regex("[!'()*]");
var newParameters = new Dictionary<string, object>
{
{ "wts", (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds }
};
foreach (var para in parameters)
{
var key = para.Key;
var value = para.Value.ToString();
var encodedValue = chrFilter.Replace(value, "");
newParameters.Add(Uri.EscapeDataString(key), Uri.EscapeDataString(encodedValue));
}
var keys = newParameters.Keys.ToList();
keys.Sort();
var queryList = new List<string>();
foreach (var item in keys)
{
var value = newParameters[item];
queryList.Add($"{item}={value}");
}
var queryString = string.Join("&", queryList);
var md5Hasher = MD5.Create();
var hashStr = queryString + mixinKey;
var hashedQueryString = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(hashStr));
var wbiSign = BitConverter.ToString(hashedQueryString).Replace("-", "").ToLower();
newParameters.Add("w_rid", wbiSign);
return newParameters;
}
public static Tuple<string, string> GetKey()
{
var user = SettingsManager.GetInstance().GetUserInfo();
return new Tuple<string, string>(user.ImgKey, user.SubKey);
}
}
}

@ -53,6 +53,9 @@ namespace DownKyi.Core.BiliApi.Users.Models
//public int vip_theme_type { get; set; }
[JsonProperty("wallet")]
public UserInfoWallet Wallet { get; set; }
[JsonProperty("wbi_img")]
public Wbi Wbi { get; set; }
}
//public class NavDataLevelInfo
@ -105,4 +108,14 @@ namespace DownKyi.Core.BiliApi.Users.Models
[JsonProperty("mid")]
public long Mid { get; set; }
}
[JsonObject]
public class Wbi
{
[JsonProperty("img_url")]
public string ImgUrl { get; set; }
[JsonProperty("sub_url")]
public string SubUrl { get; set; }
}
}

@ -1,7 +1,9 @@
using DownKyi.Core.BiliApi.Users.Models;
using DownKyi.Core.BiliApi.Sign;
using DownKyi.Core.BiliApi.Users.Models;
using DownKyi.Core.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace DownKyi.Core.BiliApi.Users
{
@ -43,7 +45,12 @@ namespace DownKyi.Core.BiliApi.Users
/// <returns></returns>
public static UserInfoForSpace GetUserInfoForSpace(long mid)
{
string url = $"https://api.bilibili.com/x/space/wbi/acc/info?mid={mid}";
var parameters = new Dictionary<string, object>
{
{ "mid", mid }
};
string query = WbiSign.ParametersToQuery(WbiSign.EncodeWbi(parameters));
string url = $"https://api.bilibili.com/x/space/wbi/acc/info?{query}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);

@ -1,4 +1,5 @@
using DownKyi.Core.BiliApi.Users.Models;
using DownKyi.Core.BiliApi.Sign;
using DownKyi.Core.BiliApi.Users.Models;
using DownKyi.Core.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -114,7 +115,17 @@ namespace DownKyi.Core.BiliApi.Users
/// <returns></returns>
public static SpacePublicationList GetPublication(long mid, int pn, int ps, long tid = 0, PublicationOrder order = PublicationOrder.PUBDATE, string keyword = "")
{
string url = $"https://api.bilibili.com/x/space/wbi/arc/search?mid={mid}&pn={pn}&ps={ps}&order={order.ToString("G").ToLower()}&tid={tid}&keyword={keyword}";
var parameters = new Dictionary<string, object>
{
{ "mid", mid },
{ "pn", pn },
{ "ps", ps },
{ "order", order.ToString("G").ToLower() },
{ "tid", tid },
{ "keyword", keyword },
};
string query = WbiSign.ParametersToQuery(WbiSign.EncodeWbi(parameters));
string url = $"https://api.bilibili.com/x/space/wbi/arc/search?{query}";
string referer = "https://www.bilibili.com";
string response = WebClient.RequestWeb(url, referer);

@ -186,6 +186,7 @@
<Compile Include="BiliApi\Login\LoginQR.cs" />
<Compile Include="BiliApi\Login\Models\LoginStatus.cs" />
<Compile Include="BiliApi\Login\Models\LoginUrl.cs" />
<Compile Include="BiliApi\Sign\WbiSign.cs" />
<Compile Include="BiliApi\Users\Models\BangumiFollow.cs" />
<Compile Include="BiliApi\Users\Models\BangumiFollowAreas.cs" />
<Compile Include="BiliApi\Users\Models\BangumiFollowNewEp.cs" />

@ -6,5 +6,8 @@
public string Name { get; set; }
public bool IsLogin { get; set; } // 是否登录
public bool IsVip { get; set; } // 是否为大会员未登录时为false
public string ImgKey { get; set; }
public string SubKey { get; set; }
}
}

@ -200,7 +200,7 @@
<system:String x:Key="UseSSL">启用https若下载器提示SSL错误则关闭此项</system:String>
<system:String x:Key="UserAgent">UserAgent</system:String>
<system:String x:Key="SelectDownloader">选择下载器(重启生效):</system:String>
<system:String x:Key="BuiltinDownloader">内建下载器(测试)</system:String>
<system:String x:Key="BuiltinDownloader">内建下载器</system:String>
<system:String x:Key="Aria2cDownloader">Aria2下载器</system:String>
<system:String x:Key="CustomAria2cDownloader">自定义Aria2下载器</system:String>
<system:String x:Key="AriaServerHost">Aria服务器地址</system:String>

@ -20,14 +20,13 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace DownKyi.Services.Download
{
public abstract class DownloadService
{
protected string Tag = "DownloadService";
protected TaskbarIcon _notifyIcon;
protected TaskbarIcon _notifyIcon;
protected IDialogService dialogService;
protected ObservableCollection<DownloadingItem> downloadingList;
protected ObservableCollection<DownloadedItem> downloadedList;

@ -1,4 +1,5 @@
using DownKyi.Core.BiliApi.Users;
using DownKyi.Core.BiliApi.Users.Models;
using DownKyi.Core.Logging;
using DownKyi.Core.Settings;
using DownKyi.Core.Settings.Models;
@ -12,6 +13,7 @@ using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
@ -227,6 +229,36 @@ namespace DownKyi.ViewModels
InputText = string.Empty;
}
private UserInfoForNavigation GetUserInfo()
{
// 获取用户信息
var userInfo = UserInfo.GetUserInfoForNavigation();
if (userInfo != null)
{
SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings
{
Mid = userInfo.Mid,
Name = userInfo.Name,
IsLogin = userInfo.IsLogin,
IsVip = userInfo.VipStatus == 1,
ImgKey = userInfo.Wbi.ImgUrl.Split('/').ToList().Last().Split('.')[0],
SubKey = userInfo.Wbi.SubUrl.Split('/').ToList().Last().Split('.')[0],
});
}
else
{
SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings
{
Mid = -1,
Name = "",
IsLogin = false,
IsVip = false,
});
}
return userInfo;
}
/// <summary>
/// 更新用户登录信息
/// </summary>
@ -248,27 +280,7 @@ namespace DownKyi.ViewModels
await Task.Run(new Action(() =>
{
// 获取用户信息
var userInfo = UserInfo.GetUserInfoForNavigation();
if (userInfo != null)
{
SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings
{
Mid = userInfo.Mid,
Name = userInfo.Name,
IsLogin = userInfo.IsLogin,
IsVip = userInfo.VipStatus == 1
});
}
else
{
SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings
{
Mid = -1,
Name = "",
IsLogin = false,
IsVip = false
});
}
var userInfo = GetUserInfo();
PropertyChangeAsync(new Action(() =>
{
@ -318,21 +330,27 @@ namespace DownKyi.ViewModels
{
return;
}
// 启动
if (parameter == "start")
{
UpdateUserInfo();
}
// 从登录页面返回
if (parameter == "login")
else if (parameter == "login")
{
UpdateUserInfo();
}
// 注销
if (parameter == "logout")
else if (parameter == "logout")
{
UpdateUserInfo();
}
// 其他情况只更新设置的用户信息不更新UI
else
{
GetUserInfo();
}
}

Loading…
Cancel
Save