Skip to content

Commit b577b12

Browse files
awordxAkimioJR
authored andcommitted
增加异步递归函数获取媒体库海报
1 parent e72d4cc commit b577b12

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

app/modules/libraryposter/poster.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ def __init__(
3131
self.__title_font_path = Path(title_font_path)
3232
self.__subtitle_font_path = Path(subtitle_font_path)
3333
self.__configs = configs
34+
async def fetch_items(self, parent_id: str, user_id: str, max_depth: int = 1, current_depth: int = 0) -> list[
35+
dict[str, Any]]:
36+
all_items = []
37+
url = f"{self.__server_url}/Users/{user_id}/Items?ParentId={parent_id}&api_key={self.__api_key}"
38+
resp = await RequestUtils.get(url)
39+
40+
if not resp or resp.status_code != 200:
41+
return []
42+
43+
for item in resp.json().get("Items", []):
44+
if item.get("IsFolder", False) and current_depth < max_depth:
45+
# 递归获取子项
46+
sub_items = await self.fetch_items(
47+
parent_id=item["Id"],
48+
user_id=user_id,
49+
max_depth=max_depth,
50+
current_depth=current_depth + 1
51+
)
52+
all_items.extend(sub_items)
53+
else:
54+
all_items.append(item)
55+
56+
return all_items
3457

3558
async def get_users(self) -> list[dict[str, Any]]:
3659
"""
@@ -70,7 +93,7 @@ async def get_libraries(self) -> list[dict[str, Any]]:
7093

7194
return resp.json()["Items"]
7295

73-
async def get_library_items(
96+
async def get_library_items(#修改的函数
7497
self,
7598
library_id: str,
7699
user_id: str = "",
@@ -81,6 +104,7 @@ async def get_library_items(
81104
:param user_id: 用户 ID(可选)
82105
:return: 媒体库项目列表
83106
"""
107+
max_depth = 0
84108
all_items = []
85109
if not user_id:
86110
users = await self.get_users()
@@ -97,16 +121,12 @@ async def get_library_items(
97121
f"获取 {library_id} 媒体库信息失败, 状态码: {resp.status_code if resp else '无响应'}"
98122
)
99123
return []
100-
for item in resp.json().get("Items", []):
101-
if item.get("IsFolder", False):
102-
# 递归获取子项
103-
url = f"{self.__server_url}/Users/{user_id}/Items?ParentId={item['Id']}&api_key={self.__api_key}"
104-
sub_items = await RequestUtils.get(url)
105-
if sub_items and sub_items.status_code == 200:
106-
all_items.extend(sub_items.json().get("Items", []))
107-
else:
108-
all_items.append(item)
109124

125+
while len(all_items) < 15 and max_depth <= 5:
126+
all_items = await self.fetch_items(library_id, user_id, max_depth=max_depth)
127+
max_depth += 1
128+
129+
logger.info(f'当前文件夹可用海报数目:{len(all_items)}')
110130
return all_items
111131

112132
async def download_item_image(

0 commit comments

Comments
 (0)