99import datetime
1010from typing import List , Dict , Any , Optional , Union
1111from utils .logger import logger , debug_log
12- from config import API_KEY , API_URL , API_TIMEOUT , MONITORED_ONLY , SKIP_FUTURE_RELEASES
12+ from config import API_KEY , API_URL , API_TIMEOUT , MONITORED_ONLY , SKIP_FUTURE_RELEASES , COMMAND_WAIT_DELAY , COMMAND_WAIT_ATTEMPTS
1313
1414# Create a session for reuse
1515session = requests .Session ()
@@ -40,6 +40,44 @@ def radarr_request(endpoint: str, method: str = "GET", data: Dict = None) -> Opt
4040 logger .error (f"API request error: { e } " )
4141 return None
4242
43+ def wait_for_command (command_id : int ):
44+ logger .debug (f"Waiting for command { command_id } to complete..." )
45+ attempts = 0
46+ while True :
47+ try :
48+ time .sleep (COMMAND_WAIT_DELAY )
49+ response = radarr_request (f"command/{ command_id } " )
50+ logger .debug (f"Command { command_id } Status: { response ['status' ]} " )
51+ except Exception as error :
52+ logger .error (f"Error fetching command status on attempt { attempts + 1 } : { error } " )
53+ return False
54+
55+ attempts += 1
56+
57+ if response ['status' ].lower () in ['complete' , 'completed' ] or attempts >= COMMAND_WAIT_ATTEMPTS :
58+ break
59+
60+ if response ['status' ].lower () not in ['complete' , 'completed' ]:
61+ logger .warning (f"Command { command_id } did not complete within the allowed attempts." )
62+ return False
63+
64+ time .sleep (0.5 )
65+
66+ return response ['status' ].lower () in ['complete' , 'completed' ]
67+
68+ def get_download_queue_size () -> Optional [int ]:
69+ """
70+ GET /api/v3/queue
71+ Returns total number of items in the queue with the status 'downloading'.
72+ """
73+ response = radarr_request ("queue?status=downloading" )
74+ total_records = response .get ("totalRecords" , 0 )
75+ if not isinstance (total_records , int ):
76+ total_records = 0
77+ logger .debug (f"Download Queue Size: { total_records } " )
78+
79+ return total_records
80+
4381def get_movies () -> List [Dict ]:
4482 """Get all movies from Radarr (full list)"""
4583 result = radarr_request ("movie" )
@@ -117,20 +155,23 @@ def refresh_movie(movie_id: int) -> Optional[Dict]:
117155 "name" : "RefreshMovie" ,
118156 "movieIds" : [movie_id ]
119157 }
120- return radarr_request ("command" , method = "POST" , data = data )
158+ response = radarr_request ("command" , method = "POST" , data = data )
159+ return wait_for_command (response ['id' ])
121160
122161def movie_search (movie_id : int ) -> Optional [Dict ]:
123162 """Search for a movie by ID"""
124163 data = {
125164 "name" : "MoviesSearch" ,
126165 "movieIds" : [movie_id ]
127166 }
128- return radarr_request ("command" , method = "POST" , data = data )
167+ response = radarr_request ("command" , method = "POST" , data = data )
168+ return wait_for_command (response ['id' ])
129169
130170def rescan_movie (movie_id : int ) -> Optional [Dict ]:
131171 """Rescan movie files"""
132172 data = {
133173 "name" : "RescanMovie" ,
134- "movieIds " : [ movie_id ]
174+ "movieId " : movie_id
135175 }
136- return radarr_request ("command" , method = "POST" , data = data )
176+ response = radarr_request ("command" , method = "POST" , data = data )
177+ return wait_for_command (response ['id' ])
0 commit comments