CuteReimu / bilibili

哔哩哔哩bilibili的API的Go SDK
GNU Affero General Public License v3.0
87 stars 14 forks source link

获取评论接口,当评论包含`card_label`字段时,会产生`json.UnmarshalTypeError`错误 #86

Closed zhaojames0707 closed 2 weeks ago

zhaojames0707 commented 2 weeks ago

调用的方法

client.GetCommentsDetail(param)

请求

GetCommentsDetailParam {
        Type: 1, // 代表视频
    Oid:  113428324753030,
    Sort: 1,
}

返回

json: cannot unmarshal array into Go struct field Comment.data.replies.card_label of type bilibili.CardLabel

期望结果

No response

建议

似乎是接口返回的card_label字段为数组,但代码中定义为了结构体类型导致

CuteReimu commented 2 weeks ago

看起来是的,bilibili-api-collect有很多文档还是有一些小问题的。明天修复一下

CuteReimu commented 2 weeks ago

我看文档中给的示例是这样的:

{
    "folder": {
        "has_folded": false,
        "is_folded": false,
        "rule": "https://www.bilibili.com/blackboard/foldingreply.html"
    }
}

确实是obj。这就有点难搞了,要不我直接把Folder改成any怎么样?

    Content      CommentContent `json:"content"`       // 评论信息
    Replies      []*Comment     `json:"replies"`       // 评论回复条目预览。**仅嵌套一层**。否则为 null
    Assist       int            `json:"assist"`        // (?)
-   Folder       Folder         `json:"folder"`        // 折叠信息
+   Folder       any            `json:"folder"`        // 折叠信息
    UpAction     UpAction       `json:"up_action"`     // 评论 UP 主操作信息
    ShowFollow   bool           `json:"show_follow"`   // (?)

有什么好的建议吗?

zhaojames0707 commented 2 weeks ago

遇到的问题主要是 card_label 字段返回了数组而非对象,以及 card_label.type 字段是 int 而非 string,对应 comment.go 中的 Comment 以及 CardLabel 这两个结构体定义; 数据样例:

{
  "code" : 0,
  "message" : "0",
  "ttl" : 1,
  "data" : {
    "page" : {
      "num" : 1,
      "size" : 20,
      "count" : 1,
      "acount" : 1
    },
    "config" : {
      "showtopic" : 1,
      "show_up_flag" : true,
      "read_only" : false
    },
    "replies" : [ {
      "rpid" : 244976830800,
      "oid" : 113226075346138,
      "type" : 1,
      "mid" : 2035257198,
      "root" : 0,
      "parent" : 0,
      "dialog" : 0,
      "count" : 0,
      "rcount" : 0,
      "state" : 0,
      "fansgrade" : 0,
      "attr" : 256,
      "ctime" : 1729333214,
      "mid_str" : "2035257198",
      "oid_str" : "113226075346138",
      "rpid_str" : "244976830800",
      "root_str" : "0",
      "parent_str" : "0",
      "dialog_str" : "0",
      "like" : 1,
      "action" : 0,
      "member" : {
        "mid" : "2035257198",
        "uname" : "就是觉得不像那些",
        "sex" : "保密",
        "sign" : "",
        "avatar" : "https://i0.hdslb.com/bfs/face/798094bf491baf5ad3215bf7183c8e7f7bdcc0e8.jpg",
        "rank" : "10000",
        "face_nft_new" : 0,
        "is_senior_member" : 0,
        "senior" : { },
        "level_info" : {
          "current_level" : 3,
          "current_min" : 0,
          "current_exp" : 0,
          "next_exp" : 0
        },
        "pendant" : {
          "pid" : 0,
          "name" : "",
          "image" : "",
          "expire" : 0,
          "image_enhance" : "",
          "image_enhance_frame" : "",
          "n_pid" : 0
        },
        "nameplate" : {
          "nid" : 0,
          "name" : "",
          "image" : "",
          "image_small" : "",
          "level" : "",
          "condition" : ""
        },
        "official_verify" : {
          "type" : -1,
          "desc" : ""
        },
        "vip" : {
          "vipType" : 0,
          "vipDueDate" : 0,
          "dueRemark" : "",
          "accessStatus" : 0,
          "vipStatus" : 0,
          "vipStatusWarn" : "",
          "themeType" : 0,
          "label" : {
            "path" : "",
            "text" : "",
            "label_theme" : "",
            "text_color" : "",
            "bg_style" : 0,
            "bg_color" : "",
            "border_color" : "",
            "use_img_label" : true,
            "img_label_uri_hans" : "",
            "img_label_uri_hant" : "",
            "img_label_uri_hans_static" : "https://i0.hdslb.com/bfs/vip/d7b702ef65a976b20ed854cbd04cb9e27341bb79.png",
            "img_label_uri_hant_static" : "https://i0.hdslb.com/bfs/activity-plat/static/20220614/e369244d0b14644f5e1a06431e22a4d5/KJunwh19T5.png"
          },
          "avatar_subscript" : 0,
          "nickname_color" : ""
        },
        "fans_detail" : null,
        "user_sailing" : null,
        "is_contractor" : true,
        "contract_desc" : "原始粉丝",
        "nft_interaction" : null,
        "avatar_item" : {
          "container_size" : {
            "width" : 1.8,
            "height" : 1.8
          },
          "fallback_layers" : {
            "layers" : [ {
              "visible" : true,
              "general_spec" : {
                "pos_spec" : {
                  "coordinate_pos" : 2,
                  "axis_x" : 0.9,
                  "axis_y" : 0.9
                },
                "size_spec" : {
                  "width" : 1,
                  "height" : 1
                },
                "render_spec" : {
                  "opacity" : 1
                }
              },
              "layer_config" : {
                "tags" : {
                  "AVATAR_LAYER" : { },
                  "GENERAL_CFG" : {
                    "config_type" : 1,
                    "general_config" : {
                      "web_css_style" : {
                        "borderRadius" : "50%"
                      }
                    }
                  }
                },
                "is_critical" : true
              },
              "resource" : {
                "res_type" : 3,
                "res_image" : {
                  "image_src" : {
                    "src_type" : 1,
                    "placeholder" : 6,
                    "remote" : {
                      "url" : "https://i0.hdslb.com/bfs/face/798094bf491baf5ad3215bf7183c8e7f7bdcc0e8.jpg",
                      "bfs_style" : "widget-layer-avatar"
                    }
                  }
                }
              }
            } ],
            "is_critical_group" : true
          },
          "mid" : "2035257198"
        }
      },
      "content" : {
        "message" : "UP主加油!看好你噢~",
        "members" : [ ],
        "jump_url" : { },
        "max_line" : 6
      },
      "replies" : [ ],
      "assist" : 0,
      "up_action" : {
        "like" : true,
        "reply" : false
      },
      "invisible" : false,
      "card_label" : [ {
        "rpid" : 244976830800,
        "text_content" : "UP主觉得很赞",
        "text_color_day" : "#757575",
        "text_color_night" : "#939393",
        "label_color_day" : "#F4F4F4",
        "label_color_night" : "#1E1E1E",
        "image" : "",
        "type" : 0,
        "background" : "",
        "background_width" : 0,
        "background_height" : 0,
        "jump_url" : "",
        "effect" : 0,
        "effect_start_time" : 0
      } ],
      "reply_control" : {
        "up_like" : true,
        "max_line" : 6,
        "is_contractor" : true,
        "contract_desc" : "原始粉丝",
        "time_desc" : "24天前发布",
        "location" : "IP属地:未知"
      },
      "folder" : {
        "has_folded" : false,
        "is_folded" : false,
        "rule" : ""
      },
      "dynamic_id_str" : "0",
      "note_cvid_str" : "0",
      "track_info" : ""
    } ],
    "upper" : {
      "mid" : 602500278,
      "top" : null,
      "vote" : null
    },
    "top" : null,
    "vote" : 0,
    "blacklist" : 0,
    "assist" : 0,
    "mode" : 3,
    "support_mode" : [ 2, 3 ],
    "control" : {
      "input_disable" : false,
      "root_input_text" : "下面我简单喵两句",
      "child_input_text" : "下面我简单喵两句",
      "giveup_input_text" : "不发没关系,请继续友善哦~",
      "screenshot_icon_state" : 1,
      "upload_picture_icon_state" : 1,
      "answer_guide_text" : "需要升级成为lv2会员后才可以评论,先去答题转正吧!",
      "answer_guide_icon_url" : "http://i0.hdslb.com/bfs/emote/96940d16602cacbbac796245b7bb99fa9b5c970c.png",
      "answer_guide_ios_url" : "https://www.bilibili.com/h5/newbie/entry?navhide=1&re_src=12",
      "answer_guide_android_url" : "https://www.bilibili.com/h5/newbie/entry?navhide=1&re_src=6",
      "bg_text" : "",
      "empty_page" : null,
      "show_type" : 1,
      "show_text" : "",
      "web_selection" : false,
      "disable_jump_emote" : false,
      "enable_charged" : false,
      "enable_cm_biz_helper" : false,
      "preload_resources" : null
    },
    "folder" : {
      "has_folded" : false,
      "is_folded" : false,
      "rule" : ""
    }
  }
}

folder 字段暂时还没遇到问题哈

我看文档中给的示例是这样的:

{
    "folder": {
        "has_folded": false,
        "is_folded": false,
        "rule": "https://www.bilibili.com/blackboard/foldingreply.html"
    }
}

确实是obj。这就有点难搞了,要不我直接把Folder改成any怎么样?

    Content      CommentContent `json:"content"`       // 评论信息
    Replies      []*Comment     `json:"replies"`       // 评论回复条目预览。**仅嵌套一层**。否则为 null
    Assist       int            `json:"assist"`        // (?)
-   Folder       Folder         `json:"folder"`        // 折叠信息
+   Folder       any            `json:"folder"`        // 折叠信息
    UpAction     UpAction       `json:"up_action"`     // 评论 UP 主操作信息
    ShowFollow   bool           `json:"show_follow"`   // (?)

有什么好的建议吗?

CuteReimu commented 2 weeks ago

看错了。有这个例子我就方便改了,多谢