跳转至

CommentApi

modules.comment.CommentApi

CommentApi(client: Client)

Bases: ApiModule

评论 API.

Source code in qqmusic_api/modules/_base.py
def __init__(self, client: "Client") -> None:
    self._client = client
    self._session = client._session

get_comment_count

get_comment_count(
    biz_id: int,
    biz_type: int | CommentBizType = SONG,
    biz_sub_type: int | None = None,
)

获取歌曲评论数量.

PARAMETER DESCRIPTION
biz_id

业务 ID.

TYPE: int

biz_type

业务类型, 默认为普通歌曲.

TYPE: int | CommentBizType DEFAULT: SONG

biz_sub_type

业务子类型.

TYPE: int | None DEFAULT: None

Source code in qqmusic_api/modules/comment.py
def get_comment_count(
    self,
    biz_id: int,
    biz_type: int | CommentBizType = CommentBizType.SONG,
    biz_sub_type: int | None = None,
):
    """获取歌曲评论数量.

    Args:
        biz_id: 业务 ID.
        biz_type: 业务类型, 默认为普通歌曲.
        biz_sub_type: 业务子类型.
    """
    # 支持 request_list
    req_data: dict[str, Any] = {
        "biz_id": str(biz_id),
        "biz_type": int(biz_type),
    }
    if biz_sub_type is not None:
        req_data["biz_sub_type"] = biz_sub_type
    elif biz_type == CommentBizType.SONG:
        req_data["biz_sub_type"] = 2

    data = {"request": req_data}
    return self._build_cgi(
        "music.globalComment.CommentCountSrv",
        "GetCmCount",
        data,
        response_model=CommentCountResponse,
    )

get_hot_comments

get_hot_comments(
    biz_id: int,
    page_num: int = 1,
    page_size: int = 15,
    last_comment_seq_no: str = "",
    biz_type: int | CommentBizType = SONG,
    biz_sub_type: int | None = None,
)

获取歌曲热评.

PARAMETER DESCRIPTION
biz_id

业务 ID.

TYPE: int

page_num

页码.

TYPE: int DEFAULT: 1

page_size

每页数量.

TYPE: int DEFAULT: 15

last_comment_seq_no

上一页最后一条评论 ID (可选).

TYPE: str DEFAULT: ''

biz_type

业务类型.

TYPE: int | CommentBizType DEFAULT: SONG

biz_sub_type

业务子类型.

TYPE: int | None DEFAULT: None

Source code in qqmusic_api/modules/comment.py
def get_hot_comments(
    self,
    biz_id: int,
    page_num: int = 1,
    page_size: int = 15,
    last_comment_seq_no: str = "",
    biz_type: int | CommentBizType = CommentBizType.SONG,
    biz_sub_type: int | None = None,
):
    """获取歌曲热评.

    Args:
        biz_id: 业务 ID.
        page_num: 页码.
        page_size: 每页数量.
        last_comment_seq_no: 上一页最后一条评论 ID (可选).
        biz_type: 业务类型.
        biz_sub_type: 业务子类型.
    """
    params: dict[str, Any] = {
        "BizType": int(biz_type),
        "BizId": str(biz_id),
        "LastCommentSeqNo": last_comment_seq_no,
        "PageSize": page_size,
        "PageNum": page_num - 1,
        "HotType": 1,
        "WithAirborne": 0,
        "PicEnable": 1,
    }
    if biz_sub_type is not None:
        params["BizSubType"] = biz_sub_type
    return self._build_cgi(
        "music.globalComment.CommentRead",
        "GetHotCommentList",
        params,
        response_model=CommentListResponse,
        pager_strategy=_build_comment_pager_strategy(),
    ).with_extractor(lambda r: r.comments)

get_new_comments

get_new_comments(
    biz_id: int,
    page_num: int = 1,
    page_size: int = 15,
    last_comment_seq_no: str = "",
    biz_type: int | CommentBizType = SONG,
    biz_sub_type: int | None = None,
)

获取歌曲最新评论.

PARAMETER DESCRIPTION
biz_id

业务 ID.

TYPE: int

page_num

页码.

TYPE: int DEFAULT: 1

page_size

每页数量.

TYPE: int DEFAULT: 15

last_comment_seq_no

上一页最后一条评论 ID (可选).

TYPE: str DEFAULT: ''

biz_type

业务类型.

TYPE: int | CommentBizType DEFAULT: SONG

biz_sub_type

业务子类型.

TYPE: int | None DEFAULT: None

Source code in qqmusic_api/modules/comment.py
def get_new_comments(
    self,
    biz_id: int,
    page_num: int = 1,
    page_size: int = 15,
    last_comment_seq_no: str = "",
    biz_type: int | CommentBizType = CommentBizType.SONG,
    biz_sub_type: int | None = None,
):
    """获取歌曲最新评论.

    Args:
        biz_id: 业务 ID.
        page_num: 页码.
        page_size: 每页数量.
        last_comment_seq_no: 上一页最后一条评论 ID (可选).
        biz_type: 业务类型.
        biz_sub_type: 业务子类型.
    """
    params: dict[str, Any] = {
        "PageSize": page_size,
        "PageNum": page_num - 1,
        "HashTagID": "",
        "BizType": int(biz_type),
        "PicEnable": 1,
        "LastCommentSeqNo": last_comment_seq_no,
        "SelfSeeEnable": 1,
        "BizId": str(biz_id),
        "AudioEnable": 1,
    }
    if biz_sub_type is not None:
        params["BizSubType"] = biz_sub_type
    return self._build_cgi(
        "music.globalComment.CommentRead",
        "GetNewCommentList",
        params,
        response_model=CommentListResponse,
        pager_strategy=_build_comment_pager_strategy(),
    ).with_extractor(lambda r: r.comments)

get_recommend_comments

get_recommend_comments(
    biz_id: int,
    page_num: int = 1,
    page_size: int = 15,
    last_comment_seq_no: str = "",
    biz_type: int | CommentBizType = SONG,
    biz_sub_type: int | None = None,
)

获取歌曲推荐评论.

PARAMETER DESCRIPTION
biz_id

业务 ID.

TYPE: int

page_num

页码.

TYPE: int DEFAULT: 1

page_size

每页数量.

TYPE: int DEFAULT: 15

last_comment_seq_no

上一页最后一条评论 ID (可选).

TYPE: str DEFAULT: ''

biz_type

业务类型.

TYPE: int | CommentBizType DEFAULT: SONG

biz_sub_type

业务子类型.

TYPE: int | None DEFAULT: None

Source code in qqmusic_api/modules/comment.py
def get_recommend_comments(
    self,
    biz_id: int,
    page_num: int = 1,
    page_size: int = 15,
    last_comment_seq_no: str = "",
    biz_type: int | CommentBizType = CommentBizType.SONG,
    biz_sub_type: int | None = None,
):
    """获取歌曲推荐评论.

    Args:
        biz_id: 业务 ID.
        page_num: 页码.
        page_size: 每页数量.
        last_comment_seq_no: 上一页最后一条评论 ID (可选).
        biz_type: 业务类型.
        biz_sub_type: 业务子类型.
    """
    params: dict[str, Any] = {
        "PageSize": page_size,
        "PageNum": page_num - 1,
        "BizType": int(biz_type),
        "PicEnable": 1,
        "Flag": 1,
        "LastCommentSeqNo": last_comment_seq_no,
        "CmListUIVer": 1,
        "BizId": str(biz_id),
        "AudioEnable": 1,
    }
    if biz_sub_type is not None:
        params["BizSubType"] = biz_sub_type
    return self._build_cgi(
        "music.globalComment.CommentRead",
        "GetRecCommentList",
        params,
        response_model=CommentListResponse,
        pager_strategy=_build_comment_pager_strategy(),
    ).with_extractor(lambda r: r.comments)

get_moment_comments

get_moment_comments(
    biz_id: int,
    page_size: int = 15,
    last_comment_seq_no: str = "",
    biz_type: int | CommentBizType = SONG,
    biz_sub_type: int | None = None,
)

获取歌曲时刻评论.

PARAMETER DESCRIPTION
biz_id

业务 ID.

TYPE: int

page_size

每页数量.

TYPE: int DEFAULT: 15

last_comment_seq_no

上一页最后一条评论 ID (可选).

TYPE: str DEFAULT: ''

biz_type

业务类型.

TYPE: int | CommentBizType DEFAULT: SONG

biz_sub_type

业务子类型.

TYPE: int | None DEFAULT: None

Source code in qqmusic_api/modules/comment.py
def get_moment_comments(
    self,
    biz_id: int,
    page_size: int = 15,
    last_comment_seq_no: str = "",
    biz_type: int | CommentBizType = CommentBizType.SONG,
    biz_sub_type: int | None = None,
):
    """获取歌曲时刻评论.

    Args:
        biz_id: 业务 ID.
        page_size: 每页数量.
        last_comment_seq_no: 上一页最后一条评论 ID (可选).
        biz_type: 业务类型.
        biz_sub_type: 业务子类型.
    """
    params: dict[str, Any] = {
        "LastPos": last_comment_seq_no,
        "HashTagID": "",
        "SeekTs": -1,
        "Size": page_size,
        "BizType": int(biz_type),
        "BizId": str(biz_id),
    }
    if biz_sub_type is not None:
        params["BizSubType"] = biz_sub_type
    return self._build_cgi(
        "music.globalComment.SongTsComment",
        "GetSongTsCmList",
        params,
        response_model=MomentCommentResponse,
        pager_strategy=CursorStrategy[MomentCommentResponse](
            cursor_key="LastPos",
            has_more_extractor=lambda response: response.has_more == 1,
            cursor_extractor=lambda response: response.next_pos,
        ),
    ).with_extractor(lambda r: r.comments)

add_comment

add_comment(
    biz_id: int,
    content: str,
    reply_cmt_id: str | None = None,
    biz_type: int | CommentBizType = SONG,
    biz_sub_type: int | None = None,
    credential: Credential | None = None,
)

添加评论.

PARAMETER DESCRIPTION
biz_id

业务 ID.

TYPE: int

content

评论内容.

TYPE: str

reply_cmt_id

回复的评论 ID.

TYPE: str | None DEFAULT: None

biz_type

业务类型.

TYPE: int | CommentBizType DEFAULT: SONG

biz_sub_type

业务子类型.

TYPE: int | None DEFAULT: None

credential

登录凭据.

TYPE: Credential | None DEFAULT: None

Source code in qqmusic_api/modules/comment.py
def add_comment(
    self,
    biz_id: int,
    content: str,
    reply_cmt_id: str | None = None,
    biz_type: int | CommentBizType = CommentBizType.SONG,
    biz_sub_type: int | None = None,
    credential: Credential | None = None,
):
    """添加评论.

    Args:
        biz_id: 业务 ID.
        content: 评论内容.
        reply_cmt_id: 回复的评论 ID.
        biz_type: 业务类型.
        biz_sub_type: 业务子类型.
        credential: 登录凭据.
    """
    req_data: dict[str, Any] = {
        "Content": content,
        "BizType": int(biz_type),
        "BizId": str(biz_id),
    }
    if reply_cmt_id is not None:
        req_data["RepliedCmId"] = reply_cmt_id
    if biz_sub_type is not None:
        req_data["BizSubType"] = biz_sub_type

    return self._build_cgi(
        "music.globalComment.CommentWriteServer",
        "AddComment",
        req_data,
        credential=credential,
        response_model=AddCommentResponse,
        require_login=True,
    )

delete_comment async

delete_comment(
    cm_id: str, credential: Credential | None = None
) -> bool

删除评论.

PARAMETER DESCRIPTION
cm_id

评论 ID.

TYPE: str

credential

登录凭据.

TYPE: Credential | None DEFAULT: None

RETURNS DESCRIPTION
bool

是否删除成功,评论不存在也为 True.

Source code in qqmusic_api/modules/comment.py
async def delete_comment(
    self,
    cm_id: str,
    credential: Credential | None = None,
) -> bool:
    """删除评论.

    Args:
        cm_id: 评论 ID.
        credential: 登录凭据.

    Returns:
        是否删除成功,评论不存在也为 True.
    """
    data = await self._build_cgi(
        "music.globalComment.CommentWriteServer",
        "DelComment",
        {
            "CommentId": cm_id,
        },
        credential=credential,
        require_login=True,
    )
    return data.get("SubCode", 0) == 0