From d3117d61eb35cb140dd3d2838a85c4526113162a Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Wed, 8 Sep 2021 14:42:59 +0300 Subject: [PATCH] Change test for no move --- image_classifier/image_classifier.py | 37 ++++++++++++++-------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/image_classifier/image_classifier.py b/image_classifier/image_classifier.py index e2d321a..cf78486 100755 --- a/image_classifier/image_classifier.py +++ b/image_classifier/image_classifier.py @@ -86,27 +86,28 @@ class image_classifier: self._log.debug(f"Updated file '{file}'.") dirname = os.path.dirname(os.path.realpath(file)) filename = os.path.basename(file) - if not self.no_move: - new_path = False - if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys: - original_date = self.metadata['Exif.Photo.DateTimeOriginal'].value - self._log.debug(f"File creation time: {original_date} (type: {type(original_date)})") - new_path = os.path.join(dirname, original_date.strftime('%Y/%m/%d'), filename) - if not new_path: - match = re.search(r'(?P20[0-9]{2})[\-/\._]?(?P[0-1]?[0-9])[\-/\._]?(?P[0-3]?[0-9])', filename) - if match: - new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) - if not new_path: - match = re.search(r'(?P[0-3]?[0-9])[\-/\._]?(?P[0-1]?[0-9])[\-/\._]?(?P20[0-9]{2})', filename) - if match: - new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) - if not new_path: - new_path = os.path.join(dirname, 'unknown-time', filename) - os.makedirs(os.path.dirname(new_path), exist_ok=True) + + new_path = False + if 'Exif.Photo.DateTimeOriginal' in self.metadata.exif_keys: + original_date = self.metadata['Exif.Photo.DateTimeOriginal'].value + self._log.debug(f"File creation time: {original_date} (type: {type(original_date)})") + new_path = os.path.join(dirname, original_date.strftime('%Y/%m/%d'), filename) + if not new_path: + match = re.search(r'(?P20[0-9]{2})[\-/\._]?(?P[0-1]?[0-9])[\-/\._]?(?P[0-3]?[0-9])', filename) + if match: + new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) + if not new_path: + match = re.search(r'(?P[0-3]?[0-9])[\-/\._]?(?P[0-1]?[0-9])[\-/\._]?(?P20[0-9]{2})', filename) + if match: + new_path = os.path.join(dirname, match.group('year'), match.group('month'), match.group('day'), filename) + if not new_path: + new_path = os.path.join(dirname, 'unknown-time', filename) + os.makedirs(os.path.dirname(new_path), exist_ok=True) + if self.no_move == False: self._log.info(f"Moving file '{file}' to '{new_path}'...") shutil.move(file, new_path) else: - self._log.debug('Not moving to subfolders.') + self._log.info(f"NOT moving file '{file}' to '{new_path}' because of --no-move") def print_metadata(self): print("IPTC keys:")