diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7e62e61 --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +from setuptools import setup, find_packages + +setup( + name='torrename', + version='1.0.0', + description='File matcher and rename tool given a .torrent file', + author='Mario Romero', + author_email='mario@1159.cl', + url='https://git.1159.cl/Mario1159/torrename', + packages=find_packages(), + entry_points={ + 'console_scripts': [ + 'torrename = src.main:main', + ], + }, +) \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..c1eee7c --- /dev/null +++ b/src/main.py @@ -0,0 +1,63 @@ +import torf, argparse, glob, os, pathlib + +def rename(src_path: pathlib.Path, new_name: str): + renamed_path = src_path.parent.joinpath(new_name) + os.rename(src_path.as_posix(), renamed_path.as_posix()) + +def rename_based_on_size(torrent_files: torf.File, files_downloaded: torf.File): + # Make list excluding files with the same size + unique_files_downloaded = [] + for file_downloaded in files_downloaded: + duplicated = False + for file in unique_files_downloaded: + if file.size == file_downloaded.size: + unique_files_downloaded.remove(file) + duplicated = True + break + if not duplicated: + unique_files_downloaded.append(torf.File(file_downloaded, file_downloaded.size)) + # Rename files with the same size + files_failed = [] + for torrent_file in torrent_files: + success = False + for file_downloaded in unique_files_downloaded: + if torrent_file.size == os.path.getsize(file_downloaded): + rename(pathlib.Path(file_downloaded), torrent_file.name) + unique_files_downloaded.remove(file_downloaded) + success = True + break + if not success: + files_failed.append(torrent_file) + return files_failed + + +def main(): + parser = argparse.ArgumentParser( + prog='torrename', + description='File matcher and rename tool given a .torrent file') + + parser.add_argument('torrent', help='.torrent filepath') + parser.add_argument('files', help='list of files to be compared') + parser.add_argument('--hash-check', action='store_true', help='compare hashes of torrent after file match, this option can take a longer time') + args = parser.parse_args() + + torrent = torf.Torrent.read(args.torrent) + + files_downloaded = [] + for path in glob.glob(args.files): + files_downloaded.append(torf.File(path, os.path.getsize(path))) + + files_failed = rename_based_on_size(torrent.files, files_downloaded) + if not files_failed: + for file in files_downloaded: + filepath = pathlib.Path(file) + if (args.hash_check): + if torrent.verify(filepath.parent): + print(f'Hash verification of file {filepath.name} succeded') + else: + print(f'Hash verification of file {filepath.name} failed') + else: + print(f'The following files were not found: {files_failed}') + +if __name__ == "__main__": + main() \ No newline at end of file