[go: nahoru, domu]

Skip to content

Commit

Permalink
feat: 自定义 Credential Cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
z0z0r4 committed Feb 24, 2024
1 parent 89d089b commit 8294f14
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions bilibili_api/utils/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(
buvid3: Union[str, None] = None,
dedeuserid: Union[str, None] = None,
ac_time_value: Union[str, None] = None,
**kwargs
) -> None:
"""
各字段获取方式查看:https://nemo2011.github.io/bilibili-api/#/get-credential.md
Expand All @@ -43,6 +44,9 @@ def __init__(
dedeuserid (str | None, optional): 浏览器 Cookies 中的 DedeUserID 字段值. Defaults to None.
ac_time_value (str | None, optional): 浏览器 Cookies 中的 ac_time_value 字段值. Defaults to None.
其他 Cookie 参数可以直接填入,优先级低于上述参数。
"""
self.sessdata = (
None
Expand All @@ -56,6 +60,9 @@ def __init__(
self.dedeuserid = dedeuserid
self.ac_time_value = ac_time_value

for key, value in kwargs.items():
setattr(self, key, value)

def get_cookies(self) -> dict:
"""
获取请求 Cookies 字典
Expand All @@ -71,6 +78,11 @@ def get_cookies(self) -> dict:
}
if self.dedeuserid:
cookies.update({"DedeUserID": self.dedeuserid})

# 填入所有其他参数
for key, value in self.__dict__.items():
if key not in cookies and value is not None:
cookies[key] = value
return cookies

def has_dedeuserid(self) -> bool:
Expand Down
1 change: 1 addition & 0 deletions bilibili_api/utils/credential_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ async def refresh_cookies(credential: Credential) -> Credential:
bili_jct=resp.cookies["bili_jct"],
dedeuserid=resp.cookies["DedeUserID"],
ac_time_value=resp.json()["data"]["refresh_token"],
**credential.__dict__.items(),
)
await confirm_refresh(credential, new_credential)
return new_credential
Expand Down

0 comments on commit 8294f14

Please sign in to comment.