Changes to get updating title and reflecting it in the file name to work.

* Cache metadata when calling get_metadata()
* Removing old title from filename (if set) so we do not end up appending multiple titles in the file name.
This commit is contained in:
Jaisen Mathai 2015-10-29 01:12:52 -07:00
parent e103acd889
commit bc156a0577
4 changed files with 51 additions and 8 deletions

View File

@ -26,7 +26,8 @@ class FileSystem:
"""
Delete a directory only if it's empty.
Instead of checking first using `len([name for name in os.listdir(directory_path)]) == 0` we catch the OSError exception.
Instead of checking first using `len([name for name in os.listdir(directory_path)]) == 0`
we catch the OSError exception.
@param, directory_name, string, A fully qualified path of the directory to delete.
"""
@ -76,7 +77,7 @@ class FileSystem:
if(metadata == None):
return None
# If the file has EXIF title we use that in the file name (i.e. my-favorite-photo-img_1234.jpg)
# If the file has EXIF title we use that in the file name (i.e. my-favorite-photo-img_1234.jpg)
# We want to remove the date prefix we add to the name.
# This helps when re-running the program on file which were already processed.
base_name = re.sub('^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}-', '', metadata['base_name'])
@ -84,7 +85,7 @@ class FileSystem:
base_name = metadata['base_name']
if('title' in metadata and metadata['title'] is not None and len(metadata['title']) > 0):
title_sanitized = re.sub('\W+', '-', metadata['title'].strip())
base_name = '%s-%s' % (title_sanitized , base_name)
base_name = '%s-%s' % (base_name, title_sanitized)
file_name = '%s-%s.%s' % (time.strftime('%Y-%m-%d_%H-%M-%S', metadata['date_taken']), base_name, metadata['extension'])
return file_name.lower()

View File

@ -39,7 +39,13 @@ class Media(object):
'longitude_ref': 'Exif.GPSInfo.GPSLongitudeRef',
}
self.exiftool_attributes = None
self.metadata = None
"""
Get album from EXIF
@returns, None or string
"""
def get_album(self):
if(not self.is_valid()):
return None
@ -212,9 +218,12 @@ class Media(object):
if(not self.is_valid()):
return None
if(self.metadata is not None):
return self.metadata
source = self.source
metadata = {
self.metadata = {
'date_taken': self.get_date_taken(),
'latitude': self.get_coordinate('latitude'),
'longitude': self.get_coordinate('longitude'),
@ -225,7 +234,7 @@ class Media(object):
'extension': self.get_extension()
}
return metadata
return self.metadata
"""
Get the mimetype of the file.
@ -288,6 +297,18 @@ class Media(object):
os.remove(exiftool_backup_file)
return True
"""
Specifically update the basename attribute in the metadata dictionary for this instance.
This is used for when we update the EXIF title of a media file.
Since that determines the name of a file if we update the title of a file more than once it appends to the file name.
I.e. 2015-12-31_00-00-00-my-first-title-my-second-title.jpg
@param, string, new_basename, New basename of file (with the old title removed
"""
def set_metadata_basename(self, new_basename):
self.get_metadata()
self.metadata['base_name'] = new_basename
@classmethod
def get_class_by_file(Media, _file, classes):
extension = os.path.splitext(_file)[1][1:].lower()

View File

@ -186,7 +186,7 @@ class Video(Media):
@returns, boolean
"""
def __update_using_plist(self, **kwargs):
if('latitude' not in kwargs and 'longitude' not in kwargs and 'time' not in kwargs):
if('latitude' not in kwargs and 'longitude' not in kwargs and 'time' not in kwargs and 'title' not in kwargs):
if(constants.debug == True):
print 'No lat/lon passed into __create_plist'
return False

View File

@ -92,12 +92,33 @@ def main(config, args):
media.set_album(config['album'])
updated = True
# Updating a title can be problematic when doing it 2+ times on a file.
# You would end up with img_001.jpg -> img_001-first-title.jpg -> img_001-first-title-second-title.jpg.
# To resolve that we have to track the prior title (if there was one.
# Then we massage the updated_media's metadata['base_name'] to remove the old title.
# Since FileSystem.get_file_name() relies on base_name it will properly rename the file by updating the title
# instead of appending it.
remove_old_title_from_name = False
if(config['title'] is not None):
media.set_title(config['title'])
# We call get_metadata() to cache it before making any changes
metadata = media.get_metadata()
title_update_status = media.set_title(config['title'])
original_title = metadata['title']
if(title_update_status and original_title is not None):
# @TODO: We should move this to a shared method since FileSystem.get_file_name() does it too.
original_title = re.sub('\W+', '-', original_title.lower())
original_base_name = metadata['base_name']
remove_old_title_from_name = True
updated = True
if(updated == True):
dest_path = filesystem.process_file(file_path, destination, media, move=True, allowDuplicate=True)
updated_media = _class(file_path)
# See comments above on why we have to do this when titles get updated.
if(remove_old_title_from_name is True and len(original_title) > 0):
updated_media.get_metadata()
updated_media.set_metadata_basename(original_base_name.replace('-%s' % original_title, ''))
dest_path = filesystem.process_file(file_path, destination, updated_media, move=True, allowDuplicate=True)
if(constants.debug == True):
print '%s -> %s' % (file_path, dest_path)