diff --git a/bilibili_api/channel_series.py b/bilibili_api/channel_series.py index 94dc33ee..aca9e5da 100644 --- a/bilibili_api/channel_series.py +++ b/bilibili_api/channel_series.py @@ -79,35 +79,39 @@ def __init__( self.is_new = type_.value self.id_ = id_ self.owner = User(self.__uid, credential=credential) - self.credential: Credential = credential + self.credential: Credential = credential if credential else Credential() self.meta = None - if not f"{type_.value}-{id_}" in channel_meta_cache.keys(): - if self.is_new: - api = API_USER["channel_series"]["season_info"] - params = {"season_id": self.id_} - else: - api = API_USER["channel_series"]["info"] - params = {"series_id": self.id_} - resp = Api(**api).update_params(**params).result_sync - if self.is_new: - self.meta = resp["info"] - self.meta["mid"] = resp["info"]["upper"]["mid"] - self.__uid = self.meta["mid"] - self.owner = User(self.__uid, credential=credential) - else: - self.meta = resp["meta"] - self.__uid = self.meta["mid"] - self.owner = User(self.__uid, credential=credential) - else: + if f"{type_.value}-{id_}" in channel_meta_cache.keys(): self.meta = channel_meta_cache[f"{type_.value}-{id_}"] - def get_meta(self) -> dict: + async def __fetch_meta(self) -> None: + from .user import User + if self.is_new: + api = API_USER["channel_series"]["season_info"] + params = {"season_id": self.id_} + else: + api = API_USER["channel_series"]["info"] + params = {"series_id": self.id_} + resp = await Api(**api).update_params(**params).result + if self.is_new: + self.meta = resp["info"] + self.meta["mid"] = resp["info"]["upper"]["mid"] + self.__uid = self.meta["mid"] + self.owner = User(self.__uid, credential=self.credential) + else: + self.meta = resp["meta"] + self.__uid = self.meta["mid"] + self.owner = User(self.__uid, credential=self.credential) + + async def get_meta(self) -> dict: """ 获取元数据 Returns: 调用 API 返回的结果 """ + if not self.meta: + await self.__fetch_meta() return self.meta # type: ignore async def get_videos( @@ -125,6 +129,8 @@ async def get_videos( Returns: 调用 API 返回的结果 """ + if self.__uid == -1: + await self.__fetch_meta() if self.is_new: return await self.owner.get_channel_videos_season(self.id_, sort, pn, ps) else: diff --git a/bilibili_api/utils/sync.py b/bilibili_api/utils/sync.py index e5f938a2..03622213 100644 --- a/bilibili_api/utils/sync.py +++ b/bilibili_api/utils/sync.py @@ -5,24 +5,29 @@ """ import asyncio -from concurrent.futures import ThreadPoolExecutor -from typing import Any, Coroutine +from typing import Any, TypeVar, Coroutine +T = TypeVar("T") -def sync(coroutine: Coroutine) -> Any: + +def __ensure_event_loop() -> None: + try: + asyncio.get_event_loop() + + except: + asyncio.set_event_loop(asyncio.new_event_loop()) + + +def sync(coroutine: Coroutine[Any, Any, T]) -> T: """ 同步执行异步函数,使用可参考 [同步执行异步代码](https://nemo2011.github.io/bilibili-api/#/sync-executor) Args: - coroutine (Coroutine): 执行异步函数所创建的协程对象 + coroutine (Coroutine): 异步函数 Returns: - 该协程对象的返回值 + 该异步函数的返回值 """ - try: - asyncio.get_running_loop() - except RuntimeError: - return asyncio.run(coroutine) - else: - with ThreadPoolExecutor() as executor: - return executor.submit(asyncio.run, coroutine).result() + __ensure_event_loop() + loop = asyncio.get_event_loop() + return loop.run_until_complete(coroutine)