From e1b8dda59263f3efdffb9cd4b63028c6951172ce 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: Thu, 21 May 2020 17:47:00 +0800 Subject: [PATCH] FreqLimiter for weibo --- hoshino/modules/weibo/__init__.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/hoshino/modules/weibo/__init__.py b/hoshino/modules/weibo/__init__.py index e857cc57..5e29d154 100644 --- a/hoshino/modules/weibo/__init__.py +++ b/hoshino/modules/weibo/__init__.py @@ -18,6 +18,9 @@ }] ''' + +lmt = util.FreqLimiter(5) + def _load_config(services_config): for sv_config in services_config: sv.logger.debug(sv_config) @@ -78,6 +81,11 @@ def wb_to_message(wb): # @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) + lmt.start_cd(uid) + alias = session.current_arg_text if alias not in alias_dic: await session.finish(f"未找到微博: {alias}") @@ -88,6 +96,7 @@ async def get_last_5_weibo(session): spiders = subr_dic[service_name]["spiders"] for spider in spiders: if spider.get_user_id() == user_id: + await spider.get_latest_weibos() last_5_weibos = spider.get_last_5_weibos() formatted_weibos = [wb_to_message(wb) for wb in last_5_weibos] for wb in formatted_weibos: