BitMover/rename.py

116 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python
import argparse
import sys
import os
from bitmover import copy_from_source
from file_stuff import path_exists, cmp_files
from get_image_tag import get_image_date, get_exif_tag
from hashing import xx_hash
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--folder", help = "folder with files to rename")
parser.add_argument("-o", "--keeporiginalname", help = "keeps the original name attached to the file.")
parser.add_argument("-d", "--dryrun", help = "dry run, no action")
args = parser.parse_args()
if args.folder:
FOLDER = args.folder
else:
print("you need to specify a folder. Duh.")
sys.exit()
if args.dryrun:
dry_run = True
else:
dry_run = False
if args.keeporiginalname:
keep_orig_name = True
else:
keep_orig_name = False
def get_file_size(f):
return os.path.getsize(f)
for file in os.listdir(FOLDER):
if file.lower().endswith("gif"):
copy_from_source(FOLDER,'/Volumes/VIDEO_ARRAY_01/Multimedia/gif',file)
file_match = cmp_files(os.path.join(FOLDER,file),
os.path.join('/Volumes/VIDEO_ARRAY_01/Multimedia/gif',file))
if file_match is True:
os.remove(os.path.join(FOLDER,file))
if file.lower().endswith("png"):
copy_from_source(FOLDER,'/Volumes/VIDEO_ARRAY_01/Multimedia/png',file)
file_match = cmp_files(os.path.join(FOLDER, file),
os.path.join('/Volumes/VIDEO_ARRAY_01/Multimedia/gif', file))
if file_match is True:
os.remove(os.path.join(FOLDER, file))
if file.lower().endswith("heic"):
copy_from_source(FOLDER,'/Volumes/VIDEO_ARRAY_01/Multimedia/heic',file)
file_match = cmp_files(os.path.join(FOLDER, file),
os.path.join('/Volumes/VIDEO_ARRAY_01/Multimedia/heic', file))
if file_match is True:
os.remove(os.path.join(FOLDER, file))
if file.lower().endswith("jpg") or \
file.lower().endswith("jpeg") or \
file.lower().endswith("nef") or \
file.lower().endswith("rw2") or \
file.lower().endswith("arw") or \
file.lower().endswith("dng"):
old_path = os.path.join(FOLDER,file)
lowered_name = file.lower()
file_size = get_file_size(old_path)
file_extension = os.path.splitext(lowered_name)[1]
image_date = get_image_date(old_path)
image_date_year = image_date.strftime("%Y")
image_date_month = image_date.strftime("%m")
image_date_day = image_date.strftime("%d")
image_date_hour = image_date.strftime("%H")
image_date_minute = image_date.strftime("%M")
image_date_second = image_date.strftime("%S")
image_date_microsecond = image_date.strftime("%f")
image_date_subsecond = str(get_exif_tag(old_path,'EXIF SubSec'))
image_hash = xx_hash(old_path)
if image_date_subsecond:
subsecond_desired_length = 6
if image_date_subsecond == 'None':
image_date_subsecond = subsecond_desired_length*str('0')
l_image_date_subsecond = len(image_date_subsecond)
if subsecond_desired_length > l_image_date_subsecond:
pad = subsecond_desired_length - l_image_date_subsecond
image_date_subsecond = image_date_subsecond + pad*str("0")
new_name = f'{image_date_year}-{image_date_month}-{image_date_day}-{image_date_hour}{image_date_minute}{image_date_second}{image_date_subsecond}_{file_size}_{image_hash}'
else:
new_name = f'{image_date_year}-{image_date_month}-{image_date_day}-{image_date_hour}{image_date_minute}{image_date_second}000_{file_size}_{image_hash}'
if keep_orig_name is True:
new_file_name = f'{new_name}-{lowered_name}'
else:
new_file_name = f'{new_name}{file_extension}'
new_path = os.path.join(FOLDER,new_file_name)
if path_exists(new_path):
print(f"{new_path} exists.. skipping.")
elif dry_run is True:
print(f'Dry run: {old_path} becomes {new_path}')
else:
print(f'Renaming {old_path} to: {new_path}')
os.rename(old_path,new_path)