Skip to content

Commit

Permalink
🎨 playwright添加cookie参数
Browse files Browse the repository at this point in the history
  • Loading branch information
HibiKier committed Nov 7, 2024
1 parent 3f06131 commit b2da0a9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

<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="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>

Expand Down
12 changes: 5 additions & 7 deletions zhenxun/models/chat_history.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from typing import Literal, Tuple
from typing import Literal

from tortoise import fields
from tortoise.functions import Count
Expand Down Expand Up @@ -27,7 +27,7 @@ class ChatHistory(Model):
platform = fields.CharField(255, null=True)
"""平台"""

class Meta:
class Meta: # type: ignore
table = "chat_history"
table_description = "聊天记录数据表"

Expand All @@ -53,7 +53,7 @@ async def get_group_msg_rank(
query = query.filter(create_time__range=date_scope)
return list(
await query.annotate(count=Count("user_id"))
.order_by(o + "count")
.order_by(f"{o}count")
.group_by("user_id")
.limit(limit)
.values_list("user_id", "count")
Expand All @@ -74,9 +74,7 @@ async def get_group_first_msg_datetime(
)
else:
message = await cls.all().order_by("create_time").first()
if message:
return message.create_time
return None
return message.create_time if message else None

@classmethod
async def get_message(
Expand All @@ -85,7 +83,7 @@ async def get_message(
gid: str,
type_: Literal["user", "group"],
msg_type: Literal["private", "group"] | None = None,
days: int | Tuple[datetime, datetime] | None = None,
days: int | tuple[datetime, datetime] | None = None,
) -> list[Self]:
"""获取消息查询query
Expand Down
14 changes: 12 additions & 2 deletions zhenxun/utils/http_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,20 @@ async def head_mirror(client: type[AsyncHttpx], url: str) -> dict[str, Any]:
class AsyncPlaywright:
@classmethod
@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()
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()
try:
yield page
Expand All @@ -461,6 +467,7 @@ async def screenshot(
timeout: float | None = None,
type_: Literal["jpeg", "png"] | None = None,
user_agent: str | None = None,
cookies: list[dict[str, Any]] | dict[str, Any] | None = None,
**kwargs,
) -> UniMessage | None:
"""截图,该方法仅用于简单快捷截图,复杂截图请操作 page
Expand All @@ -474,6 +481,8 @@ async def screenshot(
wait_until: 等待类型
timeout: 超时限制
type_: 保存类型
user_agent: user_agent
cookies: cookies
"""
if viewport_size is None:
viewport_size = {"width": 2560, "height": 1080}
Expand All @@ -482,6 +491,7 @@ async def screenshot(
wait_time = wait_time * 1000 if wait_time else None
element_list = [element] if isinstance(element, str) else element
async with cls.new_page(
cookies,
viewport=viewport_size,
user_agent=user_agent,
**kwargs,
Expand Down

0 comments on commit b2da0a9

Please sign in to comment.