From e7f2a01554898994c91516029eb59d7af53906cc Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sat, 5 Aug 2023 13:33:03 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=94=AF=E6=8C=81=20=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=B7=E6=96=B0=20bili=20cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- defs/bilibili.py | 26 ++++++++++++++++++++++++-- defs/cookie.py | 12 +++++++++++- defs/glover.py | 6 ++++-- modules/bilibili.py | 7 +++++++ requirements.txt | 2 +- 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/defs/bilibili.py b/defs/bilibili.py index cf9b536..bafaacb 100644 --- a/defs/bilibili.py +++ b/defs/bilibili.py @@ -13,7 +13,7 @@ from qrcode.image.pil import PilImage from io import BytesIO from PIL import Image, ImageDraw, ImageFont -from defs.cookie import get_bili_cookie, get_bili_browser_cookie +from defs.cookie import get_bili_cookie, get_bili_browser_cookie, set_bili_cookie from defs.browser import get_browser from init import request @@ -33,12 +33,34 @@ def from_cookie_get_credential() -> Optional[Credential]: dedeuserid = cookie["DedeUserID"] except KeyError: return None - return Credential(sessdata, bili_jct, buvid3, dedeuserid) + try: + ac_time_value = cookie["ac_time_value"] + except KeyError: + ac_time_value = None + return Credential(sessdata, bili_jct, buvid3, dedeuserid, ac_time_value) credential = from_cookie_get_credential() +def set_cookie_from_cred(new_cred: Credential) -> None: + cookie = get_bili_cookie() + cookie["SESSDATA"] = new_cred.sessdata + cookie["bili_jct"] = new_cred.bili_jct + cookie["buvid3"] = new_cred.buvid3 + cookie["ac_time_value"] = new_cred.ac_time_value + set_bili_cookie(cookie) + + +async def check_and_refresh_credential() -> None: + """ + 检查并刷新 Credential 对象。 + """ + if await credential.check_refresh(): + await credential.refresh() + set_cookie_from_cred(credential) + + def cut_text(old_str, cut): """ :说明: `get_cut_str` diff --git a/defs/cookie.py b/defs/cookie.py index d5bff5a..b73fdb3 100644 --- a/defs/cookie.py +++ b/defs/cookie.py @@ -1,9 +1,19 @@ from typing import Dict, List -from defs.glover import bili_cookie +from defs.glover import config, save_config + + +def set_bili_cookie(cookies: Dict[str, str]): + cookies_str = "" + for k, v in cookies.items(): + if k and v: + cookies_str += f"{k}={v};" + config.set("api", "bili_cookie", cookies_str) + save_config() def get_bili_cookie() -> Dict[str, str]: + bili_cookie = config.get("api", "bili_cookie", fallback="") data = {} for i in bili_cookie.split(";"): if i: diff --git a/defs/glover.py b/defs/glover.py index 43e4d64..354d824 100644 --- a/defs/glover.py +++ b/defs/glover.py @@ -15,7 +15,6 @@ splash_channel: int = 0 splash_channel_username: str = "" # [api] amap_key: str = "" -bili_cookie: str = "" config = RawConfigParser() config.read("config.ini") api_id = config.getint("pyrogram", "api_id", fallback=api_id) @@ -31,8 +30,11 @@ splash_channel_username = config.get( "post", "splash_channel_username", fallback=splash_channel_username ) amap_key = config.get("api", "amap_key", fallback=amap_key) -bili_cookie = config.get("api", "bili_cookie", fallback=bili_cookie) try: ipv6 = bool(strtobool(ipv6)) except ValueError: ipv6 = False + + +def save_config(): + config.write(open("config.ini", "w", encoding="utf-8")) diff --git a/modules/bilibili.py b/modules/bilibili.py index 97e6572..61be32f 100644 --- a/modules/bilibili.py +++ b/modules/bilibili.py @@ -9,9 +9,11 @@ from defs.bilibili import ( video_info_get, binfo_image_create, get_dynamic_screenshot_pc, + check_and_refresh_credential, ) from defs.button import gen_button, Button from init import bot +from scheduler import scheduler @bot.on_message( @@ -64,3 +66,8 @@ async def bili_dynamic(_: Client, message: Message): ), ) raise ContinuePropagation + + +@scheduler.scheduled_job("interval", hours=1, id="bili_cookie_refresh") +async def bili_cookie_refresh(): + await check_and_refresh_credential() diff --git a/requirements.txt b/requirements.txt index 0585554..312126e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ pyrogram==2.0.106 tgcrypto==1.2.5 -bilibili-api-python==15.4.4 +bilibili-api-python==15.5.3 httpx pillow cashews