🎨 playwright添加cookie参数

This commit is contained in:
HibiKier 2024-11-07 13:38:20 +08:00
parent 3f06131c34
commit b2da0a902d
3 changed files with 18 additions and 10 deletions

View File

@ -60,7 +60,7 @@
<img width="350" height="350" src="https://raw.githubusercontent.com/HibiKier/zhenxun_bot/main/docs_image/help.png"/> <img width="350" height="350" src="https://raw.githubusercontent.com/HibiKier/zhenxun_bot/main/docs_image/help.png"/>
<img width="250" height="500" src="https://raw.githubusercontent.com/HibiKier/zhenxun_bot/main/docs_image/html_help.png"/> <img width="250" height="500" src="https://raw.githubusercontent.com/HibiKier/zhenxun_bot/main/docs_image/html_help.png"/>
<img width="180" height="450" src="https://github.com/HibiKier/zhenxun_bot/blob/dev/docs_image/zhenxun_help.png"/> <img width="180" height="450" src="https://raw.githubusercontent.com/HibiKier/zhenxun_bot/main/docs_image/zhenxun_help.png"/>
</div> </div>

View File

@ -1,5 +1,5 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Literal, Tuple from typing import Literal
from tortoise import fields from tortoise import fields
from tortoise.functions import Count from tortoise.functions import Count
@ -27,7 +27,7 @@ class ChatHistory(Model):
platform = fields.CharField(255, null=True) platform = fields.CharField(255, null=True)
"""平台""" """平台"""
class Meta: class Meta: # type: ignore
table = "chat_history" table = "chat_history"
table_description = "聊天记录数据表" table_description = "聊天记录数据表"
@ -53,7 +53,7 @@ class ChatHistory(Model):
query = query.filter(create_time__range=date_scope) query = query.filter(create_time__range=date_scope)
return list( return list(
await query.annotate(count=Count("user_id")) await query.annotate(count=Count("user_id"))
.order_by(o + "count") .order_by(f"{o}count")
.group_by("user_id") .group_by("user_id")
.limit(limit) .limit(limit)
.values_list("user_id", "count") .values_list("user_id", "count")
@ -74,9 +74,7 @@ class ChatHistory(Model):
) )
else: else:
message = await cls.all().order_by("create_time").first() message = await cls.all().order_by("create_time").first()
if message: return message.create_time if message else None
return message.create_time
return None
@classmethod @classmethod
async def get_message( async def get_message(
@ -85,7 +83,7 @@ class ChatHistory(Model):
gid: str, gid: str,
type_: Literal["user", "group"], type_: Literal["user", "group"],
msg_type: Literal["private", "group"] | None = None, msg_type: Literal["private", "group"] | None = None,
days: int | Tuple[datetime, datetime] | None = None, days: int | tuple[datetime, datetime] | None = None,
) -> list[Self]: ) -> list[Self]:
"""获取消息查询query """获取消息查询query

View File

@ -431,14 +431,20 @@ class AsyncHttpx:
class AsyncPlaywright: class AsyncPlaywright:
@classmethod @classmethod
@asynccontextmanager @asynccontextmanager
async def new_page(cls, **kwargs) -> AsyncGenerator[Page, None]: async def new_page(
cls, cookies: list[dict[str, Any]] | dict[str, Any] | None = None, **kwargs
) -> AsyncGenerator[Page, None]:
"""获取一个新页面 """获取一个新页面
参数: 参数:
user_agent: 请求头 cookies: cookies
""" """
browser = await get_browser() browser = await get_browser()
ctx = await browser.new_context(**kwargs) ctx = await browser.new_context(**kwargs)
if cookies:
if isinstance(cookies, dict):
cookies = [cookies]
await ctx.add_cookies(cookies) # type: ignore
page = await ctx.new_page() page = await ctx.new_page()
try: try:
yield page yield page
@ -461,6 +467,7 @@ class AsyncPlaywright:
timeout: float | None = None, timeout: float | None = None,
type_: Literal["jpeg", "png"] | None = None, type_: Literal["jpeg", "png"] | None = None,
user_agent: str | None = None, user_agent: str | None = None,
cookies: list[dict[str, Any]] | dict[str, Any] | None = None,
**kwargs, **kwargs,
) -> UniMessage | None: ) -> UniMessage | None:
"""截图,该方法仅用于简单快捷截图,复杂截图请操作 page """截图,该方法仅用于简单快捷截图,复杂截图请操作 page
@ -474,6 +481,8 @@ class AsyncPlaywright:
wait_until: 等待类型 wait_until: 等待类型
timeout: 超时限制 timeout: 超时限制
type_: 保存类型 type_: 保存类型
user_agent: user_agent
cookies: cookies
""" """
if viewport_size is None: if viewport_size is None:
viewport_size = {"width": 2560, "height": 1080} viewport_size = {"width": 2560, "height": 1080}
@ -482,6 +491,7 @@ class AsyncPlaywright:
wait_time = wait_time * 1000 if wait_time else None wait_time = wait_time * 1000 if wait_time else None
element_list = [element] if isinstance(element, str) else element element_list = [element] if isinstance(element, str) else element
async with cls.new_page( async with cls.new_page(
cookies,
viewport=viewport_size, viewport=viewport_size,
user_agent=user_agent, user_agent=user_agent,
**kwargs, **kwargs,