跳转至

client

core.client

QQMusic API 客户端.

Client

Client(
    credential: Credential | None = None,
    *,
    platform: Platform | None = None,
    device_path: str | None = None,
    max_concurrency: int | None = None,
    transport: Transport | None = None,
)

QQMusic API Client.

初始化客户端实例.

PARAMETER DESCRIPTION
credential

全局默认凭证.

TYPE: Credential | None DEFAULT: None

platform

全局默认请求平台.

TYPE: Platform | None DEFAULT: None

device_path

设备信息文件路径.

TYPE: str | None DEFAULT: None

max_concurrency

共享并发容量与分区 worker 上限. 必须为正整数,
默认为 20.

TYPE: int | None DEFAULT: None

transport

外部注入的传输实现 (满足 Transport 协议); 注入后
该实例生命周期归 Client 所有, close 时一并关闭. 缺省时
构建内置 NiquestsTransport.

TYPE: Transport | None DEFAULT: None

RAISES DESCRIPTION
ValueError

max_concurrency 非正整数.

Source code in qqmusic_api/core/client.py
def __init__(
    self,
    credential: Credential | None = None,
    *,
    platform: Platform | None = None,
    device_path: str | None = None,
    max_concurrency: int | None = None,
    transport: Transport | None = None,
):
    """初始化客户端实例.

    Args:
        credential: 全局默认凭证.
        platform: 全局默认请求平台.
        device_path: 设备信息文件路径.
        max_concurrency: 共享并发容量与分区 worker 上限. 必须为正整数,
            默认为 20.
        transport: 外部注入的传输实现 (满足 Transport 协议); 注入后
            该实例生命周期归 Client 所有, close 时一并关闭. 缺省时
            构建内置 NiquestsTransport.

    Raises:
        ValueError: max_concurrency 非正整数.
    """
    if max_concurrency is not None and (not isinstance(max_concurrency, int) or max_concurrency <= 0):
        raise ValueError("max_concurrency 必须为正整数")

    self._credential = credential or Credential()
    self._platform = platform or Platform.ANDROID
    max_concurrency_val = max_concurrency or DEFAULT_MAX_CONCURRENCY
    self._engine = RequestEngine.create(
        device_path=device_path,
        max_concurrency=max_concurrency_val,
        transport=transport,
    )

credential property writable

credential: Credential

获取当前全局凭证.

platform property writable

platform: Platform

获取当前全局默认平台.

version_policy property

version_policy: VersionPolicy

获取请求引擎使用的版本策略.

helper cached property

helper: HelperApi

辅助模块.

comment cached property

comment: CommentApi

评论模块.

private_message cached property

private_message: PrivateMessageApi

私信模块.

recommend cached property

recommend: RecommendApi

推荐模块.

top cached property

top: TopApi

排行榜模块.

album cached property

album: AlbumApi

专辑模块.

mv cached property

mv: MvApi

MV 模块.

login cached property

login: LoginApi

登录模块.

search cached property

search: SearchApi

搜索模块.

lyric cached property

lyric: LyricApi

歌词模块.

singer cached property

singer: SingerApi

歌手模块.

song cached property

song: SongApi

歌曲模块.

songlist cached property

songlist: SonglistApi

歌单模块.

sound_power cached property

sound_power: SoundPowerApi

音响力与听歌榜模块.

user cached property

user: UserApi

用户模块.

close async

close() -> None

关闭客户端并释放全部网络与引擎资源.

幂等操作, 委托 RequestEngine 关闭底层传输与清理在途操作.

Source code in qqmusic_api/core/client.py
async def close(self) -> None:
    """关闭客户端并释放全部网络与引擎资源.

    幂等操作, 委托 RequestEngine 关闭底层传输与清理在途操作.
    """
    await self._engine.close()

execute async

execute(request: BaseRequest[ResultT]) -> ResultT

执行单个请求描述符并解析响应结果.

PARAMETER DESCRIPTION
request

要执行的请求描述符.

TYPE: BaseRequest[ResultT]

RETURNS DESCRIPTION
ResultT

解析后的响应模型实例或原始数据字典.

Source code in qqmusic_api/core/client.py
async def execute(self, request: BaseRequest[ResultT]) -> ResultT:
    """执行单个请求描述符并解析响应结果.

    Args:
        request: 要执行的请求描述符.

    Returns:
        解析后的响应模型实例或原始数据字典.
    """
    scope = self._resolve_scope(request)
    return await self._engine.execute(request, scope)

stream async

stream(
    request: HttpRequest[Any],
) -> AsyncGenerator[RawStream, None]

打开流式响应租约.

进入上下文时按需打开响应流, 退出上下文时保证释放底层网络租约.

PARAMETER DESCRIPTION
request

要以流式方式执行的 HTTP 请求描述符.

TYPE: HttpRequest[Any]

YIELDS DESCRIPTION
AsyncGenerator[RawStream, None]

原始数据流对象.

RAISES DESCRIPTION
TypeError

请求类型不支持流式, 或传输实现无流式能力.

Source code in qqmusic_api/core/client.py
@asynccontextmanager
async def stream(self, request: HttpRequest[Any]) -> AsyncGenerator[RawStream, None]:
    """打开流式响应租约.

    进入上下文时按需打开响应流, 退出上下文时保证释放底层网络租约.

    Args:
        request: 要以流式方式执行的 HTTP 请求描述符.

    Yields:
        原始数据流对象.

    Raises:
        TypeError: 请求类型不支持流式, 或传输实现无流式能力.
    """
    scope = self._resolve_scope(request)
    async with self._engine.open_stream(request, scope) as raw_stream:
        yield raw_stream

gather async

gather(
    requests: Iterable[BaseRequest[ResultT]],
    *,
    batch_size: int = 20,
    return_exceptions: Literal[False] = False,
) -> list[ResultT]
gather(
    requests: Iterable[BaseRequest[Any]],
    *,
    batch_size: int = 20,
    return_exceptions: Literal[True],
) -> list[Any]
gather(
    requests: Iterable[BaseRequest[Any]],
    *,
    batch_size: int = 20,
    return_exceptions: bool = False,
) -> list[Any]
gather(
    requests: Iterable[BaseRequest[Any]],
    *,
    batch_size: int = 20,
    return_exceptions: bool = False,
) -> list[Any]

批量并发执行一组请求描述符并返回其执行结果.

Source code in qqmusic_api/core/client.py
async def gather(
    self,
    requests: Iterable[BaseRequest[Any]],
    *,
    batch_size: int = 20,
    return_exceptions: bool = False,
) -> list[Any]:
    """批量并发执行一组请求描述符并返回其执行结果."""
    calls = [RequestCall(request=req, scope=self._resolve_scope(req)) for req in requests]
    return await self._engine.gather(
        calls,
        batch_size=batch_size,
        return_exceptions=return_exceptions,
    )