fix(maizone): 增强解析空间动态数据的健壮性以防止崩溃
QQ空间API在某些情况下可能返回非预期的数据格式,例如 `pictotal` 或 `commentlist` 字段为 `None` 而不是空列表。 之前的代码直接对这些字段进行迭代,当遇到非列表类型时会导致 `TypeError` 异常,从而中断动态的获取流程。 本次修改通过在处理图片和评论列表前添加 `isinstance` 类型检查,确保了只在数据结构符合预期时才进行操作,从而避免了因API返回数据格式异常而导致的程序崩溃。
This commit is contained in:
@@ -740,28 +740,42 @@ class QZoneService:
|
||||
feeds_list = []
|
||||
my_name = json_data.get("logininfo", {}).get("name", "")
|
||||
for msg in json_data.get("msglist", []):
|
||||
# 只有在处理好友说说时,才检查是否已评论并跳过
|
||||
commentlist = msg.get("commentlist")
|
||||
|
||||
# 只有在处理好友说说时,才检查是否已评论并跳过
|
||||
if not is_monitoring_own_feeds:
|
||||
is_commented = any(
|
||||
c.get("name") == my_name for c in msg.get("commentlist", []) if isinstance(c, dict)
|
||||
)
|
||||
is_commented = False
|
||||
if isinstance(commentlist, list):
|
||||
is_commented = any(
|
||||
c.get("name") == my_name for c in commentlist if isinstance(c, dict)
|
||||
)
|
||||
if is_commented:
|
||||
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 = []
|
||||
if "commentlist" in msg:
|
||||
for c in msg["commentlist"]:
|
||||
comments.append(
|
||||
{
|
||||
"qq_account": c.get("uin"),
|
||||
"nickname": c.get("name"),
|
||||
"content": c.get("content"),
|
||||
"comment_tid": c.get("tid"),
|
||||
"parent_tid": c.get("parent_tid"), # API直接返回了父ID
|
||||
}
|
||||
)
|
||||
if isinstance(commentlist, list):
|
||||
for c in commentlist:
|
||||
# 确保评论条目也是字典
|
||||
if isinstance(c, dict):
|
||||
comments.append(
|
||||
{
|
||||
"qq_account": c.get("uin"),
|
||||
"nickname": c.get("name"),
|
||||
"content": c.get("content"),
|
||||
"comment_tid": c.get("tid"),
|
||||
"parent_tid": c.get("parent_tid"),
|
||||
}
|
||||
)
|
||||
|
||||
feeds_list.append(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user