@@ -58,29 +58,55 @@ def add_pending_scan(scan_type: str, scan_id: str, name: str, library_key: str =
5858
5959
6060def check_scan_status (scan_id : str ) -> str :
61- """Check if a scan is still pending or completed."""
61+ """Check if a scan is still pending or completed using Plex Activities API ."""
6262 if scan_id not in PENDING_SCANS :
6363 return "unknown"
64-
64+
6565 scan_info = PENDING_SCANS [scan_id ]
6666 scan_type = scan_info .get ("type" )
6767 library_key = scan_info .get ("library_key" )
68+
69+ # Get current activities from Plex
70+ from plex_utils import get_plex_activities , is_plex_scanning
71+ activities = get_plex_activities ()
6872
73+ # Check if there are any scanning activities
74+ has_scanning_activity = len (activities ) > 0
75+
6976 if scan_type == "library" and library_key :
70- # Check if library is still scanning
71- from plex_utils import is_plex_scanning
77+ # Check if this specific library is scanning
7278 try :
7379 section_id = int (library_key )
74- is_scanning = is_plex_scanning (section_id )
75- if not is_scanning :
76- # Scan likely completed
77- scan_info ["status" ] = "completed"
78- scan_info ["completed_at" ] = datetime .now ().isoformat ()
79- return "completed"
80+ # Check activities for this specific section
81+ section_scanning = False
82+ for activity in activities :
83+ # Try to extract section ID from activity context
84+ # Activities might have librarySectionID in context
85+ context = activity .get ('context' , {})
86+ if isinstance (context , dict ):
87+ act_section_id = context .get ('librarySectionID' ) or context .get ('sectionID' )
88+ if act_section_id and str (act_section_id ) == str (section_id ):
89+ section_scanning = True
90+ logger .info (f"Found scanning activity for section { section_id } " )
91+ break
92+
93+ # Also check using the section's refreshing attribute
94+ if not section_scanning :
95+ section_scanning = is_plex_scanning (section_id )
96+
97+ if not section_scanning :
98+ # No scanning activity found, check timeout
99+ scan_timestamp = datetime .fromisoformat (scan_info ["timestamp" ])
100+ time_since_scan = (datetime .now () - scan_timestamp ).total_seconds ()
101+ # If no activity and it's been more than 2 minutes, assume completed
102+ if time_since_scan > 120 :
103+ scan_info ["status" ] = "completed"
104+ scan_info ["completed_at" ] = datetime .now ().isoformat ()
105+ return "completed"
80106 return "pending"
81107 except Exception as e :
82108 logger .warning (f"Error checking scan status for { scan_id } : { e } " )
83- # Assume completed after a timeout period
109+ # Fallback to timeout
84110 scan_timestamp = datetime .fromisoformat (scan_info ["timestamp" ])
85111 time_since_scan = (datetime .now () - scan_timestamp ).total_seconds ()
86112 if time_since_scan > 300 : # 5 minutes
@@ -89,19 +115,33 @@ def check_scan_status(scan_id: str) -> str:
89115 return "completed"
90116 return "pending"
91117 elif scan_type == "all_libraries" :
92- # For all libraries scan, mark as completed after a reasonable time
93- scan_timestamp = datetime .fromisoformat (scan_info ["timestamp" ])
94- time_since_scan = (datetime .now () - scan_timestamp ).total_seconds ()
95- # Assume completed after 10 minutes for all libraries
96- if time_since_scan > 600 :
97- scan_info ["status" ] = "completed"
98- scan_info ["completed_at" ] = datetime .now ().isoformat ()
99- return "completed"
118+ # For all libraries scan, check if any scanning activities exist
119+ if not has_scanning_activity :
120+ # No scanning activities, check timeout
121+ scan_timestamp = datetime .fromisoformat (scan_info ["timestamp" ])
122+ time_since_scan = (datetime .now () - scan_timestamp ).total_seconds ()
123+ # If no activity and it's been more than 5 minutes, assume completed
124+ if time_since_scan > 300 :
125+ scan_info ["status" ] = "completed"
126+ scan_info ["completed_at" ] = datetime .now ().isoformat ()
127+ return "completed"
100128 return "pending"
101129 else :
102130 # For item scans, check the library status
103131 if library_key :
104- return check_scan_status (f"library_{ library_key } " )
132+ try :
133+ section_id = int (library_key )
134+ is_scanning = is_plex_scanning (section_id )
135+ if not is_scanning :
136+ scan_timestamp = datetime .fromisoformat (scan_info ["timestamp" ])
137+ time_since_scan = (datetime .now () - scan_timestamp ).total_seconds ()
138+ if time_since_scan > 120 : # 2 minutes
139+ scan_info ["status" ] = "completed"
140+ scan_info ["completed_at" ] = datetime .now ().isoformat ()
141+ return "completed"
142+ return "pending"
143+ except :
144+ pass
105145 # Default: assume completed after 5 minutes
106146 scan_timestamp = datetime .fromisoformat (scan_info ["timestamp" ])
107147 time_since_scan = (datetime .now () - scan_timestamp ).total_seconds ()
@@ -896,7 +936,7 @@ def api_plex_scan_all():
896936 try :
897937 scan_id = f"all_libraries_{ uuid .uuid4 ().hex [:8 ]} "
898938 add_pending_scan ("all_libraries" , scan_id , "All Libraries" )
899-
939+
900940 bot_instance = app .config .get ('discord_bot' )
901941 if not bot_instance :
902942 return jsonify ({"success" : False , "message" : "Bot instance not available" }), 500
@@ -1002,7 +1042,8 @@ def api_plex_item_scan():
10021042
10031043 # Create scan ID and add to pending scans
10041044 scan_id = f"item_{ uuid .uuid4 ().hex [:8 ]} "
1005- add_pending_scan ("item" , scan_id , item_name , library_key = library_key , item_key = item_key )
1045+ add_pending_scan ("item" , scan_id , item_name ,
1046+ library_key = library_key , item_key = item_key )
10061047
10071048 logger .info (f"Starting async scan for item: { item_key } " )
10081049 # Run scan in async context
@@ -1060,7 +1101,7 @@ def api_pending_scans():
10601101 if scan_info .get ("status" ) == "pending" :
10611102 check_scan_status (scan_id ) # Updates status internally
10621103 scan_info ["checked_at" ] = datetime .now ().isoformat ()
1063-
1104+
10641105 # Filter out completed scans older than 1 hour
10651106 now = datetime .now ()
10661107 active_scans = []
@@ -1072,10 +1113,10 @@ def api_pending_scans():
10721113 if (now - completed_time ).total_seconds () > 3600 : # 1 hour
10731114 continue
10741115 active_scans .append (scan_info )
1075-
1116+
10761117 # Sort by timestamp, newest first
10771118 active_scans .sort (key = lambda x : x .get ("timestamp" , "" ), reverse = True )
1078-
1119+
10791120 return jsonify ({
10801121 "success" : True ,
10811122 "pending_scans" : active_scans
0 commit comments