Skip to content

Commit

Permalink
Merge pull request #411 from SK-415/dev
Browse files Browse the repository at this point in the history
post5
  • Loading branch information
djkcyl authored Jul 11, 2023
2 parents 51dc6c3 + 213ff9b commit 6d45457
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 206 deletions.
10 changes: 10 additions & 0 deletions docs/usage/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ HARUKA_SCREENSHOT_STYLE=pc
HARUKA_CAPTCHA_ADDRESS=https://captcha-cd.ngworks.cn
```

## HARUKA_CAPTCHA_TOKEN

默认值:harukabot

验证码 Token,用于验证码服务器鉴权,若不填写一天内只能使用 5 次。

```yml
HARUKA_CAPTCHA_TOKEN=harukabot
```

## HARUKA_BROWSER_UA

默认值:""
Expand Down
1 change: 1 addition & 0 deletions haruka_bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Config(BaseSettings):
haruka_dynamic_at: bool = False
haruka_screenshot_style: str = "mobile"
haruka_captcha_address: str = "https://captcha-cd.ngworks.cn"
haruka_captcha_token: str = "harukabot"
haruka_browser_ua: Optional[str] = None
haruka_dynamic_timeout: int = 30
haruka_dynamic_font_source: str = "system"
Expand Down
14 changes: 10 additions & 4 deletions haruka_bot/plugins/pusher/dynamic_pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
EVENT_JOB_MISSED,
EVENT_SCHEDULER_STARTED,
)
from bilireq.exceptions import GrpcError
from bilireq.grpc.dynamic import grpc_get_user_dynamics
from bilireq.grpc.protos.bilibili.app.dynamic.v2.dynamic_pb2 import DynamicType
from grpc import StatusCode
Expand Down Expand Up @@ -44,8 +45,12 @@ async def dy_sched():
except AioRpcError as e:
if e.code() == StatusCode.DEADLINE_EXCEEDED:
logger.error(f"爬取动态超时,将在下个轮询中重试:{e.code()} {e.details()}")
return
raise
else:
logger.error(f"爬取动态失败:{e.code()} {e.details()}")
return
except GrpcError as e:
logger.error(f"爬取动态失败:{e.code} {e.msg}")
return

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

push_list = await db.get_push_list(uid, "dynamic")
Expand Down
11 changes: 7 additions & 4 deletions haruka_bot/plugins/pusher/live_pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"interval", seconds=plugin_config.haruka_live_interval, id="live_sched"
)
async def live_sched():
# sourcery skip: use-fstring-for-concatenation
"""直播推送"""
uids = await db.get_uid_list("live")

Expand All @@ -42,20 +43,22 @@ async def live_sched():
url = f"https://live.bilibili.com/{room_id}"
title = info["title"]
cover = info["cover_from_user"] or info["keyframe"]
area_parent = info["area_v2_parent_name"]
area = info["area_v2_name"]
area_parent = info["area_v2_parent_name"]
room_area = f"{area_parent} / {area}"
logger.info(f"检测到开播:{name}{uid})")
live_msg = (
f"{name} 开播啦!\n分区:{room_area}\n标题:{title}\n{MessageSegment.image(cover)}\n{url}"
f"{name} 开播啦!\n分区:{room_area}\n标题:{title}\n"
+ MessageSegment.image(cover)
+ f"\n{url}"
)
else: # 下播
logger.info(f"检测到下播:{name}{uid})")
if not plugin_config.haruka_live_off_notify: # 没开下播推送
continue
live_time_msg = (
f",本次直播时长 {calc_time_total(time.time() - live_time[uid])}。"
if live_time[uid]
f"\n本次直播时长 {calc_time_total(time.time() - live_time[uid])}。"
if live_time.get(uid)
else "。"
)
live_msg = f"{name} 下播了{live_time_msg}"
Expand Down
2 changes: 1 addition & 1 deletion haruka_bot/plugins/sub/sub_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def _(event: MessageEvent, bot: Bot):
user = await db.get_user(uid=sub.uid)
assert user is not None
message += (
f"{user.name}{user.uid})"
f"{user.name}{user.uid}\n"
f"直播:{'开' if sub.live else '关'},"
f"动态:{'开' if sub.dynamic else '关'},"
# TODO 私聊不显示全体
Expand Down
34 changes: 14 additions & 20 deletions haruka_bot/utils/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
from typing import Optional

from nonebot.log import logger
from aunly_captcha_solver import CaptchaInfer
from playwright.__main__ import main
from playwright.async_api import BrowserContext, async_playwright, Page

from ..config import plugin_config
from .fonts_provider import fill_font
from .captcha import resolve_captcha
from ..utils import get_path

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


async def init_browser(proxy=plugin_config.haruka_proxy, **kwargs) -> BrowserContext:
logger.info("初始化浏览器")
if proxy:
kwargs["proxy"] = {"server": proxy}
global _browser
Expand All @@ -38,6 +39,7 @@ async def init_browser(proxy=plugin_config.haruka_proxy, **kwargs) -> BrowserCon
else None
),
device_scale_factor=2,
timeout=plugin_config.haruka_dynamic_timeout * 1000,
**kwargs,
)
if plugin_config.haruka_screenshot_style.lower() != "mobile":
Expand All @@ -57,7 +59,7 @@ async def init_browser(proxy=plugin_config.haruka_proxy, **kwargs) -> BrowserCon

async def get_browser() -> BrowserContext:
global _browser
if not _browser or _browser.browser is None or not _browser.browser.is_connected():
if not _browser:
_browser = await init_browser()
return _browser

Expand All @@ -78,7 +80,7 @@ async def get_dynamic_screenshot(dynamic_id, style=plugin_config.haruka_screensh
clip["height"] = min(clip["height"], 32766)
return (
await page.screenshot(clip=clip, full_page=True, type="jpeg", quality=98),
err,
None,
)
except TimeoutError:
logger.warning(f"截图超时,重试 {i + 1}/3")
Expand Down Expand Up @@ -115,13 +117,12 @@ async def get_dynamic_screenshot_mobile(dynamic_id, page: Page):
await page.set_viewport_size({"width": 460, "height": 780})
await page.route(re.compile("^https://static.graiax/fonts/(.+)$"), fill_font)
if plugin_config.haruka_captcha_address:
page = await resolve_captcha(url, page)
else:
await page.goto(
url,
wait_until="networkidle",
timeout=plugin_config.haruka_dynamic_timeout * 1000,
captcha = CaptchaInfer(
plugin_config.haruka_captcha_address, plugin_config.haruka_captcha_token
)
page = await captcha.solve_captcha(page, url)
else:
await page.goto(url, wait_until="networkidle")
# 动态被删除或者进审核了
if page.url == "https://m.bilibili.com/404":
raise Notfound
Expand All @@ -137,11 +138,8 @@ async def get_dynamic_screenshot_mobile(dynamic_id, page: Page):
# "dyn.style.overflowWrap='break-word'"
# )

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

await page.add_script_tag(path=mobile_js)

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

await page.wait_for_timeout(
200 if plugin_config.haruka_dynamic_font_source == "remote" else 50
1000 if plugin_config.haruka_dynamic_font_source == "remote" else 200
)

# 判断字体是否加载完成
Expand All @@ -177,11 +175,7 @@ async def get_dynamic_screenshot_pc(dynamic_id, page: Page):
"""电脑端动态截图"""
url = f"https://t.bilibili.com/{dynamic_id}"
await page.set_viewport_size({"width": 2560, "height": 1080})
await page.goto(
url,
wait_until="networkidle",
timeout=plugin_config.haruka_dynamic_timeout * 1000,
)
await page.goto(url, wait_until="networkidle")
# 动态被删除或者进审核了
if page.url == "https://www.bilibili.com/404":
raise Notfound
Expand Down
125 changes: 0 additions & 125 deletions haruka_bot/utils/captcha.py

This file was deleted.

2 changes: 1 addition & 1 deletion haruka_bot/utils/mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function setFont(font = "", fontSource = "local") {
// 如果传入了字体名, 则将其添加到字体列表首位
if (fontSource === "local") {
needLoadFontList.unshift({
fontUrl: `https://fonts.bbot/${font}`,
fontUrl: `https://static.graiax/fonts/${font}`,
fontFamily: "BBot_Custom_Font",
});
} else if (fontSource === "remote") {
Expand Down
2 changes: 1 addition & 1 deletion haruka_bot/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from packaging.version import Version

__version__ = "1.6.0post4"
__version__ = "1.6.0post5"
VERSION = Version(__version__)
Loading

0 comments on commit 6d45457

Please sign in to comment.