支持 自动刷新 bili cookie

This commit is contained in:
xtaodada 2023-08-05 13:33:03 +08:00
parent 5ee854ad6c
commit e7f2a01554
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
5 changed files with 47 additions and 6 deletions

View File

@ -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`

View File

@ -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:

View File

@ -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"))

View File

@ -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()

View File

@ -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