fix(maizone): 增强解析空间动态数据的健壮性以防止崩溃

QQ空间API在某些情况下可能返回非预期的数据格式,例如 `pictotal` 或 `commentlist` 字段为 `None` 而不是空列表。

之前的代码直接对这些字段进行迭代,当遇到非列表类型时会导致 `TypeError` 异常,从而中断动态的获取流程。

本次修改通过在处理图片和评论列表前添加 `isinstance` 类型检查,确保了只在数据结构符合预期时才进行操作,从而避免了因API返回数据格式异常而导致的程序崩溃。
This commit is contained in:
tt-P607
2025-10-01 12:39:23 +08:00
committed by Windpicker-owo
parent be3834641f
commit d46475ca8c

View File

@@ -740,26 +740,40 @@ 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", []):
# 只有在处理好友说说时,才检查是否已评论并跳过
commentlist = msg.get("commentlist")
# 只有在处理好友说说时,才检查是否已评论并跳过 # 只有在处理好友说说时,才检查是否已评论并跳过
if not is_monitoring_own_feeds: if not is_monitoring_own_feeds:
is_commented = False
if isinstance(commentlist, list):
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 commentlist if isinstance(c, dict)
) )
if is_commented: if is_commented:
continue continue
images = [pic["url1"] for pic in msg.get("pictotal", []) if "url1" in pic] # --- 安全地处理图片列表 ---
images = []
pictotal = msg.get("pictotal")
if isinstance(pictotal, list):
images = [
pic["url1"] for pic in pictotal if isinstance(pic, dict) and "url1" in pic
]
# --- 安全地处理评论列表 ---
comments = [] comments = []
if "commentlist" in msg: if isinstance(commentlist, list):
for c in msg["commentlist"]: for c in commentlist:
# 确保评论条目也是字典
if isinstance(c, dict):
comments.append( comments.append(
{ {
"qq_account": c.get("uin"), "qq_account": c.get("uin"),
"nickname": c.get("name"), "nickname": c.get("name"),
"content": c.get("content"), "content": c.get("content"),
"comment_tid": c.get("tid"), "comment_tid": c.get("tid"),
"parent_tid": c.get("parent_tid"), # API直接返回了父ID "parent_tid": c.get("parent_tid"),
} }
) )