-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 更换获取用户信息的Api,避免风控 * Fix: 调整 wbi 参数获取为异步方法 - 调整 wbi 参数获取为异步方法 --------- Co-authored-by: Ailitonia <41713304+Ailitonia@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from functools import reduce | ||
from hashlib import md5 | ||
import urllib.parse | ||
import time | ||
|
||
from omega_miya.web_resource import HttpFetcher | ||
|
||
from .exception import BilibiliApiError | ||
|
||
|
||
mixinKeyEncTab = [ | ||
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 | ||
] | ||
|
||
|
||
def get_mixin_key(orig: str): | ||
"""对 imgKey 和 subKey 进行字符顺序打乱编码""" | ||
return reduce(lambda s, i: s + orig[i], mixinKeyEncTab, '')[:32] | ||
|
||
|
||
def enc_wbi(params: dict, img_key: str, sub_key: str): | ||
"""为请求参数进行 wbi 签名""" | ||
mixin_key = get_mixin_key(img_key + sub_key) | ||
curr_time = round(time.time()) | ||
params['wts'] = curr_time # 添加 wts 字段 | ||
params = dict(sorted(params.items())) # 按照 key 重排参数 | ||
# 过滤 value 中的 "!'()*" 字符 | ||
params = { | ||
k: ''.join(filter(lambda x: x not in "!'()*", str(v))) | ||
for k, v | ||
in params.items() | ||
} | ||
query = urllib.parse.urlencode(params) # 序列化参数 | ||
wbi_sign = md5((query + mixin_key).encode()).hexdigest() # 计算 w_rid | ||
params['w_rid'] = wbi_sign | ||
return params | ||
|
||
|
||
async def get_wbi_keys() -> tuple[str, str]: | ||
"""获取最新的 img_key 和 sub_key""" | ||
resp = await HttpFetcher().get_json_dict('https://api.bilibili.com/x/web-interface/nav') | ||
if resp.status != 200: | ||
raise BilibiliApiError(f'BilibiliApiError, {resp.result}') | ||
|
||
json_content = resp.result | ||
img_url: str = json_content['data']['wbi_img']['img_url'] | ||
sub_url: str = json_content['data']['wbi_img']['sub_url'] | ||
img_key = img_url.rsplit('/', 1)[1].split('.')[0] | ||
sub_key = sub_url.rsplit('/', 1)[1].split('.')[0] | ||
return img_key, sub_key | ||
|
||
|
||
async def transform_params(params: dict): | ||
img_key, sub_key = await get_wbi_keys() | ||
signed_params = enc_wbi( | ||
params=params, | ||
img_key=img_key, | ||
sub_key=sub_key | ||
) | ||
return signed_params | ||
|
||
|
||
__all__ = [ | ||
'transform_params' | ||
] |