refactor torrent module to use dependency injection and decouple tv and torrent module for better separation of concerns

This commit is contained in:
maxDorninger
2025-06-08 11:23:18 +02:00
parent b9f955fa3f
commit 1fddf876c8
8 changed files with 255 additions and 244 deletions

View File

@@ -44,3 +44,36 @@ def import_file(target_file: Path, source_file: Path):
if target_file.exists():
target_file.unlink()
target_file.hardlink_to(source_file)
def import_torrent(torrent: Torrent) -> (list[Path], list[Path], list[Path]):
"""
Extracts all files from the torrent download directory, including extracting archives.
Returns a tuple containing: seperated video files, subtitle files, and all files found in the torrent directory.
"""
log.info(f"Importing torrent {torrent}")
all_files = list_files_recursively(path=get_torrent_filepath(torrent=torrent))
log.debug(f"Found {len(all_files)} files downloaded by the torrent")
extract_archives(all_files)
all_files = list_files_recursively(path=get_torrent_filepath(torrent=torrent))
video_files = []
subtitle_files = []
for file in all_files:
file_type, _ = mimetypes.guess_type(str(file))
if file_type is not None:
if file_type.startswith("video"):
video_files.append(file)
log.debug(f"File is a video, it will be imported: {file}")
elif file_type.startswith("text") and Path(file).suffix == ".srt":
subtitle_files.append(file)
log.debug(f"File is a subtitle, it will be imported: {file}")
else:
log.debug(
f"File is neither a video nor a subtitle, will not be imported: {file}"
)
log.info(
f"Found {len(all_files)} files ({len(video_files)} video files, {len(subtitle_files)} subtitle files) for further processing."
)
return video_files, subtitle_files, all_files