From 73dfaf4622e93d377b9d88bc26ccd9c5cb386c8a Mon Sep 17 00:00:00 2001 From: Starnakin Date: Sun, 20 Apr 2025 13:26:55 +0200 Subject: [PATCH] add: copy give a name and not an hash --- tools/copy.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tools/copy.py b/tools/copy.py index da935c1..06fb347 100644 --- a/tools/copy.py +++ b/tools/copy.py @@ -14,8 +14,24 @@ def argument_parsing(): return args.input_folder, args.output_folder +def get_copy_info(path: str): + copy_info: dict[str, dict[str, str]] = {} + if os.path.exists(path): + with open(path, "r") as f: + for line in f.read().splitlines(): + hash, og_name, new_name = line.split(" | ") + copy_info.update({hash: {"og_name": og_name, "new_name": new_name}}) + return copy_info + +def save_copy_info(path: str, copy_info: dict[str, dict[str, str]]): + copy_info_str: str = "\n".join([f"{hash} | {copy_info[hash]['og_name']} | {copy_info[hash]['new_name']}" for hash in copy_info.keys()]) + with open(path, "w") as f: + f.write(copy_info_str) + def main(): input_folder, output_folder = argument_parsing() + copy_path = os.path.join(output_folder, "ph_copy.txt") + copy_info: dict[str, str] = get_copy_info(copy_path) files: list[str] = [os.path.join(input_folder, f) for f in os.listdir(input_folder)] files = [f for f in files if os.path.isfile(f)] with Bar("copying...", max=len(files)) as bar: @@ -23,12 +39,18 @@ def main(): with open(file, 'rb') as f: image_data = f.read() file_extention: str = file.split(".")[-1] - file_name: str = hashlib.sha256(image_data).hexdigest() - path: str = os.path.join(output_folder, file_name) - if not os.path.exists(path): + hash: str = hashlib.sha256(image_data).hexdigest() + + if copy_info.get(hash) is None: + new_name: str = str(len(copy_info)) + path: str = os.path.join(output_folder, new_name) + os.makedirs(path) - shutil.copy2(file, os.path.join(path, file_name + "." + file_extention)) + shutil.copy2(file, os.path.join(path, new_name + "." + file_extention)) + + copy_info.update({hash: {"og_name": os.path.basename(file), "new_name": new_name}}) bar.next() + save_copy_info(copy_path, copy_info) if __name__ == "__main__": exit(main()) \ No newline at end of file