Skip to content

Commit

Permalink
Merge pull request #14 from Ailitonia/dev
Browse files Browse the repository at this point in the history
B站相关插件优化
  • Loading branch information
Ailitonia authored Apr 20, 2021
2 parents 23098f3 + 73ca9e3 commit ea700a8
Show file tree
Hide file tree
Showing 17 changed files with 371 additions and 297 deletions.
5 changes: 0 additions & 5 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ BILI_UID=
BILI_SESSDATA=
BILI_CSRF=

# B站直播与动态检查模式, 是否启用检查池模式
# 单位时间内检查数量相同, 但总体检查时间间隔会根据订阅量延长, 对于订阅量较大的情况可以有效避免被B站风控
# 启用HTTP代理时视情况关闭
ENABLE_BILI_CHECK_POOL_MODE=true

# 全局HTTP代理配置
# 若ENABLE_FORCED_PROXY设置为False, 则当启用 HTTP 代理后当代理不可用时自动使用直连网络(默认)
# 若ENABLE_FORCED_PROXY设置为True, 则当启用 HTTP 代理后当代理不可用时仍不会使用直连网络
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

基于 [Nonebot2](https://github.com/nonebot/nonebot2) 的 qq 机器人

![GitHub](https://img.shields.io/github/license/Ailitonia/nonebot2_miya)
![GitHub](https://img.shields.io/github/license/Ailitonia/omega-miya)
![Python](https://img.shields.io/badge/Python-3.8+-blue)
![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/Ailitonia/nonebot2_miya?include_prereleases)
![GitHub (Pre-)Release Date](https://img.shields.io/github/release-date-pre/Ailitonia/nonebot2_miya)
![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/Ailitonia/omega-miya?include_prereleases)
![GitHub (Pre-)Release Date](https://img.shields.io/github/release-date-pre/Ailitonia/omega-miya)

</div>

Expand Down Expand Up @@ -56,7 +56,7 @@
## 关于图片数据

如果你不想自己收集图片数据, 可以将
[这组图片数据集](https://github.com/Ailitonia/nonebot2_miya/raw/main/archive_data/db_pixiv.7z)
[这组图片数据集](https://github.com/Ailitonia/omega-miya/raw/main/archive_data/db_pixiv.7z)
导入数据库

这个图片集大概有5万条左右, 基本都是按我自己口味收集的图片
Expand All @@ -75,7 +75,7 @@ SHA1: `7CFF3593A85979B5D966773F3857577CFCC2FFBD`

**注意!该cookies等同于您账号控制权,请不要将这两个值泄露给他人!**

<img src="https://raw.githubusercontent.com/Ailitonia/nonebot2_miya/main/docs/img/how_to_get_bilibili_cookies.png" width="75%">
<img src="https://raw.githubusercontent.com/Ailitonia/omega-miya/main/docs/img/how_to_get_bilibili_cookies.png" width="75%">

**注意!该cookies等同于您账号控制权,请不要将这两个值泄露给他人!**

Expand Down
23 changes: 20 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,35 @@
if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

# File path
bot_root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'omega_miya'))
bot_tmp_path = os.path.abspath(os.path.join(bot_root_path, 'tmp'))
if not os.path.exists(bot_tmp_path):
os.makedirs(bot_tmp_path)

bot_log_path = os.path.abspath(os.path.join(bot_root_path, 'log'))
if not os.path.exists(bot_log_path):
os.makedirs(bot_log_path)

# Custom logger
log_info_name = f"{datetime.today().strftime('%Y%m%d-%H%M%S')}-INFO.log"
log_error_name = f"{datetime.today().strftime('%Y%m%d-%H%M%S')}-ERROR.log"
log_file_path = os.path.join(os.path.dirname(__file__), 'omega_miya', 'log')
log_info_path = os.path.join(log_file_path, log_info_name)
log_error_path = os.path.join(log_file_path, log_error_name)
log_info_path = os.path.join(bot_log_path, log_info_name)
log_error_path = os.path.join(bot_log_path, log_error_name)

logger.add(log_info_path, rotation="00:00", diagnose=False, level="INFO", format=default_format, encoding='utf-8')
logger.add(log_error_path, rotation="00:00", diagnose=False, level="ERROR", format=default_format, encoding='utf-8')

# You can pass some keyword args config to init function
nonebot.init()

# 初始化一些系统变量配置
config = nonebot.get_driver().config
config.root_path_ = bot_root_path
config.tmp_path_ = bot_tmp_path


# 注册 cqhttp adapter
driver = nonebot.get_driver()
driver.register_adapter("cqhttp", CQHTTPBot)

Expand Down
2 changes: 1 addition & 1 deletion omega_miya/plugins/Omega_auth_manage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

# 注册事件响应器
omegaauth = on_command('OmegaAuth', rule=to_me(), aliases={'omegaauth', 'oauth'},
permission=SUPERUSER, priority=1, block=True)
permission=SUPERUSER, priority=10, block=True)


# 修改默认参数处理
Expand Down
2 changes: 1 addition & 1 deletion omega_miya/plugins/Omega_help/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
level=10,
auth_node='basic'),
permission=GROUP,
priority=1,
priority=10,
block=True)


Expand Down
2 changes: 1 addition & 1 deletion omega_miya/plugins/Omega_manage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

# 注册事件响应器
omega = on_command('Omega', rule=None, aliases={'omega'},
permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER, priority=1, block=True)
permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER, priority=10, block=True)


# 修改默认参数处理
Expand Down
4 changes: 2 additions & 2 deletions omega_miya/plugins/announce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


# 注册事件响应器
announce = on_command('公告', rule=to_me(), aliases={'announce'}, permission=SUPERUSER, priority=1, block=True)
announce = on_command('公告', rule=to_me(), aliases={'announce'}, permission=SUPERUSER, priority=10, block=True)


# 修改默认参数处理
Expand Down Expand Up @@ -63,5 +63,5 @@ async def handle_announce(bot: Bot, event: Event, state: T_State):
else:
logger.warning(f'公告未发送, 不合规的群组类型或群号: {group}')
await announce.finish('不合规的群组类型或群号')
logger.warning(f'公告已成功发送群组: {group}')
logger.info(f'公告已成功发送群组: {group}')
await announce.finish('公告发送完成')
14 changes: 14 additions & 0 deletions omega_miya/plugins/bilibili_dynamic_monitor/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pydantic import BaseSettings


class Config(BaseSettings):

# plugin custom config
"""
B站动态检查模式, 是否启用检查池模式
单位时间内检查数量相同, 但总体检查时间间隔会根据订阅量延长, 对于订阅量较大的情况可以有效避免被B站风控
"""
enable_dynamic_check_pool_mode: bool = True

class Config:
extra = "ignore"
116 changes: 15 additions & 101 deletions omega_miya/plugins/bilibili_dynamic_monitor/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from nonebot.adapters.cqhttp import MessageSegment
from omega_miya.utils.Omega_Base import DBSubscription, DBDynamic, DBTable
from .utils import get_user_dynamic_history, get_user_info, get_user_dynamic, get_dynamic_info, pic_2_base64
from .utils import ENABLE_BILI_CHECK_POOL_MODE
from .utils import ENABLE_DYNAMIC_CHECK_POOL_MODE


# 检查池模式使用的检查队列
Expand Down Expand Up @@ -137,15 +137,6 @@ async def check_dynamic(dy_uid):
dynamic_info[num]['content'], dynamic_info[num]['url'], '=' * 16,
origin_dynamic_info['name'], origin_dynamic_info['content']
)
for group_id in notice_group:
for _bot in bots:
try:
await _bot.call_api(api='send_group_msg', group_id=group_id, message=msg)
logger.info(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']}")
except Exception as _e:
logger.warning(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']} 失败, "
f"error: {repr(_e)}")
continue
# 原创的动态(有图片)
elif dynamic_info[num]['type'] == 2:
# 处理图片序列
Expand All @@ -157,101 +148,44 @@ async def check_dynamic(dy_uid):
msg = '{}发布了新动态!\n\n“{}”\n{}\n{}'.format(
dynamic_info[num]['name'], dynamic_info[num]['content'],
dynamic_info[num]['url'], pic_segs)
for group_id in notice_group:
for _bot in bots:
try:
await _bot.call_api(api='send_group_msg', group_id=group_id, message=msg)
logger.info(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']}")
except Exception as _e:
logger.warning(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']} 失败, "
f"error: {repr(_e)}")
continue
# 原创的动态(无图片)
elif dynamic_info[num]['type'] == 4:
msg = '{}发布了新动态!\n\n“{}”\n{}'.format(
dynamic_info[num]['name'], dynamic_info[num]['content'], dynamic_info[num]['url'])
for group_id in notice_group:
for _bot in bots:
try:
await _bot.call_api(api='send_group_msg', group_id=group_id, message=msg)
logger.info(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']}")
except Exception as _e:
logger.warning(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']} 失败, "
f"error: {repr(_e)}")
continue
# 视频
elif dynamic_info[num]['type'] == 8:
msg = '{}发布了新的视频!\n\n《{}》\n“{}”\n{}'.format(
dynamic_info[num]['name'], dynamic_info[num]['origin'],
dynamic_info[num]['content'], dynamic_info[num]['url'])
for group_id in notice_group:
for _bot in bots:
try:
await _bot.call_api(api='send_group_msg', group_id=group_id, message=msg)
logger.info(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']}")
except Exception as _e:
logger.warning(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']} 失败, "
f"error: {repr(_e)}")
continue
# 小视频
elif dynamic_info[num]['type'] == 16:
msg = '{}发布了新的小视频动态!\n\n“{}”\n{}'.format(
dynamic_info[num]['name'], dynamic_info[num]['content'], dynamic_info[num]['url'])
for group_id in notice_group:
for _bot in bots:
try:
await _bot.call_api(api='send_group_msg', group_id=group_id, message=msg)
logger.info(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']}")
except Exception as _e:
logger.warning(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']} 失败, "
f"error: {repr(_e)}")
continue
# 番剧
elif dynamic_info[num]['type'] in [32, 512]:
msg = '{}发布了新的番剧!\n\n《{}》\n{}'.format(
dynamic_info[num]['name'], dynamic_info[num]['origin'], dynamic_info[num]['url'])
for group_id in notice_group:
for _bot in bots:
try:
await _bot.call_api(api='send_group_msg', group_id=group_id, message=msg)
logger.info(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']}")
except Exception as _e:
logger.warning(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']} 失败, "
f"error: {repr(_e)}")
continue
# 文章
elif dynamic_info[num]['type'] == 64:
msg = '{}发布了新的文章!\n\n《{}》\n“{}”\n{}'.format(
dynamic_info[num]['name'], dynamic_info[num]['origin'],
dynamic_info[num]['content'], dynamic_info[num]['url'])
for group_id in notice_group:
for _bot in bots:
try:
await _bot.call_api(api='send_group_msg', group_id=group_id, message=msg)
logger.info(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']}")
except Exception as _e:
logger.warning(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']} 失败, "
f"error: {repr(_e)}")
continue
# 音频
elif dynamic_info[num]['type'] == 256:
msg = '{}发布了新的音乐!\n\n《{}》\n“{}”\n{}'.format(
dynamic_info[num]['name'], dynamic_info[num]['origin'],
dynamic_info[num]['content'], dynamic_info[num]['url'])
for group_id in notice_group:
for _bot in bots:
try:
await _bot.call_api(api='send_group_msg', group_id=group_id, message=msg)
logger.info(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']}")
except Exception as _e:
logger.warning(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']} 失败, "
f"error: {repr(_e)}")
continue
# B站活动相关
elif dynamic_info[num]['type'] == 2048:
msg = '{}发布了一条活动相关动态!\n\n【{}】\n“{}”\n{}'.format(
dynamic_info[num]['name'], dynamic_info[num]['origin'],
dynamic_info[num]['content'], dynamic_info[num]['url'])
else:
logger.warning(f"未知的动态类型: {dynamic_info[num]['type']}, id: {dynamic_info[num]['id']}")
msg = None

# 向群组发送消息
if msg:
for group_id in notice_group:
for _bot in bots:
try:
Expand All @@ -261,8 +195,7 @@ async def check_dynamic(dy_uid):
logger.warning(f"向群组: {group_id} 发送新动态通知: {dynamic_info[num]['id']} 失败, "
f"error: {repr(_e)}")
continue
elif dynamic_info[num]['type'] == -1:
logger.warning(f"未知的动态类型: {dynamic_info[num]['id']}")

# 更新动态内容到数据库
dy_id = dynamic_info[num]['id']
dy_type = dynamic_info[num]['type']
Expand All @@ -278,7 +211,7 @@ async def check_dynamic(dy_uid):
logger.error(f'bilibili_dynamic_monitor: 解析新动态: {dy_uid} 的时发生了错误, error info: {repr(_e)}')

# 启用了检查池模式
if ENABLE_BILI_CHECK_POOL_MODE:
if ENABLE_DYNAMIC_CHECK_POOL_MODE:
global checking_pool

# checking_pool为空则上一轮检查完了, 重新往里面放新一轮的uid
Expand All @@ -288,7 +221,7 @@ async def check_dynamic(dy_uid):
# 看下checking_pool里面还剩多少
waiting_num = len(checking_pool)

# 默认单次检查并发数为2, 默认检查间隔为30s
# 默认单次检查并发数为2, 默认检查间隔为20s
logger.debug(f'bili dynamic pool mode debug info, B_checking_pool: {checking_pool}')
if waiting_num >= 2:
# 抽取检查对象
Expand Down Expand Up @@ -328,7 +261,7 @@ async def check_dynamic(dy_uid):

# 分时间段创建计划任务, 夜间闲时降低检查频率
# 根据检查池模式初始化检查时间间隔
if ENABLE_BILI_CHECK_POOL_MODE:
if ENABLE_DYNAMIC_CHECK_POOL_MODE:
# 检查池启用
scheduler.add_job(
bilibili_dynamic_monitor,
Expand All @@ -340,7 +273,7 @@ async def check_dynamic(dy_uid):
# day_of_week=None,
# hour='9-23',
# minute='*/3',
second='15-45/30',
second='*/20',
# start_date=None,
# end_date=None,
# timezone=None,
Expand All @@ -349,7 +282,7 @@ async def check_dynamic(dy_uid):
misfire_grace_time=30
)
else:
# 检查池禁用, 日间
# 检查池禁用
scheduler.add_job(
bilibili_dynamic_monitor,
'cron',
Expand All @@ -358,32 +291,13 @@ async def check_dynamic(dy_uid):
# day='*/1',
# week=None,
# day_of_week=None,
hour='9-23',
# hour=None,
minute='*/3',
# second='*/30',
# start_date=None,
# end_date=None,
# timezone=None,
id='bilibili_dynamic_monitor_in_day_pool_disable',
coalesce=True,
misfire_grace_time=30
)
# 检查池禁用, 夜间
scheduler.add_job(
bilibili_dynamic_monitor,
'cron',
# year=None,
# month=None,
# day='*/1',
# week=None,
# day_of_week=None,
hour='0-8',
minute='*/15',
# second='*/30',
# start_date=None,
# end_date=None,
# timezone=None,
id='bilibili_dynamic_monitor_in_night_pool_disable',
id='bilibili_dynamic_monitor_pool_disable',
coalesce=True,
misfire_grace_time=30
)
Expand Down
Loading

0 comments on commit ea700a8

Please sign in to comment.