Skip to content

Commit

Permalink
UPD: Use rootfolders from Arrs for files scan
Browse files Browse the repository at this point in the history
  • Loading branch information
nandyalu committed Jan 26, 2025
1 parent 6620478 commit 3517c85
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
48 changes: 39 additions & 9 deletions backend/core/tasks/files_scan.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
import os
from app_logger import ModuleLogger
from core.base.database.manager.base import MediaDatabaseManager
from core.base.database.manager.connection import ConnectionDatabaseManager
from core.base.database.models.connection import ArrType
from core.files_handler import FilesHandler
from core.radarr.connection_manager import RadarrConnectionManager
from core.sonarr.connection_manager import SonarrConnectionManager

logger = ModuleLogger("TrailersFilesScan")


def scan_disk_for_trailers():
"""Scan the disk for trailers and update the database with the new status."""
async def scan_disk_for_trailers() -> None:
"""Scan the disk for trailers and update the database with the new status. \n
Returns:
None
"""
logger.info("Scanning disk for trailers.")
# Get all media from the database
# # Get all media from the database
db_handler = MediaDatabaseManager()
all_media = db_handler.read_all()

# Get all root folders from the media folders
# # Get all root folders from the media folders
# root_folders: set[str] = set()
# root_folders = {
# os.path.dirname(media.folder_path) for media in all_media if media.folder_path
# }

root_folders: set[str] = set()
root_folders = {
os.path.dirname(media.folder_path) for media in all_media if media.folder_path
}

# Get all connections from the database
connection_db_handler = ConnectionDatabaseManager()
all_connections = connection_db_handler.read_all()

# Get all root folders for each connection from API
for connection in all_connections:
if connection.arr_type == ArrType.RADARR:
arr_manager = RadarrConnectionManager(connection)
elif connection.arr_type == ArrType.SONARR:
arr_manager = SonarrConnectionManager(connection)
else:
continue
root_folders.update(await arr_manager.get_rootfolders())

# Find all folders with trailers
trailer_folders: set[str] = set()
Expand All @@ -27,7 +49,15 @@ def scan_disk_for_trailers():
# Match the trailer folders to the media in the database
updated_media: list[tuple[int, bool]] = []
for media in all_media:
_trailer_exists = media.folder_path in trailer_folders
if not media.folder_path:
continue
# Check if trailer was found
_trailer_exists = media.trailer_exists
for folder in trailer_folders:
if folder.startswith(media.folder_path):
_trailer_exists = True
break
# _trailer_exists = media.folder_path in trailer_folders
if media.trailer_exists != _trailer_exists:
updated_media.append((media.id, _trailer_exists))

Expand Down
8 changes: 7 additions & 1 deletion backend/core/tasks/schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def _refresh_images():
return


def _scan_disk_for_trailers():
"""Scans the disk for trailers."""
run_async(scan_disk_for_trailers)
return


def _cleanup_trailers():
"""Cleanup trailers without audio."""
run_async(trailer_cleanup)
Expand Down Expand Up @@ -89,7 +95,7 @@ def scan_disk_for_trailers_job():
None
"""
scheduler.add_job(
func=scan_disk_for_trailers,
func=_scan_disk_for_trailers,
trigger="interval",
minutes=app_settings.monitor_interval,
id="scan_disk_for_trailers_job",
Expand Down

0 comments on commit 3517c85

Please sign in to comment.