Add python setup

This commit is contained in:
Mario Romero 2023-04-05 23:29:32 -04:00
parent 604cdb854e
commit 5ef67d0d55
4 changed files with 79 additions and 0 deletions

0
__init__.py Normal file
View File

16
setup.py Normal file
View File

@ -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',
],
},
)

0
src/__init__.py Normal file
View File

63
src/main.py Normal file
View File

@ -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()