diff --git a/bilibili_api/__init__.py b/bilibili_api/__init__.py index 52304060..3a9103c2 100644 --- a/bilibili_api/__init__.py +++ b/bilibili_api/__init__.py @@ -35,6 +35,7 @@ # api HEADERS, ) +from .utils.AsyncEvent import AsyncEvent from .utils.geetest import Geetest, GeetestMeta, GeetestType from .exceptions import ( ApiException, @@ -102,7 +103,7 @@ ) -BILIBILI_API_VERSION = "17.0.0" +BILIBILI_API_VERSION = "17.0.0+" def __register_all_clients(): @@ -130,6 +131,7 @@ def __register_all_clients(): __all__ = [ "ApiException", + "AsyncEvent", "ArgsException", "BILIBILI_API_VERSION", "BiliAPIClient", diff --git a/bilibili_api/utils/AsyncEvent.py b/bilibili_api/utils/AsyncEvent.py index aa362112..0dba2ec4 100644 --- a/bilibili_api/utils/AsyncEvent.py +++ b/bilibili_api/utils/AsyncEvent.py @@ -5,7 +5,7 @@ """ import asyncio -from typing import Callable, Coroutine +from typing import Callable, Coroutine, Union class AsyncEvent: @@ -19,13 +19,13 @@ def __init__(self): self.__handlers = {} self.__ignore_events = [] - def add_event_listener(self, name: str, handler: Coroutine) -> None: + def add_event_listener(self, name: str, handler: Union[Callable, Coroutine]) -> None: """ 注册事件监听器。 Args: - name (str): 事件名。 - handler (Coroutine): 回调异步函数。 + name (str) : 事件名。 + handler (Union[Callable, Coroutine]): 回调函数。 """ name = name.upper() if name not in self.__handlers: @@ -40,7 +40,7 @@ def on(self, event_name: str) -> Callable: event_name (str): 事件名。 """ - def decorator(func: Coroutine): + def decorator(func: Union[Callable, Coroutine]): self.add_event_listener(event_name, func) return func @@ -52,13 +52,13 @@ def remove_all_event_listener(self) -> None: """ self.__handlers = {} - def remove_event_listener(self, name: str, handler: Coroutine) -> bool: + def remove_event_listener(self, name: str, handler: Union[Callable, Coroutine]) -> bool: """ 移除事件监听函数。 Args: - name (str): 事件名。 - handler (Coroutine): 要移除的函数。 + name (str): 事件名。 + handler (Union[Callable, Coroutine]): 要移除的函数。 Returns: bool, 是否移除成功。 @@ -101,8 +101,10 @@ def dispatch(self, name: str, *args, **kwargs) -> None: name = name.upper() if name in self.__handlers: - for coroutine in self.__handlers[name]: - asyncio.create_task(coroutine(*args, **kwargs)) + for callableorcoroutine in self.__handlers[name]: + obj = callableorcoroutine(*args, **kwargs) + if isinstance(obj, Coroutine): + asyncio.create_task(obj) if name != "__ALL__": kwargs.update({"name": name, "data": args}) diff --git a/bilibili_api/utils/network.py b/bilibili_api/utils/network.py index d214248a..5187a007 100644 --- a/bilibili_api/utils/network.py +++ b/bilibili_api/utils/network.py @@ -126,7 +126,7 @@ def set_on(self, status: bool) -> None: """ self.__on = status - async def __handle_events(self, data: dict) -> None: + def __handle_events(self, data: dict) -> None: evt = data["name"] desc, real_data = data["data"] if ( diff --git a/docs/modules/bilibili_api.md b/docs/modules/bilibili_api.md index ba66e79b..17588317 100644 --- a/docs/modules/bilibili_api.md +++ b/docs/modules/bilibili_api.md @@ -14,6 +14,15 @@ from bilibili_api import bilibili_api - [class ApiException()](#class-ApiException) - [class ArgsException()](#class-ArgsException) +- [class AsyncEvent()](#class-AsyncEvent) + - [def \_\_init\_\_()](#def-\_\_init\_\_) + - [def add\_event\_listener()](#def-add\_event\_listener) + - [def dispatch()](#def-dispatch) + - [def ignore\_event()](#def-ignore\_event) + - [def on()](#def-on) + - [def remove\_all\_event\_listener()](#def-remove\_all\_event\_listener) + - [def remove\_event\_listener()](#def-remove\_event\_listener) + - [def remove\_ignore\_events()](#def-remove\_ignore\_events) - [class BiliAPIClient()](#class-BiliAPIClient) - [class BiliAPIFile()](#class-BiliAPIFile) - [class BiliAPIResponse()](#class-BiliAPIResponse) @@ -141,6 +150,112 @@ API 基类异常。 +--- + +## class AsyncEvent() + +发布-订阅模式异步事件类支持。 + +特殊事件:__ALL__ 所有事件均触发 + + + + +### def \_\_init\_\_() + + + + + +### def add_event_listener() + +注册事件监听器。 + + +| name | type | description | +| - | - | - | +| name | str | 事件名。 | +| handler | Union[Callable, Coroutine] | 回调函数。 | + +**Returns:** None + + + +### def dispatch() + +异步发布事件。 + + +| name | type | description | +| - | - | - | +| name | str | 事件名。 | +| *args, **kwargs: 要传递给函数的参数。 | | 要传递给函数的参数。 | + +**Returns:** None + + + +### def ignore_event() + +忽略指定事件 + + +| name | type | description | +| - | - | - | +| name | str | 事件名。 | + +**Returns:** None + + + +### def on() + +装饰器注册事件监听器。 + + +| name | type | description | +| - | - | - | +| event_name | str | 事件名。 | + +**Returns:** None + + + +### def remove_all_event_listener() + +移除所有事件监听函数 + + + +**Returns:** None + + + +### def remove_event_listener() + +移除事件监听函数。 + + +| name | type | description | +| - | - | - | +| name | str | 事件名。 | +| handler | Union[Callable, Coroutine] | 要移除的函数。 | + +**Returns:** bool, 是否移除成功。 + + + + +### def remove_ignore_events() + +移除所有忽略事件 + + + +**Returns:** None + + + --- ## class BiliAPIClient()