Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anthony1x6000/0de3f49b397209b46f5fd58e1e5e1a44 to your computer and use it in GitHub Desktop.
Save anthony1x6000/0de3f49b397209b46f5fd58e1e5e1a44 to your computer and use it in GitHub Desktop.
Hopefully replace paths + subdir easily in qBittorrent .fastresume (useful when switching from windows to linux)
import os
import re, shutil
# Insert the path where you keep your fast resume files here.
# Make a backup first!!!
torrentsDir = r'1/'
# Insert paths to be replaced here. Dictionary key is current path. Dictionary value is new path
# If left empty, the script will only detect paths
# fastresumePaths = {}
# fastresumePaths = {b'I:\\': b'\\run\\media\\anth\\backup2TB\\'}
fastresumePaths = {b'I:\\Torrenting': b'\\run\\media\\anth\\backup2TB\\Torrenting',
}
if not fastresumePaths:
print('Finding paths...\n')
detectedPaths = []
if fastresumePaths:
if os.path.exists('out'):
shutil.rmtree('out')
os.makedirs('out')
# Loop through torrents directory
for file in os.listdir(torrentsDir):
if file.endswith('.fastresume'):
with open(torrentsDir + file, 'rb') as fastresumeFile:
filedata = fastresumeFile.read()
query = re.search(b'save_path(\d+):', filedata)
if query is None:
print(f'Could not find path in {file}')
else:
pathLen = int(query.group(1))
path = filedata[query.end():query.end() + pathLen]
# check if user entered paths to change or not
if fastresumePaths:
for oldPath, newPath in fastresumePaths.items():
newFullPath = path.replace(oldPath, newPath)
filedata = filedata.replace(
bytes(str(pathLen), 'utf-8') + b':' + path,
bytes(str(len(newFullPath)), 'utf-8') + b':' + newFullPath
)
print(f"Replaced {path} with {newFullPath}")
with open('out/' + file, 'wb') as fastresumeFile:
fastresumeFile.write(filedata)
else:
if path not in detectedPaths:
detectedPaths.append(path)
if fastresumePaths is None or not fastresumePaths:
if not detectedPaths:
print('No paths were found')
else:
print('The following paths were found\n')
for path in detectedPaths:
print(path)
print('\nCopy the following code into the "fastresumePaths" variable to replace paths.\n')
maxPathLength = len(max(detectedPaths, key=len)) + 4
print(f"# {'Original Path'.ljust(maxPathLength)} New Path")
print(f"# {'-' * (maxPathLength)} {'-' * (maxPathLength)}\n")
print('fastresumePaths = {')
for path in detectedPaths:
print(f" {str(path).ljust(maxPathLength)}: {str(path).ljust(maxPathLength)},")
print('}\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment