跳转至

TopApi

modules.top.TopApi

TopApi(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_category

get_category()

获取所有排行榜分类.

Source code in qqmusic_api/modules/top.py
def get_category(self):
    """获取所有排行榜分类."""
    return self._build_cgi(
        module="music.musicToplist.Toplist",
        method="GetAll",
        param={},
        response_model=TopCategoryResponse,
    )

get_detail

get_detail(
    top_id: int,
    num: int = 10,
    page: int = 1,
    *,
    tag: bool = True,
)

获取排行榜详情及其歌曲列表.

PARAMETER DESCRIPTION
top_id

排行榜 ID.

TYPE: int

num

返回歌曲数量.

TYPE: int DEFAULT: 10

page

页码.

TYPE: int DEFAULT: 1

tag

是否返回歌曲标签信息.

TYPE: bool DEFAULT: True

Source code in qqmusic_api/modules/top.py
def get_detail(
    self,
    top_id: int,
    num: int = 10,
    page: int = 1,
    *,
    tag: bool = True,
):
    """获取排行榜详情及其歌曲列表.

    Args:
        top_id: 排行榜 ID.
        num: 返回歌曲数量.
        page: 页码.
        tag: 是否返回歌曲标签信息.
    """
    param = {
        "topId": top_id,
        "offset": num * (page - 1),
        "num": num,
    }
    if tag:
        param["withTags"] = True

    return self._build_cgi(
        module="music.musicToplist.Toplist",
        method="GetDetail",
        param=param,
        preserve_bool=tag,
        response_model=TopDetailResponse,
        pager_strategy=OffsetStrategy[TopDetailResponse](
            offset_key="offset",
            page_size_key="num",
            total_extractor=lambda r: r.info.total_num,
            count_extractor=lambda r: len(r.songs),
        ),
    ).with_extractor(lambda r: r.songs)