Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor updates #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/sortphotos.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ def get_metadata(self, *args):
def sortPhotos(src_dir, dest_dir, sort_format, rename_format, recursive=False,
copy_files=False, test=False, remove_duplicates=True, day_begins=0,
additional_groups_to_ignore=['File'], additional_tags_to_ignore=[],
use_only_groups=None, use_only_tags=None, verbose=True, keep_filename=False):
use_only_groups=None, use_only_tags=None, verbose=True, keep_filename=False,
clean_src_dir=False):
"""
This function is a convenience wrapper around ExifTool based on common usage scenarios for sortphotos.py

Expand Down Expand Up @@ -268,13 +269,29 @@ def sortPhotos(src_dir, dest_dir, sort_format, rename_format, recursive=False,
a list of tags that will be exclusived searched across for date info
verbose : bool
True if you want to see details of file processing
clean_src_dir: bool
True if you want to remove the duplicate images from the src_dir

"""

# some error checking
if not os.path.exists(src_dir):
raise Exception('Source directory does not exist')

if clean_src_dir:
print(f'Deleting empty directories in {src_dir}')
bash_command = f'find {src_dir} -type d -empty -delete'
process = subprocess.Popen(
bash_command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()

# clear out empty files since EXIF tool doesn't handle them properly
if verbose:
print(f'Removing all empty files')
bash_command = f'find {src_dir} -type f -empty -delete'
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()

# setup arguments to exiftool
args = ['-j', '-a', '-G']

Expand Down Expand Up @@ -365,7 +382,10 @@ def sortPhotos(src_dir, dest_dir, sort_format, rename_format, recursive=False,
for thedir in dirs:
dest_file = os.path.join(dest_file, thedir)
if not test and not os.path.exists(dest_file):
os.makedirs(dest_file)
try:
os.makedirs(dest_file)
except:
print(f'Tried to make directory for {dest_file} but got an error')

# rename file if necessary
filename = os.path.basename(src_file)
Expand Down Expand Up @@ -400,6 +420,11 @@ def sortPhotos(src_dir, dest_dir, sort_format, rename_format, recursive=False,
dest_compare = dest_file
if remove_duplicates and filecmp.cmp(src_file, dest_compare): # check for identical files
fileIsIdentical = True
if clean_src_dir:
try:
os.remove(src_file)
except:
printf('Could not remove file {src_file}')
if verbose:
print('Identical file already exists. Duplicate will be ignored.\n')
break
Expand Down Expand Up @@ -438,6 +463,11 @@ def sortPhotos(src_dir, dest_dir, sort_format, rename_format, recursive=False,
print()
# sys.stdout.flush()

if clean_src_dir:
print(f'Deleting empty directories in {src_dir}')
bash_command = f'find {src_dir} -type d -empty -delete'
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()

if not verbose:
print()
Expand Down Expand Up @@ -489,15 +519,17 @@ def main():
parser.add_argument('--use-only-tags', type=str, nargs='+',
default=None,
help='specify a restricted set of tags to search for date information\n\
e.g., EXIF:CreateDate')
e.g., EXIF: CreateDate ')
parser.add_argument('--clean_src_dir', type=bool, default=False, help='remove duplicates from src_dir')

# parse command line arguments
args = parser.parse_args()

sortPhotos(args.src_dir, args.dest_dir, args.sort, args.rename, args.recursive,
args.copy, args.test, not args.keep_duplicates, args.day_begins,
args.ignore_groups, args.ignore_tags, args.use_only_groups,
args.use_only_tags, not args.silent, args.keep_filename)
args.copy, args.test, not args.keep_duplicates, args.day_begins,
args.ignore_groups, args.ignore_tags, args.use_only_groups,
args.use_only_tags, not args.silent, args.keep_filename,
args.clean_src_dir)

if __name__ == '__main__':
main()