refactor(maizone): 优化说说评论回复逻辑
重构了处理和回复评论的算法,以提高准确性和代码清晰度。 主要变更: - 在获取说说列表时引入 `is_monitoring_own_feeds` 参数,以区分处理自己和好友的说说,避免跳过对自己说说的评论检查。 - 优化了评论回复逻辑,通过将评论区分为用户评论和自己的回复,更精确地识别未回复的评论,防止了之前可能出现的重复回复或漏回复的问题。
This commit is contained in:
@@ -154,7 +154,8 @@ class QZoneService:
|
|||||||
# --- 第一步: 单独处理自己说说的评论 ---
|
# --- 第一步: 单独处理自己说说的评论 ---
|
||||||
if self.get_config("monitor.enable_auto_reply", False):
|
if self.get_config("monitor.enable_auto_reply", False):
|
||||||
try:
|
try:
|
||||||
own_feeds = await api_client["list_feeds"](qq_account, 5) # 获取自己最近5条说说
|
# 传入新参数,表明正在检查自己的说说
|
||||||
|
own_feeds = await api_client["list_feeds"](qq_account, 5, is_monitoring_own_feeds=True)
|
||||||
if own_feeds:
|
if own_feeds:
|
||||||
logger.info(f"获取到自己 {len(own_feeds)} 条说说,检查评论...")
|
logger.info(f"获取到自己 {len(own_feeds)} 条说说,检查评论...")
|
||||||
for feed in own_feeds:
|
for feed in own_feeds:
|
||||||
@@ -255,16 +256,17 @@ class QZoneService:
|
|||||||
if not comments:
|
if not comments:
|
||||||
return
|
return
|
||||||
|
|
||||||
# 找到所有我已经回复过的评论的ID
|
# 1. 将评论分为用户评论和自己的回复
|
||||||
replied_to_tids = {
|
user_comments = [c for c in comments if str(c.get('qq_account')) != str(qq_account)]
|
||||||
c['parent_tid'] for c in comments
|
my_replies = [c for c in comments if str(c.get('qq_account')) == str(qq_account)]
|
||||||
if c.get('parent_tid') and str(c.get('qq_account')) == str(qq_account)
|
|
||||||
}
|
|
||||||
|
|
||||||
# 找出所有非我发出且我未回复过的评论
|
# 2. 获取所有已经被我回复过的评论的ID
|
||||||
|
replied_comment_ids = {reply.get('parent_tid') for reply in my_replies if reply.get('parent_tid')}
|
||||||
|
|
||||||
|
# 3. 找出所有尚未被回复过的用户评论
|
||||||
comments_to_reply = [
|
comments_to_reply = [
|
||||||
c for c in comments
|
comment for comment in user_comments
|
||||||
if str(c.get('qq_account')) != str(qq_account) and c.get('comment_tid') not in replied_to_tids
|
if comment.get('comment_tid') not in replied_comment_ids
|
||||||
]
|
]
|
||||||
|
|
||||||
if not comments_to_reply:
|
if not comments_to_reply:
|
||||||
@@ -641,7 +643,7 @@ class QZoneService:
|
|||||||
logger.error(f"上传图片 {index+1} 异常: {e}", exc_info=True)
|
logger.error(f"上传图片 {index+1} 异常: {e}", exc_info=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def _list_feeds(t_qq: str, num: int) -> List[Dict]:
|
async def _list_feeds(t_qq: str, num: int, is_monitoring_own_feeds: bool = False) -> List[Dict]:
|
||||||
"""获取指定用户说说列表"""
|
"""获取指定用户说说列表"""
|
||||||
try:
|
try:
|
||||||
params = {
|
params = {
|
||||||
@@ -667,10 +669,14 @@ class QZoneService:
|
|||||||
feeds_list = []
|
feeds_list = []
|
||||||
my_name = json_data.get("logininfo", {}).get("name", "")
|
my_name = json_data.get("logininfo", {}).get("name", "")
|
||||||
for msg in json_data.get("msglist", []):
|
for msg in json_data.get("msglist", []):
|
||||||
|
# 只有在处理好友说说时,才检查是否已评论并跳过
|
||||||
|
if not is_monitoring_own_feeds:
|
||||||
is_commented = any(
|
is_commented = any(
|
||||||
c.get("name") == my_name for c in msg.get("commentlist", []) if isinstance(c, dict)
|
c.get("name") == my_name for c in msg.get("commentlist", []) if isinstance(c, dict)
|
||||||
)
|
)
|
||||||
if not is_commented:
|
if is_commented:
|
||||||
|
continue
|
||||||
|
|
||||||
images = [pic['url1'] for pic in msg.get('pictotal', []) if 'url1' in pic]
|
images = [pic['url1'] for pic in msg.get('pictotal', []) if 'url1' in pic]
|
||||||
|
|
||||||
comments = []
|
comments = []
|
||||||
|
|||||||
Reference in New Issue
Block a user