跳转至

SearchApi

modules.search.SearchApi

SearchApi(client: Client)

Bases: ApiModule

搜索相关 API.

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

get_hotkey

get_hotkey()

获取热搜词列表.

Source code in qqmusic_api/modules/search.py
def get_hotkey(self):
    """获取热搜词列表."""
    return self._build_request(
        "music.musicsearch.HotkeyService",
        "GetHotkeyForQQMusicMobile",
        {"search_id": get_searchID()},
    )

complete

complete(keyword: str)

搜索词补全建议.

PARAMETER DESCRIPTION
keyword

关键词.

TYPE: str

Source code in qqmusic_api/modules/search.py
def complete(self, keyword: str):
    """搜索词补全建议.

    Args:
        keyword: 关键词.
    """
    return self._build_request(
        "music.smartboxCgi.SmartBoxCgi",
        "GetSmartBoxResult",
        {
            "search_id": get_searchID(),
            "query": keyword,
            "num_per_page": 0,
            "page_idx": 0,
        },
    )
quick_search(keyword: str) -> dict[str, Any]

快速搜索 (直接返回解析后的 JSON 数据).

PARAMETER DESCRIPTION
keyword

关键词.

TYPE: str

RETURNS DESCRIPTION
dict[str, Any]

dict[str, Any]: 搜索结果字典.

Source code in qqmusic_api/modules/search.py
async def quick_search(self, keyword: str) -> dict[str, Any]:
    """快速搜索 (直接返回解析后的 JSON 数据).

    Args:
        keyword: 关键词.

    Returns:
        dict[str, Any]: 搜索结果字典.
    """
    resp = await self._client.fetch(
        "GET",
        "https://c.y.qq.com/splcloud/fcgi-bin/smartbox_new.fcg",
        params={"key": keyword},
    )
    resp.raise_for_status()
    return resp.json()["data"]
general_search(
    keyword: str, page: int = 1, *, highlight: bool = True
)

综合搜索.

PARAMETER DESCRIPTION
keyword

关键词.

TYPE: str

page

页码.

TYPE: int DEFAULT: 1

highlight

是否高亮关键词.

TYPE: bool DEFAULT: True

Source code in qqmusic_api/modules/search.py
def general_search(
    self,
    keyword: str,
    page: int = 1,
    *,
    highlight: bool = True,
):
    """综合搜索.

    Args:
        keyword: 关键词.
        page: 页码.
        highlight: 是否高亮关键词.
    """
    return self._build_request(
        "music.adaptor.SearchAdaptor",
        "do_search_v2",
        {
            "searchid": get_searchID(),
            "search_type": 100,
            "page_num": 15,
            "query": keyword,
            "page_id": page,
            "highlight": highlight,
            "grp": True,
        },
        response_model=GeneralSearchResponse,
        pager_meta=PagerMeta(
            strategy=MultiFieldContinuationStrategy(
                lambda params, response, adapter: {
                    **cast("dict[str, Any]", params),
                    "searchid": response.searchid,
                    "page_id": response.nextpage,
                    "page_start": cast("dict[str, Any]", adapter.get_cursor(response)),
                },
                context_name="general_search",
            ),
            adapter=ResponseAdapter(
                has_more_flag=lambda response: response.nextpage != -1,
                cursor="nextpage_start",
            ),
        ),
    )

search_by_type

search_by_type(
    keyword: str,
    search_type: int | SearchType = SONG,
    num: int = 10,
    page: int = 1,
    *,
    highlight: bool = True,
)

按类型搜索结果.

PARAMETER DESCRIPTION
keyword

关键词.

TYPE: str

search_type

搜索类型.

TYPE: int | SearchType DEFAULT: SONG

num

返回结果数量.

TYPE: int DEFAULT: 10

page

页码.

TYPE: int DEFAULT: 1

highlight

是否高亮关键词.

TYPE: bool DEFAULT: True

Source code in qqmusic_api/modules/search.py
def search_by_type(
    self,
    keyword: str,
    search_type: int | SearchType = SearchType.SONG,
    num: int = 10,
    page: int = 1,
    *,
    highlight: bool = True,
):
    """按类型搜索结果.

    Args:
        keyword: 关键词.
        search_type: 搜索类型.
        num: 返回结果数量.
        page: 页码.
        highlight: 是否高亮关键词.
    """
    normalized_search_type = int(SearchType(search_type))
    return self._build_request(
        "music.search.SearchCgiService",
        "DoSearchForQQMusicMobile",
        {
            "searchid": get_searchID(),
            "query": keyword,
            "search_type": normalized_search_type,
            "num_per_page": num,
            "page_num": page,
            "highlight": highlight,
            "grp": True,
        },
        platform=Platform.ANDROID,
        response_model=SearchByTypeResponse,
        pager_meta=PagerMeta(
            strategy=PageStrategy(page_key="page_num", page_size=num, start_page=page),
            adapter=ResponseAdapter(
                has_more_flag=lambda r: getattr(r, "nextpage", -1) != -1,
                total="total_num",
            ),
        ),
    )