feat: Modify auto follow task to async task

This commit is contained in:
LWR
2023-07-23 01:54:56 +08:00
parent b9c414812d
commit 0d1fb946c5

View File

@@ -195,39 +195,41 @@ class StarBot:
uid = config.get("LOGIN_UID") uid = config.get("LOGIN_UID")
me = User(uid, get_credential()) me = User(uid, get_credential())
async def follow_task(uid_set): async def auto_follow_task():
for u in uid_set: try:
follow_user = User(u, get_credential()) follows = set()
await follow_user.modify_relation(RelationType.SUBSCRIBE) page = 1
await asyncio.sleep(10) while True:
logger.success(f"已成功关注了 {len(uid_set)} 个 UP 主") res = await me.get_followings(page)
follows = follows.union(set(map(lambda x: x["mid"], res["list"])))
if len(res["list"]) < 20:
break
page += 1
try: need_follow_uids = set()
follows = set() for u in self.__datasource.get_up_list():
page = 1 if u.uid != uid and any(map(lambda t: t.dynamic_update.enabled, u.targets)):
while True: need_follow_uids.add(u.uid)
res = await me.get_followings(page) need_follow_uids.difference_update(follows)
follows = follows.union(set(map(lambda x: x["mid"], res["list"])))
if len(res["list"]) < 20:
break
page += 1
need_follow_uids = set() if len(need_follow_uids) == 0:
for up in self.__datasource.get_up_list(): logger.success(f"不存在打开了动态推送但未关注的 UP 主")
if up.uid != uid and any(map(lambda d: d.enabled, map(lambda t: t.dynamic_update, up.targets))): return
need_follow_uids.add(up.uid)
need_follow_uids.difference_update(follows)
if len(need_follow_uids) > 0:
logger.info(f"检测到 {len(need_follow_uids)} 个打开了动态推送但未关注的 UP 主, 启动自动关注任务") logger.info(f"检测到 {len(need_follow_uids)} 个打开了动态推送但未关注的 UP 主, 启动自动关注任务")
asyncio.create_task(follow_task(need_follow_uids)) for i, u in enumerate(need_follow_uids):
else: follow_user = User(u, get_credential())
logger.success(f"不存在打开了动态推送但未关注的 UP 主") await follow_user.modify_relation(RelationType.SUBSCRIBE)
except ResponseCodeException as ex: await asyncio.sleep(10)
if ex.code == 22115 or ex.code == 22007: logger.success(f"已关注: {i + 1} / {len(need_follow_uids)}")
logger.warning(f"读取登录账号的关注列表失败, 请检查登录凭据是否已失效, 错误信息: {ex.msg}") logger.success(f"已成功关注了 {len(need_follow_uids)} 个 UP 主")
except Exception as ex: except ResponseCodeException as e:
logger.exception(f"读取登录账号的关注列表失败", ex) if e.code == 22115 or e.code == 22007:
logger.warning(f"读取登录账号的关注列表失败, 请检查登录凭据是否已失效, 错误信息: {e.msg}")
except Exception as e:
logger.exception(f"自动关注任务异常", e)
asyncio.create_task(auto_follow_task())
# 检测消息补发配置完整性 # 检测消息补发配置完整性
if config.get("BAN_RESEND") and config.get("MASTER_QQ") is None: if config.get("BAN_RESEND") and config.get("MASTER_QQ") is None: