Skip to content

Commit

Permalink
v1.4.0 Merge pull request #60 from djkcyl/dev
Browse files Browse the repository at this point in the history
v1.4.0 更新
  • Loading branch information
djkcyl authored Jun 3, 2023
2 parents e80bab8 + 57b7400 commit e068c88
Show file tree
Hide file tree
Showing 25 changed files with 1,069 additions and 837 deletions.
2 changes: 1 addition & 1 deletion aunly_bbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
logger.success("字体下载完成!")

host = BotConfig.Mirai.mirai_host
if cache.get("skip_verfiy"):
if cache.get("skip_verify"):
if verify_mirai(host, BotConfig.Mirai.account, BotConfig.Mirai.verify_key):
logger.success("Mirai HTTP API 验证成功!")
else:
Expand Down
9 changes: 5 additions & 4 deletions aunly_bbot/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ..core.announcement import PROJECT_VERSION


@click.group(help="BBot 命令行工具")
@click.group("BBot Cli", help="BBot 命令行工具")
@click.version_option(
PROJECT_VERSION,
"-v",
Expand All @@ -19,13 +19,14 @@ def main():

@click.command(name="run", help="运行 BBot")
@click.option("-t", "--test", is_flag=True, help="测试模式")
@click.option("-s", "--skip-verfiy", is_flag=True, help="跳过 MAH 可用性检查")
@click.option("-s", "--skip-verify", is_flag=True, help="跳过 MAH 可用性检查")
@click.option("-i", "--ignore-sub", is_flag=True, help="忽略登录模式下的账户订阅列表")
@click.help_option("-h", "--help", help="显示帮助信息")
def run_bot(test: bool, skip_verfiy: bool, ignore_sub: bool):
def run_bot(test: bool, skip_verify: bool, ignore_sub: bool):
from ..core import cache

cache["test"] = test
cache["skip_verfiy"] = skip_verfiy
cache["skip_verify"] = skip_verify
cache["ignore_sub"] = ignore_sub

from .run import run_bot
Expand Down
22 changes: 20 additions & 2 deletions aunly_bbot/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def __init__(self) -> None:
self.mirai_verify_key()
self.debug()
self.use_browser()
self.bilibili_mobile_style()
self.bilibili_concurrent()
self.openai_summary()
self.bilibili_username()
Expand All @@ -55,7 +54,7 @@ def __init__(self) -> None:
self.max_subscriptions()
self.update_check()

if data.get("session", None):
if data.get("session"):
httpx.post(
f"{self.config['Mirai']['mirai_host']}/release",
json={
Expand Down Expand Up @@ -111,6 +110,12 @@ def mirai_mirai_host(self):
)
self.config["Mirai"]["mirai_host"] = mirai_host
return
except KeyError:
click.secho(
"你输入的地址可能不是 Mirai HTTP API 的地址或 Mirai HTTP API 运行异常,请检查后重试!",
fg="bright_red",
bold=True,
)
except httpx.HTTPError:
click.secho("无法连接到 Mirai HTTP API,请检查地址是否正确!", fg="bright_red", bold=True)
continue
Expand Down Expand Up @@ -225,6 +230,8 @@ def use_browser(self):
annotation="使用键盘的 ↑ 和 ↓ 来选择, 按回车确认",
).prompt()
self.config["Bilibili"]["use_browser"] = browser.name == "是(开启)"
self.bilibili_mobile_style()
self.allow_fallback()
else:
self.config["Bilibili"]["use_browser"] = False

Expand All @@ -240,6 +247,15 @@ def bilibili_mobile_style(self):
else:
self.config["Bilibili"]["mobile_style"] = False

def allow_fallback(self):
allow_fallback = ListPrompt(
"是否允许使用备用动态图片渲染(在浏览器截图失败时尝试使用)?",
[Choice("是(开启)"), Choice("否(关闭)")],
allow_filter=False,
annotation="使用键盘的 ↑ 和 ↓ 来选择, 按回车确认",
).prompt()
self.config["Bilibili"]["allow_fallback"] = allow_fallback.name == "是(开启)"

def openai_summary(self):
openai_summary = ListPrompt(
"是否使用 OpenAI 进行视频和专栏内容摘要提取?",
Expand All @@ -251,6 +267,8 @@ def openai_summary(self):
self.config["Bilibili"]["openai_summarization"] = True
self.openai_api_token()
self.openai_model()
else:
self.config["Bilibili"]["openai_summarization"] = False

def openai_api_token(self):
openai_token = InputPrompt("请输入 OpenAI Token: ").prompt()
Expand Down
10 changes: 8 additions & 2 deletions aunly_bbot/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)


data_version = 4
data_version = 5
db = SqliteDatabase("data/data.db")


Expand Down Expand Up @@ -114,7 +114,10 @@ class Meta:
table_name = "data_version"


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


if not DataVersion.select().exists():
Expand All @@ -138,6 +141,9 @@ class Meta:
# 在 SubList 表中添加 cover_img 字段,允许为空
db.execute_sql("ALTER TABLE sub_list ADD COLUMN cover_img VARCHAR(255) NULL")
DataVersion.update(version=4).execute()
elif DataVersion.get().version == 4:
logger.info("当前数据版本为 4,正在更新至 5")
DataVersion.update(version=5).execute()

logger.success("数据库更新完成")
time.sleep(2)
Expand Down
21 changes: 15 additions & 6 deletions aunly_bbot/core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ def in_screen():
return psutil.Process().pid == 1


if in_screen() or is_package:
logger.info("检测到当前运行在各类容器中,或运行为打包(nuitka、pyinstaller)版本,已禁用 richuru")
if in_screen() or is_package or not BotConfig.use_richuru:
logger.info("检测到当前运行在各类容器中、运行为打包(nuitka、pyinstaller)版本或未在配置文件中启用,已禁用 richuru")
logger.remove(0)
logger.add(sys.stderr, level=log_level, backtrace=True, diagnose=True)
logger.add(
sys.stderr,
level=log_level,
backtrace=True,
diagnose=True,
filter=lambda record: (
"sentry_patched_callhandlers" not in record["function"]
or "HTTP Request" not in record["message"]
),
)
else:
richuru.install(level=log_level)

Expand All @@ -43,7 +52,7 @@ def in_screen():
backtrace=True,
diagnose=True,
rotation="00:00",
retention="1 years",
retention="1 week",
compression="tar.xz",
level="INFO",
)
Expand All @@ -55,7 +64,7 @@ def in_screen():
backtrace=True,
diagnose=True,
rotation="00:00",
retention="15 days",
retention="3 days",
compression="tar.xz",
level="DEBUG",
)
Expand All @@ -67,7 +76,7 @@ def in_screen():
backtrace=True,
diagnose=True,
rotation="00:00",
retention="15 days",
retention="3 days",
compression="tar.xz",
level="WARNING",
)
Expand Down
5 changes: 2 additions & 3 deletions aunly_bbot/function/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
import aunly_bbot.function.pusher.init
import aunly_bbot.function.pusher.dynamic
import aunly_bbot.function.pusher.live
import aunly_bbot.function.scheduler.version_update # noqa

# import function.scheduler.refresh_token # noqa
import aunly_bbot.function.scheduler.version_update
import aunly_bbot.function.scheduler.refresh_token # noqa

from loguru import logger

Expand Down
4 changes: 2 additions & 2 deletions aunly_bbot/function/command/configure/nick.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Twilight(
[
"at" @ ElementMatch(At, optional=True),
"act" @ RegexMatch(r"设定|删除"),
"act" @ RegexMatch(r"添加|修改|设定|删除"),
RegexMatch(r"昵称|别名"),
"uid" @ ParamMatch(optional=True),
"nick" @ ParamMatch(optional=True),
Expand All @@ -56,7 +56,7 @@ async def main(
uid = await uid_extract(uid.result.display, group.id)
if uid:
acts = act.result.display
if acts == "设定":
if acts in ["设定", "修改", "添加"]:
nicks = nick.result.display
if len(nicks) > 24:
msg = "昵称过长,设定失败"
Expand Down
102 changes: 56 additions & 46 deletions aunly_bbot/function/command/content_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ...core.data import ContentResolveData
from ...core.control import Interval, Permission
from ...utils.video_subtitle import get_subtitle
from ...utils.message_resolve import message_resolve
from ...utils.bilibili_parse import extract_bilibili_info
from ...utils.draw_bili_image import binfo_image_create
from ...utils.text2image import rich_text2image, browser_text2image
from ...utils.bilibili_request import get_b23_url, grpc_get_view_info
Expand All @@ -34,41 +34,17 @@
async def main(
app: Ariadne, group: Group, member: Member, message: MessageChain, source: Source
):
bili_number = await message_resolve(message)
bili_number = await extract_bilibili_info(message)
if not bili_number:
return

if bili_number[:2] in ["BV", "bv", "av"]:
try:
if (video_info := await video_info_get(bili_number)) is None:
await Interval.manual(group.id, 5)
return
elif video_info.ecode == 1:
await app.send_group_message(
group, MessageChain(f"未找到视频 {bili_number},可能已被 UP 主删除。"), quote=source
)
return
except (AioRpcError, GrpcError) as e:
aid, cid, bvid, title, video_info = await extract_video_info(bili_number)
except AbortError as e:
await Interval.manual(group.id, 5)
logger.exception(e)
return await app.send_group_message(
group, MessageChain(f"{bili_number} 视频信息获取失败,错误信息:{type(e)} {e}"), quote=source
)
except Exception as e:
capture_exception()
await Interval.manual(group.id, 5)
logger.exception(e)
return await app.send_group_message(
group, MessageChain(f"{bili_number} 视频信息解析失败,错误信息:{type(e)} {e}"), quote=source
)
aid = video_info.activity_season.arc.aid or video_info.arc.aid
cid = (
video_info.activity_season.pages[0].page.cid
if video_info.activity_season.pages
else video_info.pages[0].page.cid
)
bvid = video_info.activity_season.bvid or video_info.bvid
title = video_info.activity_season.arc.title or video_info.arc.title
return await app.send_group_message(group, MessageChain(e.message), quote=source)

archive_data = ContentResolveData(aid=aid)
archive_data.title = title
await Interval.manual(aid + group.id, 30)
Expand Down Expand Up @@ -110,20 +86,27 @@ async def openai_summarization():
else:
logger.info(f"{aid} 总结不存在,正在尝试请求......")
try:
await Interval.manual(member, 600)
if (
BotConfig.Bilibili.openai_whitelist_users
and member.id
not in BotConfig.Bilibili.openai_whitelist_users
):
await Interval.manual(
member, BotConfig.Bilibili.openai_cooldown
)
except ExecutionStop:
msg = f"{member.id} 在 10 分钟内已经请求过总结,跳过本次请求"
logger.info(msg)
raise AbortError(msg)
ai_summary = await subtitle_summarise(subtitle, title)
if ai_summary.summary:
summarise = ai_summary.summary
if ai_summary.response:
summarise = ai_summary.response
archive_data.openai = summarise
else:
logger.warning(f"视频 {aid} 总结失败:{ai_summary.raw}")
return

if "no meaning" in summarise.lower():
if "no meaning" in summarise.lower() or len(summarise) < 20:
nonlocal chatgpt_thinks
chatgpt_thinks = False
raise AbortError("ChatGPT 认为这些字幕没有意义")
Expand All @@ -134,11 +117,7 @@ async def openai_summarization():
else:
image = await rich_text2image(summarise)
if image:
await app.send_group_message(
group,
MessageChain(Image(data_bytes=image)),
quote=info_message.source,
)
images.append(image)
except AbortError as e:
logger.warning(f"视频 {aid} 总结被终止:{e}")
except Exception:
Expand All @@ -164,26 +143,31 @@ async def wordcloud():

wordcloud = await get_worldcloud_image(word_frequencies)
if wordcloud:
await app.send_group_message(
group,
MessageChain(Image(data_bytes=wordcloud)),
quote=info_message.source,
)
images.append(wordcloud)
except Exception:
capture_exception()
logger.exception(f"视频 {aid} 词云出错")

images = []
if BotConfig.Bilibili.openai_summarization:
await openai_summarization()
if BotConfig.Bilibili.use_wordcloud and chatgpt_thinks:
await wordcloud()

if images:
await app.send_group_message(
group,
MessageChain([Image(data_bytes=x) for x in images]),
quote=info_message.source,
)

except AbortError as e:
logger.warning(f"视频 {aid} 总结失败:{e.message}")
return
archive_data.save()

except TimeoutException:
logger.exception(f"视频 {aid} 信息生成超时")
await app.send_group_message(
group, MessageChain(f"{bili_number} 视频信息生成超时,请稍后再试。"), quote=source
)
Expand Down Expand Up @@ -213,8 +197,8 @@ async def openai_summarization():
summarise = archive_data.openai
else:
ai_summary = await column_summarise(cv_title, cv_text)
if ai_summary.summary:
summarise = ai_summary.summary
if ai_summary.response:
summarise = ai_summary.response
archive_data.openai = summarise
else:
return
Expand Down Expand Up @@ -282,3 +266,29 @@ async def video_info_get(vid_id: str):
aid = int(vid_id[2:])
return await grpc_get_view_info(aid=aid) if aid > 1 else None
return await grpc_get_view_info(bvid=vid_id)


async def extract_video_info(bili_number: str):
try:
if (video_info := await video_info_get(bili_number)) is None:
raise AbortError(f"无法获取视频 {bili_number}。")
elif video_info.ecode == 1:
raise AbortError(f"未找到视频 {bili_number},可能已被 UP 主删除。")
except (AioRpcError, GrpcError) as e:
logger.exception(e)
raise AbortError(f"{bili_number} 视频信息获取失败,错误信息:{type(e)} {e}")
except Exception as e:
capture_exception()
logger.exception(e)
raise AbortError(f"{bili_number} 视频信息解析失败,错误信息:{type(e)} {e}")

aid = video_info.activity_season.arc.aid or video_info.arc.aid
cid = (
video_info.activity_season.pages[0].page.cid
if video_info.activity_season.pages
else video_info.pages[0].page.cid
)
bvid = video_info.activity_season.bvid or video_info.bvid
title = video_info.activity_season.arc.title or video_info.arc.title

return aid, cid, bvid, title, video_info
Loading

0 comments on commit e068c88

Please sign in to comment.