Skip to content

Commit

Permalink
feat: 自动重发机制
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Jun 26, 2024
1 parent a173710 commit ad6a065
Showing 1 changed file with 71 additions and 53 deletions.
124 changes: 71 additions & 53 deletions campux/social/mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class SocialPlatformManager:

invalid_count: int = 0

publishing_semaphore: asyncio.Semaphore = asyncio.Semaphore(1)
"""发布信号量,防止同时发布多个稿件"""

def __init__(self, ap: app.Application):
self.ap = ap
self.platform_api = qzone_api.QzoneAPI(ap)
Expand Down Expand Up @@ -64,56 +67,71 @@ async def can_operate(self) -> bool:
return await self.platform_api.token_valid()

async def publish_post(self, post_id: int):
try:
# 强制延迟
await asyncio.sleep(self.ap.config.campux_publish_post_time_delay)

post = await self.ap.cpx_api.get_post_info(post_id)

images_to_post = []

images_to_post.append(
await self.renderer.render(post)
)

for image_key in post.images:
image = await self.ap.cpx_api.download_image(image_key)
images_to_post.append(image)

await self.platform_api.publish_emotion(
f"#{post_id}"+self.ap.config.campux_publish_text_extra,
images_to_post
)

# 记录log
await self.ap.cpx_api.post_post_log(
post_id,
op=0,
old_stat="in_queue",
new_stat="in_queue",
comment=f"{self.ap.config.campux_qq_bot_uin} 发表稿件"
)
# 通知到hash
await self.ap.mq.mark_post_published(post_id)

# 通知到群里
asyncio.create_task(self.ap.imbot.send_group_message(
self.ap.config.campux_review_qq_group_id,
f"已成功发表:#{post.id}"
))
except Exception as e:

traceback.print_exc()
asyncio.create_task(self.ap.imbot.send_group_message(
self.ap.config.campux_review_qq_group_id,
f"发表失败:#{post_id}\n{str(e)}"
))

# 记录log
await self.ap.cpx_api.post_post_log(
post_id,
op=0,
old_stat="in_queue",
new_stat="in_queue",
comment=f"{self.ap.config.campux_qq_bot_uin} 发表失败: {str(e)}"
)
# 强制延迟
await asyncio.sleep(self.ap.config.campux_publish_post_time_delay)

max_retry = 3

async with self.publishing_semaphore:
for i in range(max_retry):
try:
await self._publish_post(post_id)
asyncio.create_task(self.ap.imbot.send_group_message(
self.ap.config.campux_review_qq_group_id,
f"已成功发表:#{post_id}"
))
return
except Exception as e:
nonebot.logger.error(f"发表稿件失败:{str(e)}")

await self.ap.cpx_api.post_post_log(
post_id,
op=0,
old_stat="in_queue",
new_stat="in_queue",
comment=f"{self.ap.config.campux_qq_bot_uin} 发表失败({i}): {str(e)}"
)

if i == max_retry - 1:
asyncio.create_task(self.ap.imbot.send_group_message(
self.ap.config.campux_review_qq_group_id,
f"发表失败:#{post_id}\n{str(e)}"
))
else:
await asyncio.sleep(5)

async def _publish_post(self, post_id: int):

post = await self.ap.cpx_api.get_post_info(post_id)

images_to_post = []

images_to_post.append(
await self.renderer.render(post)
)

for image_key in post.images:
image = await self.ap.cpx_api.download_image(image_key)
images_to_post.append(image)

await self.platform_api.publish_emotion(
f"#{post_id}"+self.ap.config.campux_publish_text_extra,
images_to_post
)

# 记录log
await self.ap.cpx_api.post_post_log(
post_id,
op=0,
old_stat="in_queue",
new_stat="in_queue",
comment=f"{self.ap.config.campux_qq_bot_uin} 发表稿件"
)
# 通知到hash
await self.ap.mq.mark_post_published(post_id)

# 通知到群里
asyncio.create_task(self.ap.imbot.send_group_message(
self.ap.config.campux_review_qq_group_id,
f"已成功发表:#{post.id}"
))

0 comments on commit ad6a065

Please sign in to comment.