Skip to content

Add direct file mover #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
81 changes: 66 additions & 15 deletions scripts/mover.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
parser.add_argument(
"--cache-mount",
"--cache_mount",
help="Cache mount point in Unraid. This is used to additionally filter for only torrents that exists on the cache mount."
"Use this option ONLY if you follow TRaSH Guides folder structure.",
default=None,
help="Cache mount point in Unraid according to TRaSH Guides folder structure.",
required=True,
)
parser.add_argument(
"--days-from", "--days_from", help="Set Number of Days to stop torrents between two offsets", type=int, default=0
)
parser.add_argument("--days-to", "--days_to", help="Set Number of Days to stop torrents between two offsets", type=int, default=2)
parser.add_argument("--debug", help="Enable debug logger for mover", type=bool, default=False)
# --DEFINE VARIABLES--#

# --START SCRIPT--#
Expand All @@ -39,16 +39,26 @@ def filter_torrents(torrent_list, timeoffset_from, timeoffset_to, cache_mount):
result = []
for torrent in torrent_list:
if torrent.added_on >= timeoffset_to and torrent.added_on <= timeoffset_from:
if not cache_mount or exists_in_cache(cache_mount, torrent.content_path):
if os.path.exists(cache_path(cache_mount, torrent.content_path)):
result.append(torrent)
elif torrent.added_on < timeoffset_to:
break
return result


def exists_in_cache(cache_mount, content_path):
cache_path = os.path.join(cache_mount, content_path.lstrip("/"))
return os.path.exists(cache_path)
def cache_path(cache_mount, content_path):
return os.path.join(cache_mount, content_path.lstrip("/"))


def find_hardlinks(file_path, cache_mount):
inode = os.stat(file_path).st_ino
hardlinks = set()
for dirpath, _, filenames in os.walk(cache_mount):
for filename in filenames:
candidate_path = os.path.join(dirpath, filename)
if os.stat(candidate_path).st_ino == inode:
hardlinks.add(candidate_path)
return hardlinks


def stop_start_torrents(torrent_list, pause=True):
Expand Down Expand Up @@ -83,15 +93,56 @@ def stop_start_torrents(torrent_list, pause=True):

torrents = filter_torrents(torrent_list, timeoffset_from.timestamp(), timeoffset_to.timestamp(), args.cache_mount)

# Pause Torrents
print(f"Pausing [{len(torrents)}] torrents from {args.days_from} - {args.days_to} days ago")
stop_start_torrents(torrents, True)
# Pause Torrents
stop_start_torrents(torrent_list)

file_paths = set()
link_paths = set()
dir_paths = set()

for torrent in torrents:
content_path = cache_path(args.cache_mount, torrent.content_path)

if os.path.isdir(content_path):
# If file_path is a directory, include all files within it
for root, dirs, files in os.walk(content_path, topdown=False):
for file in files:
file_path = os.path.join(root, file)
file_paths.add(os.path.join(root, file))

for dir in dirs:
directory_path = os.path.join(root, dir)
dir_paths.add(directory_path)
dir_paths.add(content_path)
else:
file_paths.add(content_path)

for file_path in file_paths:
for link in find_hardlinks(content_path, args.cache_mount):
if link not in file_paths:
link_paths.add(link)

time.sleep(10)
# Start mover
print("Starting Mover")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we have it bellow, per each category: files, links, dirs

# Or using mover tunning
# os.system('/usr/local/sbin/mover start')
os.system("/usr/local/sbin/mover.old start")
# Start Torrents

if file_paths:
print(f"Moving files [{len(file_paths)}].")

files_string = "\n".join(file_paths)
os.system(f"echo '{files_string}' | /usr/local/sbin/move -d {int(args.debug)}")

if link_paths:
print(f"Moving file links [{len(link_paths)}].")

links_string = "\n".join(link_paths)
os.system(f"echo '{links_string}' | /usr/local/sbin/move -d {int(args.debug)}")

if dir_paths:
print(f"Moving directories [{len(dir_paths)}].")

dirs_string = "\n".join(dir_paths)
os.system(f"echo '{dirs_string}' | /usr/local/sbin/move -d {int(args.debug)}")

print(f"Resuming [{len(torrents)}] paused torrents from {args.days_from} - {args.days_to} days ago")
# Resume Torrents
stop_start_torrents(torrents, False)