Skip to content

Commit d573a1d

Browse files
awordxAkimioJR
authored andcommitted
修改函数为生成器写法,并测试了jellyfine兼容
1 parent 8082260 commit d573a1d

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

app/modules/libraryposter/poster.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +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(self, parent_id: str, user_id: str, max_depth: int = 1, current_depth: int = 0) -> list[
35-
dict[str, Any]]:
36-
all_items = []
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]:
3741
url = f"{self.__server_url}/Users/{user_id}/Items?ParentId={parent_id}&api_key={self.__api_key}"
3842
resp = await RequestUtils.get(url)
3943

4044
if not resp or resp.status_code != 200:
41-
return []
42-
45+
return
4346
for item in resp.json().get("Items", []):
4447
if item.get("IsFolder", False) and current_depth < max_depth:
4548
# 递归获取子项
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)
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
5356
else:
54-
all_items.append(item)
55-
56-
return all_items
57+
yield item
5758

5859
async def get_users(self) -> list[dict[str, Any]]:
5960
"""
@@ -121,12 +122,12 @@ async def get_library_items(
121122
f"获取 {library_id} 媒体库信息失败, 状态码: {resp.status_code if resp else '无响应'}"
122123
)
123124
return []
124-
125-
while len(all_items) < 15 and max_depth <= 5:#如果当前目录获取的海报小于15张,那么增加一级文件夹层级获取更多海报,最多5个级别
126-
all_items = await self.fetch_items(library_id, user_id, max_depth=max_depth)
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
127130
max_depth += 1
128-
129-
logger.info(f'当前文件夹可用海报数目:{len(all_items)}')
130131
return all_items
131132

132133
async def download_item_image(

0 commit comments

Comments
 (0)