import os import re # Insert the path where you keep your fast resume files here. # Make a backup first!!! torrentsDir = r'/torrents/' # 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 = {} if not fastresumePaths: print('Finding paths...\n') detectedPaths = [] # 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: if path in fastresumePaths: newPath = fastresumePaths[path] filedata = filedata.replace( bytes(str(pathLen), 'utf-8') + b':' + path, bytes(str(len(newPath)), 'utf-8') + b':' + newPath ) print(f"Replaced {path} with {newPath}") with open(torrentsDir + 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')