skip if there is no folder date time

This commit is contained in:
Antonio J. Delgado 2023-06-27 13:58:28 +03:00
parent 98e999b781
commit 8bb91c5641

View file

@ -103,7 +103,7 @@ class image_classifier:
if more_files: if more_files:
files = files + more_files files = files + more_files
except PermissionError as error: except PermissionError as error:
self._log.warning(f"Permission denied accessing folder '{path}'") self._log.warning(f"Permission denied accessing folder '{path}'. {error}")
return files return files
def process_metadata(self, file): def process_metadata(self, file):
@ -119,7 +119,7 @@ class image_classifier:
''' Obtain the file date from EXIF metadata or the file name. Return None ''' Obtain the file date from EXIF metadata or the file name. Return None
if it's not accessible or 'unknown-time' if it can't determine the date''' if it's not accessible or 'unknown-time' if it can't determine the date'''
file_date = None file_date = None
dirname = os.path.dirname(os.path.realpath(file)) # dirname = os.path.dirname(os.path.realpath(file))
filename = os.path.basename(file) filename = os.path.basename(file)
if not os.access(file, os.R_OK): if not os.access(file, os.R_OK):
self._log.error(f"The file '{file}' is not readable.") self._log.error(f"The file '{file}' is not readable.")
@ -130,10 +130,10 @@ class image_classifier:
self._log.debug(f"File creation time in EXIF: {original_date} \ self._log.debug(f"File creation time in EXIF: {original_date} \
(type: {type(original_date)})") (type: {type(original_date)})")
try: try:
#file_date = original_date.strftime('%Y/%Y.%m.%d') # file_date = original_date.strftime('%Y/%Y.%m.%d')
file_date = original_date file_date = original_date
except Exception as error: except Exception as error:
self._log.error(f"Failed to convert EXIF information about date '{original_date}'.") self._log.error(f"Failed to convert EXIF information about date '{original_date}'. {error}")
file_date = None file_date = None
if file_date is None: if file_date is None:
self._log.debug('Date not stored in EXIF metadata') self._log.debug('Date not stored in EXIF metadata')
@ -157,7 +157,6 @@ class image_classifier:
self._log.debug(f"Time based folder name section '{file_date}'") self._log.debug(f"Time based folder name section '{file_date}'")
return file_date return file_date
def process_file(self, file): def process_file(self, file):
''' Process a file, find faces, add EXIF information and ''' Process a file, find faces, add EXIF information and
move it to the folder of the day''' move it to the folder of the day'''
@ -169,43 +168,44 @@ class image_classifier:
if self.is_image(file): if self.is_image(file):
self.process_metadata(file) self.process_metadata(file)
folder_date_time = self.get_file_date(file) folder_date_time = self.get_file_date(file)
folder_date = folder_date_time.strftime(self.folder_date_format) if folder_date_time:
if folder_date: folder_date = folder_date_time.strftime(self.folder_date_format)
if self.is_image(file): if folder_date:
people = self.find_faces(file) if self.is_image(file):
if people: people = self.find_faces(file)
self._log.debug(f"Found {len(people)} known people in the image.") if people:
self._log.debug(json.dumps(people, indent=2)) self._log.debug(f"Found {len(people)} known people in the image.")
self.append_people(file, people) self._log.debug(json.dumps(people, indent=2))
self.append_people(file, people)
folder = os.path.join(dirname, folder_date, filename) folder = os.path.join(dirname, folder_date, filename)
new_path = os.path.dirname(folder) new_path = os.path.dirname(folder)
if not os.path.exists(new_path): if not os.path.exists(new_path):
os.makedirs(new_path) os.makedirs(new_path)
if self.people_folder: if self.people_folder:
for person in people: for person in people:
person_path = os.path.join(self.people_folder, person_path = os.path.join(self.people_folder,
person.replace(' ', '.'), person.replace(' ', '.'),
folder_date) folder_date)
if not os.path.exists(person_path): if not os.path.exists(person_path):
os.makedirs(person_path) os.makedirs(person_path)
self._log.info(f"Copying file '{file}' to person '{person}' folder,\ self._log.info(f"Copying file '{file}' to person '{person}' folder,\
'{person_path}'...") '{person_path}'...")
try:
shutil.copy(file, person_path)
except FileNotFoundError as error:
self._log.error(f"Error copying. File not found. {error}")
if not self.no_move:
if os.path.exists(os.path.join(new_path, filename)):
self._log.debug(f"Destination '{new_path}/{filename}' exists, removing it...")
os.remove(os.path.join(new_path, filename))
self._log.info(f"Moving file '{file}' to '{new_path}'...")
try: try:
shutil.copy(file, person_path) shutil.move(file, new_path)
except FileNotFoundError as error: except FileNotFoundError as error:
self._log.error(f"Error copying. File not found. {error}") self._log.error(f"Error copying. File not found. {error}")
if not self.no_move: else:
if os.path.exists(os.path.join(new_path, filename)): self._log.info(f"NOT moving file '{file}' to '{new_path}' because of --no-move")
self._log.debug(f"Destination '{new_path}/{filename}' exists, removing it...")
os.remove(os.path.join(new_path, filename))
self._log.info(f"Moving file '{file}' to '{new_path}'...")
try:
shutil.move(file, new_path)
except FileNotFoundError as error:
self._log.error(f"Error copying. File not found. {error}")
else:
self._log.info(f"NOT moving file '{file}' to '{new_path}' because of --no-move")
def print_metadata(self): def print_metadata(self):
print("IPTC keys:") print("IPTC keys:")