@@ -31,7 +31,8 @@ def get_queue(arr_url: str, arr_api_key: str, arr_type: str = "sonarr") -> List[
3131 # Queue response has a 'records' field containing the actual queue items
3232 return queue_data .get ('records' , [])
3333 except Exception as e :
34- logger .error ("Error fetching queue from %s at %s: %s" , arr_type , arr_url , e )
34+ logger .error ("Error fetching queue from %s at %s: %s" ,
35+ arr_type , arr_url , e )
3536 return []
3637
3738
@@ -53,7 +54,7 @@ def get_stuck_items(arr_url: str, arr_api_key: str, arr_type: str = "sonarr") ->
5354 for item in queue :
5455 status = item .get ('status' , '' ).lower ()
5556 status_messages = item .get ('statusMessages' , [])
56-
57+
5758 # Check if status is "downloadClientUnavailable" or if statusMessages indicate it
5859 is_stuck = False
5960 if status == 'downloadclientunavailable' :
@@ -103,12 +104,14 @@ def remove_from_queue(arr_url: str, arr_api_key: str, queue_id: int, arr_type: s
103104 'blocklist' : blocklist
104105 }
105106
106- response = requests .delete (api_url , headers = headers , params = params , timeout = 10 )
107+ response = requests .delete (
108+ api_url , headers = headers , params = params , timeout = 10 )
107109 response .raise_for_status ()
108110
109111 return {"success" : True , "message" : f"Removed queue item { queue_id } " }
110112 except Exception as e :
111- logger .error ("Error removing queue item %s from %s: %s" , queue_id , arr_type , e )
113+ logger .error ("Error removing queue item %s from %s: %s" ,
114+ queue_id , arr_type , e )
112115 return {"success" : False , "message" : f"Error: { str (e )} " }
113116
114117
@@ -133,22 +136,24 @@ def grab_release(arr_url: str, arr_api_key: str, release: Dict[str, Any], arr_ty
133136
134137 # Use the command API to grab the release
135138 api_url = f"{ arr_url .rstrip ('/' )} /api/v3/command"
136- headers = {'X-Api-Key' : arr_api_key , 'Content-Type' : 'application/json' }
139+ headers = {'X-Api-Key' : arr_api_key ,
140+ 'Content-Type' : 'application/json' }
137141
138142 # For Sonarr, we need episode IDs; for Radarr, we need movie ID
139143 if arr_type .lower () == 'sonarr' :
140144 episode_data = release .get ('episode' )
141145 episode_ids = []
142-
146+
143147 if isinstance (episode_data , dict ):
144148 # Single episode object
145149 ep_id = episode_data .get ('id' )
146150 if ep_id :
147151 episode_ids = [ep_id ]
148152 elif isinstance (episode_data , list ):
149153 # Array of episodes
150- episode_ids = [ep .get ('id' ) for ep in episode_data if ep .get ('id' )]
151-
154+ episode_ids = [ep .get ('id' )
155+ for ep in episode_data if ep .get ('id' )]
156+
152157 if not episode_ids :
153158 # Fallback: try to get series ID and trigger series search
154159 series_id = release .get ('series' , {}).get ('id' )
@@ -167,10 +172,10 @@ def grab_release(arr_url: str, arr_api_key: str, release: Dict[str, Any], arr_ty
167172 else : # Radarr
168173 movie_data = release .get ('movie' )
169174 movie_id = None
170-
175+
171176 if isinstance (movie_data , dict ):
172177 movie_id = movie_data .get ('id' )
173-
178+
174179 if not movie_id :
175180 return {"success" : False , "message" : "No movie ID found in release" }
176181
@@ -179,7 +184,8 @@ def grab_release(arr_url: str, arr_api_key: str, release: Dict[str, Any], arr_ty
179184 'movieIds' : [movie_id ]
180185 }
181186
182- response = requests .post (api_url , headers = headers , json = command_data , timeout = 10 )
187+ response = requests .post (
188+ api_url , headers = headers , json = command_data , timeout = 10 )
183189 response .raise_for_status ()
184190
185191 return {"success" : True , "message" : f"Triggered search for release" }
@@ -206,15 +212,15 @@ def retry_stuck_item(arr_url: str, arr_api_key: str, queue_item: Dict[str, Any],
206212 return {"success" : False , "message" : "No queue ID found in item" }
207213
208214 # First, remove from queue (don't blocklist, don't remove from client)
209- remove_result = remove_from_queue (arr_url , arr_api_key , queue_id , arr_type ,
210- remove_from_client = False , blocklist = False )
211-
215+ remove_result = remove_from_queue (arr_url , arr_api_key , queue_id , arr_type ,
216+ remove_from_client = False , blocklist = False )
217+
212218 if not remove_result .get ('success' ):
213219 return remove_result
214220
215221 # Then trigger a new search
216222 grab_result = grab_release (arr_url , arr_api_key , queue_item , arr_type )
217-
223+
218224 if not grab_result .get ('success' ):
219225 return {"success" : False , "message" : f"Removed from queue but failed to trigger search: { grab_result .get ('message' )} " }
220226
@@ -235,7 +241,7 @@ def monitor_and_retry_stuck_items(arr_url: str, arr_api_key: str, arr_name: str,
235241 Dict with summary of actions taken
236242 """
237243 stuck_items = get_stuck_items (arr_url , arr_api_key , arr_type )
238-
244+
239245 if not stuck_items :
240246 return {
241247 "success" : True ,
@@ -251,7 +257,7 @@ def monitor_and_retry_stuck_items(arr_url: str, arr_api_key: str, arr_name: str,
251257 for item in stuck_items :
252258 queue_id = item .get ('id' )
253259 title = "Unknown"
254-
260+
255261 if arr_type .lower () == 'sonarr' :
256262 series = item .get ('series' , {})
257263 episode = item .get ('episode' , {})
@@ -260,17 +266,20 @@ def monitor_and_retry_stuck_items(arr_url: str, arr_api_key: str, arr_name: str,
260266 movie = item .get ('movie' , {})
261267 title = movie .get ('title' , 'Unknown' )
262268
263- logger .info ("Found stuck item in %s: %s (Queue ID: %s)" , arr_name , title , queue_id )
264-
269+ logger .info ("Found stuck item in %s: %s (Queue ID: %s)" ,
270+ arr_name , title , queue_id )
271+
265272 retry_result = retry_stuck_item (arr_url , arr_api_key , item , arr_type )
266-
273+
267274 if retry_result .get ('success' ):
268275 retried_count += 1
269- logger .info ("Successfully retried stuck item in %s: %s" , arr_name , title )
276+ logger .info ("Successfully retried stuck item in %s: %s" ,
277+ arr_name , title )
270278 else :
271279 error_msg = f"{ title } : { retry_result .get ('message' , 'Unknown error' )} "
272280 errors .append (error_msg )
273- logger .error ("Failed to retry stuck item in %s: %s" , arr_name , error_msg )
281+ logger .error ("Failed to retry stuck item in %s: %s" ,
282+ arr_name , error_msg )
274283
275284 return {
276285 "success" : True ,
@@ -279,4 +288,3 @@ def monitor_and_retry_stuck_items(arr_url: str, arr_api_key: str, arr_name: str,
279288 "retried_count" : retried_count ,
280289 "errors" : errors
281290 }
282-
0 commit comments