添加 cookie 时自动使用用户名命名(weibo)

This commit is contained in:
suyiiyii 2024-10-13 20:44:37 +08:00
parent b1b8d37171
commit f3d8b7d5bc
Signed by: suyiiyii
GPG Key ID: 044704CB29B8AD85

@ -11,8 +11,8 @@ from nonebot.log import logger
from bs4 import BeautifulSoup as bs
from ..post import Post
from ..utils import http_client
from .platform import NewMessage
from ..utils import http_client, text_fletten
from ..types import Tag, Target, RawPost, ApiError, Category
from ..utils.site import CookieSite, create_cookie_client_manager
@ -42,6 +42,22 @@ class WeiboSite(CookieSite):
schedule_setting = {"seconds": 3}
client_mgr = create_cookie_client_manager(name)
@classmethod
async def _get_current_user_name(cls, cookies: dict) -> str:
url = "https://m.weibo.cn/setup/nick/detail"
async with http_client() as client:
r = await client.get(url, headers=_HEADER, cookies=cookies)
data = json.loads(r.text)
name = data["data"]["user"]["screen_name"]
return name
@classmethod
async def get_cookie_name(cls, content: str) -> str:
"""从cookie内容中获取cookie的友好名字添加cookie时调用持久化在数据库中"""
name = await cls._get_current_user_name(json.loads(content))
return text_fletten(f"weibo: [{name[:10]}]")
class Weibo(NewMessage):
categories = {