Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
增加 DynamicRenderGrpc 的非浏览器渲染,用于在浏览器截图失败后重试
为 windows 的打包版添加了图标
  • Loading branch information
djkcyl committed Sep 18, 2022
1 parent 292946c commit 7a5f48d
Show file tree
Hide file tree
Showing 16 changed files with 1,094 additions and 282 deletions.
21 changes: 11 additions & 10 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@

app = Ariadne(app_config)
app.config(install_log=True)
app.launch_manager.add_service(
PlaywrightService(
user_data_dir=Path("data").joinpath("browser"),
device_scale_factor=2 if BotConfig.Bilibili.mobile_style else 1.25,
user_agent=(
"Mozilla/5.0 (Linux; Android 10; RMX1911) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/100.0.4896.127 Mobile Safari/537.36"
if BotConfig.Bilibili.use_browser:
app.launch_manager.add_service(
PlaywrightService(
user_data_dir=Path("data").joinpath("browser"),
device_scale_factor=2 if BotConfig.Bilibili.mobile_style else 1.25,
user_agent=(
"Mozilla/5.0 (Linux; Android 10; RMX1911) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/100.0.4896.127 Mobile Safari/537.36"
)
if BotConfig.Bilibili.mobile_style
else "",
)
if BotConfig.Bilibili.mobile_style
else "",
)
)
app.create(GraiaScheduler)
saya = it(Saya)

Expand Down
1 change: 1 addition & 0 deletions core/bot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class _Bilibili(BaseSettings, extra=Extra.ignore):
mobile_style: bool = True
concurrency: int = 5
use_login: bool = False
use_browser: bool = True

# 验证是否可以登录
@validator("use_login", always=True)
Expand Down
36 changes: 31 additions & 5 deletions data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
from typing import Union
from loguru import logger
from datetime import datetime
from peewee import (
Cast,
Model,
Expand All @@ -11,6 +12,7 @@
)


data_version = 2
db = SqliteDatabase("data/data.db")


Expand Down Expand Up @@ -64,7 +66,31 @@ class Meta:
table_name = "sub_list"


db.create_tables([DynamicPush, LivePush, SubList], safe=True)
class DataVersion(BaseModel):
"""数据库版本记录"""

version = IntegerField()

class Meta:
table_name = "data_version"


db.create_tables([DynamicPush, LivePush, SubList, DataVersion], safe=True)


if not DataVersion.select().exists():
logger.warning(f"数据库版本记录不存在,正在创建,当前版本:{data_version}")
DataVersion(version=2).save()
elif DataVersion.get().version != data_version:
logger.warning(f"数据库版本不匹配,当前版本:{data_version},数据库版本:{DataVersion.get().version},正在更新")
while DataVersion.get().version != data_version:
if DataVersion.get().version == 1:
logger.warning("当前数据版本为 1,正在更新至 2")
# 将 LivePush 表中的 statu 字段改为 status
db.execute_sql("ALTER TABLE live_push RENAME COLUMN statu TO status")
DataVersion.update(version=2).execute()

logger.success("数据库更新完成")


def insert_dynamic_push(
Expand All @@ -90,9 +116,9 @@ def insert_live_push(
uid: Union[str, int],
status: bool,
push_groups: int,
room_name: str = None,
room_area_parent: str = None,
room_area: str = None,
room_name: str = "",
room_area_parent: str = "",
room_area: str = "",
):
"""在直播推送表中插入一条记录"""
LivePush(
Expand Down
3 changes: 2 additions & 1 deletion data/bot_config.exp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ Debug:
enable: false # 是否开启 debug
groups: # 若开启 debug 则 **需要** 填入群号, list 和 int 均可
Bilibili:
use_browser: true # 是否使用浏览器截图,若这里为 false 则使用无头浏览器
mobile_style: true # 是否采用手机的 Web 样式进行截图,use_browser 为 true 时生效
use_login: false # 是否登录 BiliBili 账号
username: # 若登录 BiliBili 账号, 则 **需要** 填入账号
password: # 若登录 BiliBili 账号, 则 **需要** 填入密码
mobile_style: true # 是否采用手机的 Web 样式进行渲染
concurrency: 5 # 未登录时发送 gRPC 请求的并发数量(1 ~ 50)
Event:
mute: true # 是否向管理员发送被禁言的事件提醒。
Expand Down
34 changes: 27 additions & 7 deletions function/command/vive_dynamic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from graia.saya import Channel
from graia.ariadne.app import Ariadne
from graia.ariadne.model import Group
Expand All @@ -9,7 +11,9 @@
from graia.ariadne.message.parser.twilight import (
Twilight,
FullMatch,
ArgResult,
RegexResult,
ArgumentMatch,
WildcardMatch,
)

Expand All @@ -24,11 +28,21 @@
@channel.use(
ListenerSchema(
listening_events=[GroupMessage],
inline_dispatchers=[Twilight([FullMatch("查看动态"), "anything" @ WildcardMatch()])],
inline_dispatchers=[
Twilight(
[
FullMatch("查看动态"),
"test" @ ArgumentMatch("--test", action="store_true", optional=True),
"anything" @ WildcardMatch(),
]
)
],
decorators=[Permission.require(), Interval.require(20)],
)
)
async def main(app: Ariadne, group: Group, message: MessageChain, anything: RegexResult):
async def main(
app: Ariadne, group: Group, message: MessageChain, test: ArgResult, anything: RegexResult
):

if not (uid := await uid_extract(anything.result.display, group.id)):
return await app.send_group_message(
Expand All @@ -41,18 +55,24 @@ async def main(app: Ariadne, group: Group, message: MessageChain, anything: Rege
if res.list:
if len(res.list) > 1:
if res.list[0].modules[0].module_author.is_top:
dyn_id = res.list[1].extend.dyn_id_str
dyn = res.list[1]
else:
dyn_id = res.list[0].extend.dyn_id_str
dyn = res.list[0]
else:
dyn_id = res.list[0].extend.dyn_id_str
shot_image = await get_dynamic_screenshot(dyn_id)
dyn = res.list[0]
if test.result:
t1 = time.time()
shot_image = await get_dynamic_screenshot(dyn)
if test.result:
t2 = time.time()
shot_time = t2 - t1
return await app.send_group_message(
group,
MessageChain(
Image(data_bytes=shot_image),
"\n",
await get_b23_url(f"https://t.bilibili.com/{dyn_id}"),
await get_b23_url(f"https://t.bilibili.com/{dyn.extend.dyn_id_str}"),
f"\n测试耗时:{shot_time:.2f}秒" if test.result else "",
),
quote=message,
)
Expand Down
31 changes: 16 additions & 15 deletions function/event/bot_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ async def main(app: Ariadne):
"""
Graia 成功启动
"""
try:
logger.info("正在获取浏览器版本")
browser_context = app.launch_manager.get_interface(PlaywrightContext)
if not BotConfig.Bilibili.mobile_style:
await browser_context.context.add_cookies(
[{"name": "hit-dyn-v2", "value": "1", "domain": ".bilibili.com", "path": "/"}]
)

page = browser_context.context.pages[0]
version = await page.evaluate("() => navigator.appVersion")
logger.info(f"[BiliBili推送] 浏览器启动完成,当前版本 {version}")
await page.close()
except Exception as e:
logger.error(f"[BiliBili推送] 浏览器启动失败 {e}")
sys.exit(1)
if BotConfig.Bilibili.use_browser:
try:
logger.info("正在获取浏览器版本")
browser_context = app.launch_manager.get_interface(PlaywrightContext)
if not BotConfig.Bilibili.mobile_style:
await browser_context.context.add_cookies(
[{"name": "hit-dyn-v2", "value": "1", "domain": ".bilibili.com", "path": "/"}]
)

page = browser_context.context.pages[0]
version = await page.evaluate("() => navigator.appVersion")
logger.info(f"[BiliBili推送] 浏览器启动完成,当前版本 {version}")
await page.close()
except Exception as e:
logger.error(f"[BiliBili推送] 浏览器启动失败 {e}")
sys.exit(1)

logger.info("[BiliBili推送] 正在获取首页 Cookie")
await hc.get("https://bilibili.com/", follow_redirects=True)
Expand Down
2 changes: 1 addition & 1 deletion function/pusher/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async def push(app: Ariadne, dyn: DynamicItem):
await push(app, dynamic)

logger.debug(f"[Dynamic] Getting screenshot of {dynid}")
shot_image = await get_dynamic_screenshot(dynid)
shot_image = await get_dynamic_screenshot(dyn)

if shot_image:
logger.debug(f"[Dynamic] Get dynamic screenshot {dynid}")
Expand Down
2 changes: 1 addition & 1 deletion function/pusher/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ async def init(app: Ariadne):
elif (f := len(followed_list)) != 0:
if sys.argv.pop() != "--ignore-sub":
logger.critical(
f"[Bilibili推送] 该账号已关注 {f} 个用户,为避免产生问题,已停止运行,请先手动取消关注或添加启动参数 --ignore-sub"
f"[Bilibili推送] 该账号已关注 {f} 个用户,为避免产生问题,已停止运行,请先手动取消关注或添加启动参数 --ignore-sub,这将取关所有已关注的用户"
)
sys.exit(1)
else:
Expand Down
38 changes: 0 additions & 38 deletions library/browser.py

This file was deleted.

Loading

0 comments on commit 7a5f48d

Please sign in to comment.