From 3442f2cc4605a6aabdec154ebb45512f6e22b0b2 Mon Sep 17 00:00:00 2001 From: Jaisen Mathai Date: Wed, 2 Dec 2015 23:47:54 -0800 Subject: [PATCH] gh-16 Add --album-from-folder parameter to elodie.py and media.set_album_from_folder method --- elodie.py | 12 +++++++++--- elodie/media/media.py | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/elodie.py b/elodie.py index 2dcf3e3..99d4940 100755 --- a/elodie.py +++ b/elodie.py @@ -20,9 +20,9 @@ from elodie.filesystem import FileSystem from elodie.localstorage import Db def usage(): - return """Usage: main.py import --source= --destination= - main.py import --file= --destination= - main.py update [--time=] [--location=] [--album=] [--title=] INPUT ... + return """Usage: elodie.py import --source= --destination= [--album-from-folder] + elodie.py import --file= --destination= [--album-from-folder] + elodie.py update [--time=] [--location=] [--album=] [--title=] INPUT ... -h --help show this """ @@ -41,6 +41,9 @@ def _import(params): if(media.__name__ == 'Video'): filesystem.set_date_from_path_video(media) + if(params['--album-from-folder'] == True): + media.set_album_from_folder() + dest_path = filesystem.process_file(current_file, destination, media, allowDuplicate=False, move=False) if(dest_path is not None): print '%s -> %s' % (current_file, dest_path) @@ -50,6 +53,9 @@ def _import(params): if(media.__name__ == 'Video'): filesystem.set_date_from_path_video(media) + if(params['--album-from-folder'] == True): + media.set_album_from_folder() + dest_path = filesystem.process_file(current_file, destination, media, allowDuplicate=False, move=False) if(dest_path is not None): print '%s -> %s' % (current_file, dest_path) diff --git a/elodie/media/media.py b/elodie/media/media.py index e147ddc..6c7cd2f 100644 --- a/elodie/media/media.py +++ b/elodie/media/media.py @@ -237,11 +237,11 @@ class Media(object): @returns, dictionary or None for non-photo files """ - def get_metadata(self): + def get_metadata(self, update_cache=False): if(not self.is_valid()): return None - if(self.metadata is not None): + if(self.metadata is not None and update_cache == False): return self.metadata source = self.source @@ -254,7 +254,8 @@ class Media(object): 'title': self.get_title(), 'mime_type': self.get_mimetype(), 'base_name': os.path.splitext(os.path.basename(source))[0], - 'extension': self.get_extension() + 'extension': self.get_extension(), + 'directory_path': os.path.dirname(source) } return self.metadata @@ -322,8 +323,28 @@ class Media(object): exiftool_backup_file = '%s%s' % (source, '_original') if(os.path.isfile(exiftool_backup_file) is True): os.remove(exiftool_backup_file) + + self.set_metadata(album=name) return True + def set_album_from_folder(self): + metadata = self.get_metadata() + + print 'huh/' + + # If this file has an album already set we do not overwrite EXIF + if(metadata['album'] is not None): + return False + + folder = os.path.basename(metadata['directory_path']) + # If folder is empty we skip + if(len(folder) == 0): + return False + + self.set_album(folder) + 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. @@ -336,6 +357,17 @@ class Media(object): self.get_metadata() self.metadata['base_name'] = new_basename + """ + Method to manually update attributes in metadata. + + @params, named paramaters + """ + def set_metadata(self, **kwargs): + metadata = self.get_metadata() + for key in kwargs: + if(key in metadata): + self.metadata[key] = kwargs[key] + @classmethod def get_class_by_file(Media, _file, classes): extension = os.path.splitext(_file)[1][1:].lower()