From 64e9b69881f247d6bf7e946b0f21c995fe504a99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=A7=81=E6=80=9D?= <5361064+zzbslayer@user.noreply.gitee.com> Date: Fri, 29 May 2020 03:03:05 +0800 Subject: [PATCH] Check weibo config from bot; other small fix --- hoshino/modules/weibo/__init__.py | 30 +++++++++++++++++++++++++----- hoshino/modules/weibo/weibo.py | 7 ++++++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/hoshino/modules/weibo/__init__.py b/hoshino/modules/weibo/__init__.py index 1b781b26e..9b4ac21d2 100644 --- a/hoshino/modules/weibo/__init__.py +++ b/hoshino/modules/weibo/__init__.py @@ -8,7 +8,7 @@ sample config.json [{ - "service_name": "bcr-weibo", + "service_name": "weibo-bcr", "enable_on_default": true, "users":[{ "user_id": "6603867494", @@ -47,7 +47,7 @@ def _load_config(services_config): -sv = Service('weibo-poller', use_priv=Priv.ADMIN, manage_priv=Priv.SUPERUSER, visible=False) +sv = Service('weibo-poller', manage_priv=Priv.SUPERUSER, visible=False) services_config = util.load_config(__file__) subr_dic = {} alias_dic = {} @@ -76,18 +76,38 @@ def wb_to_message(wb): return msg +weibo_url_prefix = "https://weibo.com/u" +@sv.on_command('weibo-config',aliases=('查看微博服务', '微博服务', '微博配置', '查看微博配置')) +async def weibo_config(session): + msg = '微博推送配置:服务名,别名,微博链接' + index = 1 + for service_config in services_config: + service_name = service_config['service_name'] + users_config = service_config['users'] + for user_config in users_config: + weibo_id = user_config['user_id'] + alias = user_config['alias'] + weibo_url = f'{weibo_url_prefix}/{weibo_id}' + msg = f'{msg}\n{index}. {service_name}, {alias}, {weibo_url}' + index+=1 + session.finish(msg) + + # @bot 看微博 alias @sv.on_command('看微博', only_to_me=True) async def get_last_5_weibo(session): uid = session.ctx['user_id'] if not lmt.check(uid): session.finish('您查询得过于频繁,请稍等片刻', at_sender=True) + return + lmt.start_cd(uid) alias = session.current_arg_text if alias not in alias_dic: - await session.finish(f"未找到微博: {alias}") + session.finish(f"未找到微博: {alias}") return + service_name = alias_dic[alias]["service_name"] user_id = alias_dic[alias]["user_id"] @@ -98,9 +118,9 @@ async def get_last_5_weibo(session): formatted_weibos = [wb_to_message(wb) for wb in last_5_weibos] for wb in formatted_weibos: await session.send(wb) - await session.finish(f"以上为 {alias} 的最新 {len(formatted_weibos)} 条微博") + session.finish(f"以上为 {alias} 的最新 {len(formatted_weibos)} 条微博") return - await session.finish(f"未找到微博: {alias}") + session.finish(f"未找到微博: {alias}") @sv.scheduled_job('interval', seconds=20*60) async def weibo_poller(): diff --git a/hoshino/modules/weibo/weibo.py b/hoshino/modules/weibo/weibo.py index 254d2924f..998fbb100 100644 --- a/hoshino/modules/weibo/weibo.py +++ b/hoshino/modules/weibo/weibo.py @@ -87,7 +87,12 @@ async def get_user_info(self, user_id): return user def clear_buffer(self): - self.received_weibo_ids.clear() + """ + 如果清理缓存前一分钟,该微博账号瞬间发送了 20 条微博 + 然后清理缓存仅仅保留后 10 条的微博id,因此可能会重复推送前 10 条微博 + 当然这种情况通常不会发生 + """ + self.received_weibo_ids = self.received_weibo_ids[-10:] def validate_config(self, config): """验证配置是否正确"""