Skip to content

Commit

Permalink
v2.6.17 (#411)
Browse files Browse the repository at this point in the history
* refactor: 重构嵌套层级过多的函数,分离出其中一部分为独立函数

* fix(plugin_loading): 确保插件加载顺序

* fix(run_handlers): 修正参数列表问题

* refactor(handlers): 去除未使用的参数

* fix(rss_parsing): 修正 `save_first_time_fetch()` 的逻辑

* fix(ParsingRss): 修正 `start()` 的相关逻辑

* feat(fetch): 为方便测试,在 debug 环境下不使用缓存

* feat(config): 新增配置项 `enable_boot_message` ,用来控制启动后是否发送提示消息 (close #412)

* fix(parsing): 修正解析处理相关逻辑

* feat(config): 新增配置项 `enable_boot_message` ,用来控制启动后是否发送提示消息

* chore(release): v2.6.17
  • Loading branch information
NekoAria authored Mar 30, 2023
1 parent 08f4a12 commit 18b1178
Show file tree
Hide file tree
Showing 27 changed files with 546 additions and 516 deletions.
1 change: 1 addition & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RSSHUB="https://rsshub.app" # rsshub订阅地址
DB_CACHE_EXPIRE=30 # 去重数据库的记录清理限定天数
LIMIT=200 # 缓存rss条数
MAX_LENGTH=1024 # 正文长度限制,防止消息太长刷屏,以及消息过长发送失败的情况
ENABLE_BOOT_MESSAGE=true # 是否启用启动时的提示消息推送

# 图片压缩
ZIP_SIZE=2048 # 非 GIF 图片压缩后的最大长宽值,单位 px
Expand Down
2 changes: 2 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
app = nonebot.get_asgi()
driver = nonebot.get_driver()
driver.register_adapter(OneBotV11Adapter)
nonebot.load_plugin("nonebot_plugin_apscheduler")
nonebot.load_plugin("nonebot_plugin_guild_patch")
nonebot.load_plugins("src/plugins")

if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ELF_RSS"
version = "2.6.16"
version = "2.6.17"
description = "QQ机器人 RSS订阅 插件,订阅源建议选择 RSSHub"
authors = ["Quan666 <[email protected]>"]
license = "GPL-3.0-only"
Expand Down Expand Up @@ -52,7 +52,7 @@ show_error_codes = true
strict = true

[tool.ruff]
select = ["F", "E", "W", "I"]
select = ["F", "E", "W", "I", "UP"]
target-version = "py38"
line-length = 120

Expand Down
10 changes: 6 additions & 4 deletions src/plugins/ELF_RSS2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

from . import command
from . import my_trigger as tr
from .config import DATA_PATH
from .config import DATA_PATH, config
from .rss_class import Rss
from .utils import send_message_to_admin

require("nonebot_plugin_apscheduler")
from nonebot_plugin_apscheduler import scheduler # noqa

VERSION = "2.6.16"
VERSION = "2.6.17"

__plugin_meta__ = PluginMetadata(
name="ELF_RSS",
Expand Down Expand Up @@ -44,9 +44,11 @@ async def start(bot: Bot) -> None:

rss_list = Rss.read_rss() # 读取list
if not rss_list:
await send_message_to_admin(f"第一次启动,你还没有订阅,记得添加哟!\n{boot_message}", bot)
if config.enable_boot_message:
await send_message_to_admin(f"第一次启动,你还没有订阅,记得添加哟!\n{boot_message}", bot)
logger.info("第一次启动,你还没有订阅,记得添加哟!")
await send_message_to_admin(f"ELF_RSS 订阅器启动成功!\n{boot_message}", bot)
if config.enable_boot_message:
await send_message_to_admin(f"ELF_RSS 订阅器启动成功!\n{boot_message}", bot)
logger.info("ELF_RSS 订阅器启动成功!")
# 创建检查更新任务
await asyncio.gather(*[tr.add_job(rss) for rss in rss_list if not rss.stop])
112 changes: 71 additions & 41 deletions src/plugins/ELF_RSS2/command/change_dy.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ def handle_regex_property(value: str, old_value: str) -> Optional[str]:
}


def handle_name_change(rss: Rss, value_to_change: str) -> None:
tr.delete_job(rss)
rss.rename_file(str(DATA_PATH / f"{Rss.handle_name(value_to_change)}.json"))


def handle_time_change(value_to_change: str) -> str:
if not re.search(r"[_*/,-]", value_to_change):
if int(float(value_to_change)) < 1:
return "1"
else:
return str(int(float(value_to_change)))
return value_to_change


# 处理要修改的订阅参数
def handle_change_list(
rss: Rss,
Expand All @@ -112,8 +126,7 @@ def handle_change_list(
guild_channel_id: Optional[str],
) -> None:
if key_to_change == "name":
tr.delete_job(rss)
rss.rename_file(str(DATA_PATH / f"{Rss.handle_name(value_to_change)}.json"))
handle_name_change(rss, value_to_change)
elif (
key_to_change in {"qq", "qun", "channel"}
and not group_id
Expand All @@ -123,11 +136,7 @@ def handle_change_list(
value_to_change, getattr(rss, attribute_dict[key_to_change])
) # type:ignore
elif key_to_change == "time":
if not re.search(r"[_*/,-]", value_to_change):
if int(float(value_to_change)) < 1:
value_to_change = "1"
else:
value_to_change = str(int(float(value_to_change)))
value_to_change = handle_time_change(value_to_change)
elif key_to_change in {
"proxy",
"tl",
Expand Down Expand Up @@ -197,23 +206,12 @@ def handle_change_list(
"""


@RSS_CHANGE.got("RSS_CHANGE", prompt=prompt)
async def handle_rss_change(
event: MessageEvent, change_info: str = ArgPlainText("RSS_CHANGE")
) -> None:
group_id = event.group_id if isinstance(event, GroupMessageEvent) else None
guild_channel_id = (
f"{event.guild_id}@{event.channel_id}"
if isinstance(event, GuildMessageEvent)
else None
)
name_list = change_info.split(" ")[0].split(",")
rss_list: List[Rss] = []
for name in name_list:
if rss_tmp := Rss.get_one_by_name(name=name):
rss_list.append(rss_tmp)

# 出于公平考虑,限制订阅者只有当前群组或频道时才能修改订阅,否则只有超级管理员能修改
async def filter_rss_by_permissions(
rss_list: List[Rss],
change_info: str,
group_id: Optional[int],
guild_channel_id: Optional[str],
) -> List[Rss]:
if group_id:
if re.search(" (qq|qun|channel)=", change_info):
await RSS_CHANGE.finish("❌ 禁止在群组中修改订阅账号!如要取消订阅请使用 deldy 命令!")
Expand All @@ -238,7 +236,32 @@ async def handle_rss_change(

if not rss_list:
await RSS_CHANGE.finish("❌ 请检查是否存在以下问题:\n1.要修改的订阅名不存在对应的记录\n2.当前群组或频道无权操作")
elif len(rss_list) > 1 and " name=" in change_info:

return rss_list


@RSS_CHANGE.got("RSS_CHANGE", prompt=prompt)
async def handle_rss_change(
event: MessageEvent, change_info: str = ArgPlainText("RSS_CHANGE")
) -> None:
group_id = event.group_id if isinstance(event, GroupMessageEvent) else None
guild_channel_id = (
f"{event.guild_id}@{event.channel_id}"
if isinstance(event, GuildMessageEvent)
else None
)
name_list = change_info.split(" ")[0].split(",")
rss_list: List[Rss] = []
for name in name_list:
if rss_tmp := Rss.get_one_by_name(name=name):
rss_list.append(rss_tmp)

# 出于公平考虑,限制订阅者只有当前群组或频道时才能修改订阅,否则只有超级管理员能修改
rss_list = await filter_rss_by_permissions(
rss_list, change_info, group_id, guild_channel_id
)

if len(rss_list) > 1 and " name=" in change_info:
await RSS_CHANGE.finish("❌ 禁止将多个订阅批量改名!会因为名称相同起冲突!")

# 参数特殊处理:正文待移除内容
Expand All @@ -259,6 +282,24 @@ async def handle_rss_change(
await RSS_CHANGE.finish(result_msg)


async def validate_rss_change(key_to_change: str, value_to_change: str) -> None:
# 对用户输入的去重模式参数进行校验
mode_property_set = {"", "-1", "link", "title", "image", "or"}
if key_to_change == "mode" and (
set(value_to_change.split(",")) - mode_property_set or value_to_change == "or"
):
await RSS_CHANGE.finish(f"❌ 去重模式参数错误!\n{key_to_change}={value_to_change}")
elif key_to_change in {
"downkey",
"wkey",
"blackkey",
"bkey",
} and not regex_validate(value_to_change.lstrip("+-")):
await RSS_CHANGE.finish(f"❌ 正则表达式错误!\n{key_to_change}={value_to_change}")
elif key_to_change == "ppk" and not regex_validate(value_to_change):
await RSS_CHANGE.finish(f"❌ 正则表达式错误!\n{key_to_change}={value_to_change}")


async def batch_change_rss(
change_list: List[str],
group_id: Optional[int],
Expand All @@ -267,28 +308,16 @@ async def batch_change_rss(
rm_list_exist: Optional[Match[str]] = None,
) -> List[Rss]:
changed_rss_list = []

for rss in rss_list:
new_rss = deepcopy(rss)
rss_name = rss.name

for change_dict in change_list:
key_to_change, value_to_change = change_dict.split("=", 1)

if key_to_change in attribute_dict.keys():
# 对用户输入的去重模式参数进行校验
mode_property_set = {"", "-1", "link", "title", "image", "or"}
if key_to_change == "mode" and (
set(value_to_change.split(",")) - mode_property_set
or value_to_change == "or"
):
await RSS_CHANGE.finish(f"❌ 去重模式参数错误!\n{change_dict}")
elif key_to_change in {
"downkey",
"wkey",
"blackkey",
"bkey",
} and not regex_validate(value_to_change.lstrip("+-")):
await RSS_CHANGE.finish(f"❌ 正则表达式错误!\n{change_dict}")
elif key_to_change == "ppk" and not regex_validate(value_to_change):
await RSS_CHANGE.finish(f"❌ 正则表达式错误!\n{change_dict}")
await validate_rss_change(key_to_change, value_to_change)
handle_change_list(
new_rss, key_to_change, value_to_change, group_id, guild_channel_id
)
Expand All @@ -298,6 +327,7 @@ async def batch_change_rss(
if new_rss.__dict__ == rss.__dict__ and not rm_list_exist:
continue
changed_rss_list.append(new_rss)

# 参数解析完毕,写入
new_rss.upsert(rss_name)

Expand Down
39 changes: 26 additions & 13 deletions src/plugins/ELF_RSS2/command/del_dy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List, Optional, Tuple

from nonebot import on_command, require
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Message, MessageEvent
from nonebot.adapters.onebot.v11.permission import GROUP_ADMIN, GROUP_OWNER
Expand Down Expand Up @@ -34,21 +36,12 @@ async def handle_first_receive(matcher: Matcher, args: Message = CommandArg()) -
matcher.set_arg("RSS_DELETE", args)


@RSS_DELETE.got("RSS_DELETE", prompt="输入要删除的订阅名")
async def handle_rss_delete(
event: MessageEvent, rss_name: str = ArgPlainText("RSS_DELETE")
) -> None:
group_id = None
guild_channel_id = None

if isinstance(event, GroupMessageEvent):
group_id = event.group_id
elif isinstance(event, GuildMessageEvent):
guild_channel_id = f"{event.guild_id}@{event.channel_id}"

rss_name_list = rss_name.strip().split(" ")
async def process_rss_deletion(
rss_name_list: List[str], group_id: Optional[int], guild_channel_id: Optional[str]
) -> Tuple[List[str], List[str]]:
delete_successes = []
delete_failures = []

for rss_name in rss_name_list:
rss = Rss.get_one_by_name(name=rss_name)
if rss is None:
Expand Down Expand Up @@ -78,6 +71,26 @@ async def handle_rss_delete(
tr.delete_job(rss)
delete_successes.append(rss_name)

return delete_successes, delete_failures


@RSS_DELETE.got("RSS_DELETE", prompt="输入要删除的订阅名")
async def handle_rss_delete(
event: MessageEvent, rss_name: str = ArgPlainText("RSS_DELETE")
) -> None:
group_id = event.group_id if isinstance(event, GroupMessageEvent) else None
guild_channel_id = (
f"{event.guild_id}@{event.channel_id}"
if isinstance(event, GuildMessageEvent)
else None
)

rss_name_list = rss_name.strip().split(" ")

delete_successes, delete_failures = await process_rss_deletion(
rss_name_list, group_id, guild_channel_id
)

result = []
if delete_successes:
if guild_channel_id:
Expand Down
Loading

0 comments on commit 18b1178

Please sign in to comment.