Skip to content

Commit 6d45457

Browse files
authored
Merge pull request #411 from SK-415/dev
post5
2 parents 51dc6c3 + 213ff9b commit 6d45457

12 files changed

Lines changed: 124 additions & 206 deletions

File tree

docs/usage/settings.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ HARUKA_SCREENSHOT_STYLE=pc
128128
HARUKA_CAPTCHA_ADDRESS=https://captcha-cd.ngworks.cn
129129
```
130130

131+
## HARUKA_CAPTCHA_TOKEN
132+
133+
默认值:harukabot
134+
135+
验证码 Token,用于验证码服务器鉴权,若不填写一天内只能使用 5 次。
136+
137+
```yml
138+
HARUKA_CAPTCHA_TOKEN=harukabot
139+
```
140+
131141
## HARUKA_BROWSER_UA
132142

133143
默认值:""

haruka_bot/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Config(BaseSettings):
1919
haruka_dynamic_at: bool = False
2020
haruka_screenshot_style: str = "mobile"
2121
haruka_captcha_address: str = "https://captcha-cd.ngworks.cn"
22+
haruka_captcha_token: str = "harukabot"
2223
haruka_browser_ua: Optional[str] = None
2324
haruka_dynamic_timeout: int = 30
2425
haruka_dynamic_font_source: str = "system"

haruka_bot/plugins/pusher/dynamic_pusher.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
EVENT_JOB_MISSED,
88
EVENT_SCHEDULER_STARTED,
99
)
10+
from bilireq.exceptions import GrpcError
1011
from bilireq.grpc.dynamic import grpc_get_user_dynamics
1112
from bilireq.grpc.protos.bilibili.app.dynamic.v2.dynamic_pb2 import DynamicType
1213
from grpc import StatusCode
@@ -44,8 +45,12 @@ async def dy_sched():
4445
except AioRpcError as e:
4546
if e.code() == StatusCode.DEADLINE_EXCEEDED:
4647
logger.error(f"爬取动态超时,将在下个轮询中重试:{e.code()} {e.details()}")
47-
return
48-
raise
48+
else:
49+
logger.error(f"爬取动态失败:{e.code()} {e.details()}")
50+
return
51+
except GrpcError as e:
52+
logger.error(f"爬取动态失败:{e.code} {e.msg}")
53+
return
4954

5055
if not dynamics: # 没发过动态
5156
if uid in offset and offset[uid] == -1: # 不记录会导致第一次发动态不推送
@@ -96,8 +101,9 @@ async def dy_sched():
96101
}
97102
message = (
98103
f"{name} {type_msg.get(dynamic.card_type, type_msg[0])}\n"
99-
f"{f'动态图片可能截图异常:{err}' if err else ''}\n"
100-
f"{MessageSegment.image(image)}\n{url}"
104+
+ str(f"动态图片可能截图异常:{err}\n" if err else "")
105+
+ MessageSegment.image(image)
106+
+ f"\n{url}"
101107
)
102108

103109
push_list = await db.get_push_list(uid, "dynamic")

haruka_bot/plugins/pusher/live_pusher.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"interval", seconds=plugin_config.haruka_live_interval, id="live_sched"
1717
)
1818
async def live_sched():
19+
# sourcery skip: use-fstring-for-concatenation
1920
"""直播推送"""
2021
uids = await db.get_uid_list("live")
2122

@@ -42,20 +43,22 @@ async def live_sched():
4243
url = f"https://live.bilibili.com/{room_id}"
4344
title = info["title"]
4445
cover = info["cover_from_user"] or info["keyframe"]
45-
area_parent = info["area_v2_parent_name"]
4646
area = info["area_v2_name"]
47+
area_parent = info["area_v2_parent_name"]
4748
room_area = f"{area_parent} / {area}"
4849
logger.info(f"检测到开播:{name}{uid})")
4950
live_msg = (
50-
f"{name} 开播啦!\n分区:{room_area}\n标题:{title}\n{MessageSegment.image(cover)}\n{url}"
51+
f"{name} 开播啦!\n分区:{room_area}\n标题:{title}\n"
52+
+ MessageSegment.image(cover)
53+
+ f"\n{url}"
5154
)
5255
else: # 下播
5356
logger.info(f"检测到下播:{name}{uid})")
5457
if not plugin_config.haruka_live_off_notify: # 没开下播推送
5558
continue
5659
live_time_msg = (
57-
f",本次直播时长 {calc_time_total(time.time() - live_time[uid])}。"
58-
if live_time[uid]
60+
f"\n本次直播时长 {calc_time_total(time.time() - live_time[uid])}。"
61+
if live_time.get(uid)
5962
else "。"
6063
)
6164
live_msg = f"{name} 下播了{live_time_msg}"

haruka_bot/plugins/sub/sub_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def _(event: MessageEvent, bot: Bot):
1919
user = await db.get_user(uid=sub.uid)
2020
assert user is not None
2121
message += (
22-
f"{user.name}{user.uid})"
22+
f"{user.name}{user.uid}\n"
2323
f"直播:{'开' if sub.live else '关'},"
2424
f"动态:{'开' if sub.dynamic else '关'},"
2525
# TODO 私聊不显示全体

haruka_bot/utils/browser.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
from typing import Optional
88

99
from nonebot.log import logger
10+
from aunly_captcha_solver import CaptchaInfer
1011
from playwright.__main__ import main
1112
from playwright.async_api import BrowserContext, async_playwright, Page
1213

1314
from ..config import plugin_config
1415
from .fonts_provider import fill_font
15-
from .captcha import resolve_captcha
1616
from ..utils import get_path
1717

1818
_browser: Optional[BrowserContext] = None
1919
mobile_js = Path(__file__).parent.joinpath("mobile.js")
2020

2121

2222
async def init_browser(proxy=plugin_config.haruka_proxy, **kwargs) -> BrowserContext:
23+
logger.info("初始化浏览器")
2324
if proxy:
2425
kwargs["proxy"] = {"server": proxy}
2526
global _browser
@@ -38,6 +39,7 @@ async def init_browser(proxy=plugin_config.haruka_proxy, **kwargs) -> BrowserCon
3839
else None
3940
),
4041
device_scale_factor=2,
42+
timeout=plugin_config.haruka_dynamic_timeout * 1000,
4143
**kwargs,
4244
)
4345
if plugin_config.haruka_screenshot_style.lower() != "mobile":
@@ -57,7 +59,7 @@ async def init_browser(proxy=plugin_config.haruka_proxy, **kwargs) -> BrowserCon
5759

5860
async def get_browser() -> BrowserContext:
5961
global _browser
60-
if not _browser or _browser.browser is None or not _browser.browser.is_connected():
62+
if not _browser:
6163
_browser = await init_browser()
6264
return _browser
6365

@@ -78,7 +80,7 @@ async def get_dynamic_screenshot(dynamic_id, style=plugin_config.haruka_screensh
7880
clip["height"] = min(clip["height"], 32766)
7981
return (
8082
await page.screenshot(clip=clip, full_page=True, type="jpeg", quality=98),
81-
err,
83+
None,
8284
)
8385
except TimeoutError:
8486
logger.warning(f"截图超时,重试 {i + 1}/3")
@@ -115,13 +117,12 @@ async def get_dynamic_screenshot_mobile(dynamic_id, page: Page):
115117
await page.set_viewport_size({"width": 460, "height": 780})
116118
await page.route(re.compile("^https://static.graiax/fonts/(.+)$"), fill_font)
117119
if plugin_config.haruka_captcha_address:
118-
page = await resolve_captcha(url, page)
119-
else:
120-
await page.goto(
121-
url,
122-
wait_until="networkidle",
123-
timeout=plugin_config.haruka_dynamic_timeout * 1000,
120+
captcha = CaptchaInfer(
121+
plugin_config.haruka_captcha_address, plugin_config.haruka_captcha_token
124122
)
123+
page = await captcha.solve_captcha(page, url)
124+
else:
125+
await page.goto(url, wait_until="networkidle")
125126
# 动态被删除或者进审核了
126127
if page.url == "https://m.bilibili.com/404":
127128
raise Notfound
@@ -137,11 +138,8 @@ async def get_dynamic_screenshot_mobile(dynamic_id, page: Page):
137138
# "dyn.style.overflowWrap='break-word'"
138139
# )
139140

140-
await page.wait_for_load_state(state="domcontentloaded", timeout=20000)
141-
if "opus" in page.url:
142-
await page.wait_for_selector(".opus-module-author", state="visible")
143-
else:
144-
await page.wait_for_selector(".dyn-header__author__face", state="visible")
141+
await page.wait_for_load_state(state="domcontentloaded")
142+
await page.wait_for_selector(".b-img__inner, .dyn-header__author__face", state="visible")
145143

146144
await page.add_script_tag(path=mobile_js)
147145

@@ -159,7 +157,7 @@ async def get_dynamic_screenshot_mobile(dynamic_id, page: Page):
159157
await page.wait_for_load_state("domcontentloaded")
160158

161159
await page.wait_for_timeout(
162-
200 if plugin_config.haruka_dynamic_font_source == "remote" else 50
160+
1000 if plugin_config.haruka_dynamic_font_source == "remote" else 200
163161
)
164162

165163
# 判断字体是否加载完成
@@ -177,11 +175,7 @@ async def get_dynamic_screenshot_pc(dynamic_id, page: Page):
177175
"""电脑端动态截图"""
178176
url = f"https://t.bilibili.com/{dynamic_id}"
179177
await page.set_viewport_size({"width": 2560, "height": 1080})
180-
await page.goto(
181-
url,
182-
wait_until="networkidle",
183-
timeout=plugin_config.haruka_dynamic_timeout * 1000,
184-
)
178+
await page.goto(url, wait_until="networkidle")
185179
# 动态被删除或者进审核了
186180
if page.url == "https://www.bilibili.com/404":
187181
raise Notfound

haruka_bot/utils/captcha.py

Lines changed: 0 additions & 125 deletions
This file was deleted.

haruka_bot/utils/mobile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function setFont(font = "", fontSource = "local") {
157157
// 如果传入了字体名, 则将其添加到字体列表首位
158158
if (fontSource === "local") {
159159
needLoadFontList.unshift({
160-
fontUrl: `https://fonts.bbot/${font}`,
160+
fontUrl: `https://static.graiax/fonts/${font}`,
161161
fontFamily: "BBot_Custom_Font",
162162
});
163163
} else if (fontSource === "remote") {

haruka_bot/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from packaging.version import Version
22

3-
__version__ = "1.6.0post4"
3+
__version__ = "1.6.0post5"
44
VERSION = Version(__version__)

0 commit comments

Comments
 (0)