Skip to content

Commit 8b365ec

Browse files
awordxAkimioJR
authored andcommitted
feat(LibraryPoster): 优化对多路径媒体库的处理
1 parent 499fbb3 commit 8b365ec

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

app/modules/libraryposter/poster.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ 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(
35+
self,
36+
parent_id: str,
37+
user_id: str,
38+
max_depth: int = 1,
39+
current_depth: int = 0
40+
) -> AsyncGenerator[dict[str, Any], None]:
41+
url = f"{self.__server_url}/Users/{user_id}/Items?ParentId={parent_id}&api_key={self.__api_key}"
42+
resp = await RequestUtils.get(url)
43+
44+
if not resp or resp.status_code != 200:
45+
return
46+
for item in resp.json().get("Items", []):
47+
if item.get("IsFolder", False) and current_depth < max_depth:
48+
# 递归获取子项
49+
async for sub_item in self.fetch_items(
50+
parent_id=item["Id"],
51+
user_id=user_id,
52+
max_depth=max_depth,
53+
current_depth=current_depth + 1
54+
):
55+
yield sub_item
56+
else:
57+
yield item
3458

3559
async def get_users(self) -> list[dict[str, Any]]:
3660
"""
@@ -81,6 +105,8 @@ async def get_library_items(
81105
:param user_id: 用户 ID(可选)
82106
:return: 媒体库项目列表
83107
"""
108+
max_depth = 0
109+
all_items = []
84110
if not user_id:
85111
users = await self.get_users()
86112
if not users:
@@ -96,8 +122,13 @@ async def get_library_items(
96122
f"获取 {library_id} 媒体库信息失败, 状态码: {resp.status_code if resp else '无响应'}"
97123
)
98124
return []
99-
100-
return resp.json()["Items"]
125+
while len(all_items) < 15 and max_depth <= 5:
126+
async for item in self.fetch_items(library_id, user_id, max_depth=max_depth):
127+
all_items.append(item)
128+
if len(all_items) >= 15:
129+
break
130+
max_depth += 1
131+
return all_items
101132

102133
async def download_item_image(
103134
self,

0 commit comments

Comments
 (0)