add: copy give a name and not an hash
This commit is contained in:
parent
37a31494fc
commit
73dfaf4622
@ -14,8 +14,24 @@ def argument_parsing():
|
|||||||
|
|
||||||
return args.input_folder, args.output_folder
|
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():
|
def main():
|
||||||
input_folder, output_folder = argument_parsing()
|
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: 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)]
|
files = [f for f in files if os.path.isfile(f)]
|
||||||
with Bar("copying...", max=len(files)) as bar:
|
with Bar("copying...", max=len(files)) as bar:
|
||||||
@ -23,12 +39,18 @@ def main():
|
|||||||
with open(file, 'rb') as f:
|
with open(file, 'rb') as f:
|
||||||
image_data = f.read()
|
image_data = f.read()
|
||||||
file_extention: str = file.split(".")[-1]
|
file_extention: str = file.split(".")[-1]
|
||||||
file_name: str = hashlib.sha256(image_data).hexdigest()
|
hash: str = hashlib.sha256(image_data).hexdigest()
|
||||||
path: str = os.path.join(output_folder, file_name)
|
|
||||||
if not os.path.exists(path):
|
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)
|
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()
|
bar.next()
|
||||||
|
save_copy_info(copy_path, copy_info)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
exit(main())
|
exit(main())
|
Loading…
Reference in New Issue
Block a user