@@ -31,30 +31,6 @@ 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
5834
5935 async def get_users (self ) -> list [dict [str , Any ]]:
6036 """
@@ -98,37 +74,53 @@ async def get_library_items(
9874 self ,
9975 library_id : str ,
10076 user_id : str = "" ,
101- ) -> list [dict [str , Any ]]:
77+ ) -> AsyncGenerator [dict [str , Any ], None ]:
10278 """
10379 获取指定媒体库的详细信息
10480 :param library_id: 媒体库 ID
10581 :param user_id: 用户 ID(可选)
10682 :return: 媒体库项目列表
10783 """
108- max_depth = 0
109- all_items = []
84+
85+ async def fetch_items (
86+ parent_id : str , user_id : str , max_depth : int = 1 , current_depth : int = 0
87+ ) -> AsyncGenerator [dict [str , Any ], None ]:
88+ """
89+ 递归获取媒体库项目
90+ """
91+ url = f"{ self .__server_url } /Users/{ user_id } /Items?ParentId={ parent_id } &api_key={ self .__api_key } "
92+ resp = await RequestUtils .get (url )
93+
94+ if not resp or resp .status_code != 200 :
95+ return
96+ for item in resp .json ().get ("Items" , []):
97+ if item .get ("IsFolder" , False ) and current_depth < max_depth :
98+ # 递归获取子项
99+ async for sub_item in fetch_items (
100+ parent_id = item ["Id" ],
101+ user_id = user_id ,
102+ max_depth = max_depth ,
103+ current_depth = current_depth + 1 ,
104+ ):
105+ yield sub_item
106+ else :
107+ yield item
108+
110109 if not user_id :
111110 users = await self .get_users ()
112111 if not users :
113- logger .warning ("未找到任何用户,无法获取媒体库项目" )
114- return []
112+ raise Exception ("未找到任何用户,无法获取媒体库项目" )
115113 user_id = users [0 ]["Id" ] # 默认使用第一个用户
116114
117115 url = f"{ self .__server_url } /Users/{ user_id } /Items?ParentId={ library_id } &api_key={ self .__api_key } "
118116 resp = await RequestUtils .get (url )
119117
120118 if resp is None or resp .status_code != 200 :
121- logger . warning (
119+ raise Exception (
122120 f"获取 { library_id } 媒体库信息失败, 状态码: { resp .status_code if resp else '无响应' } "
123121 )
124- return []
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
122+
123+ return fetch_items (library_id , user_id , 5 )
132124
133125 async def download_item_image (
134126 self ,
@@ -161,8 +153,8 @@ async def download_library_poster(
161153 :return:
162154 """
163155 logger .info (f"开始下载 { library ['Name' ]} 媒体库的海报图片" )
164- for items in await self .get_library_items (library ["Id" ]):
165- image = await self .download_item_image (items )
156+ async for item in self .get_library_items (library ["Id" ]):
157+ image = await self .download_item_image (item )
166158 if image is not None :
167159 yield image
168160
0 commit comments