Skip to content

Commit

Permalink
refactor: remove request_sync in channel_series.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo2011 committed Dec 15, 2024
1 parent 82dcb75 commit 3cdd02d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
46 changes: 26 additions & 20 deletions bilibili_api/channel_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand Down
29 changes: 17 additions & 12 deletions bilibili_api/utils/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 3cdd02d

Please sign in to comment.