photohub/tools/copy.py
2025-05-04 18:35:56 +02:00

56 lines
2.1 KiB
Python

import uuid
import sys
import os
import argparse
import hashlib
import shutil
from progress.bar import Bar
def argument_parsing():
parser = argparse.ArgumentParser()
parser.add_argument("input_folder", help="the folder of your raws", type=str)
parser.add_argument("output_folder", help="the location of your website", type=str)
args = parser.parse_args()
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) and f.endswith(".NEF")]
with Bar("copying...", max=len(files)) as bar:
for file in files:
with open(file, 'rb') as f:
image_data = f.read()
file_extention: str = file.split(".")[-1]
hash: str = hashlib.sha256(image_data).hexdigest()
if copy_info.get(hash) is None:
new_name: str = f"{len(copy_info):05d}"
path: str = os.path.join(output_folder, new_name)
os.makedirs(path)
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())