Add python setup
This commit is contained in:
parent
5ef67d0d55
commit
3ed19d2519
63
main.py
63
main.py
@ -1,63 +0,0 @@
|
||||
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()
|
Loading…
Reference in New Issue
Block a user