Created
August 27, 2022 15:27
-
-
Save rbost/9d6091f67557d21d4d1ed423fdf02510 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import piexif # Needs to be installed | |
import hashlib | |
import argparse | |
import os | |
import sys | |
from pathlib import Path | |
import datetime | |
import dateutil.parser | |
def hash_file(path): | |
# BUF_SIZE is totally arbitrary, change for your app! | |
BUF_SIZE = 65536 # lets read stuff in 64kb chunks! | |
hash = hashlib.sha256() | |
with open(path, 'rb') as f: | |
while True: | |
data = f.read(BUF_SIZE) | |
if not data: | |
break | |
hash.update(data) | |
return hash.hexdigest() | |
parser = argparse.ArgumentParser() | |
parser.add_argument("file_path", type=Path) | |
dir_path = parser.parse_args().file_path | |
# print(p.file_path, type(p.file_path), p.file_path.exists(), p.file_path.is_dir()) | |
if not dir_path.exists(): | |
print(str(dir_path) + ": does not exist") | |
exit(-1) | |
if not dir_path.is_dir(): | |
print(str(dir_path) + ": is not a directory") | |
exit(-1) | |
dir_list = os.listdir(dir_path) | |
for elt in dir_list: | |
print(elt) | |
path = os.path.join(dir_path, elt) | |
try: | |
exif_dict = piexif.load(path) | |
except: | |
# print("No exif data") | |
continue | |
date_data = exif_dict["Exif"][36867] | |
# print(exif_dict["Exif"][36867]) | |
date = dateutil.parser.parse(date_data) | |
print(date) | |
print(date.strftime("%Y_%m_%d-%H_%M_%S")) | |
date = date.strftime("%Y_%m_%d-%H_%M_%S") | |
hash_hex = hash_file(path) | |
print(hash_hex) | |
(start, ext) = os.path.splitext(elt) | |
print(ext) | |
new_filename = "{date}-{hash_hex}{ext}".format( | |
date=date, hash_hex=hash_hex, ext=ext) | |
new_path = os.path.join(dir_path, new_filename) | |
print(new_path) | |
os.rename(path, new_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment