diff --git a/elodie/media/base.py b/elodie/media/base.py index 702a289..73b662a 100644 --- a/elodie/media/base.py +++ b/elodie/media/base.py @@ -73,7 +73,7 @@ class Base(object): if(not self.is_valid()): return None - if(self.metadata is not None and update_cache is False): + if(isinstance(self.metadata, dict) and update_cache is False): return self.metadata source = self.source diff --git a/elodie/media/text.py b/elodie/media/text.py index 8cd1aa5..a5b5ed5 100644 --- a/elodie/media/text.py +++ b/elodie/media/text.py @@ -33,7 +33,8 @@ class Text(Base): def get_album(self): self.parse_metadata_line() - if not self.metadata_line or 'album' not in self.metadata_line: + if(not isinstance(self.metadata_line, dict) or + 'album' not in self.metadata_line): return None return self.metadata_line['album'] @@ -55,7 +56,8 @@ class Text(Base): self.parse_metadata_line() # We return the value if found in metadata - if self.metadata_line and 'date_taken' in self.metadata_line: + if(isinstance(self.metadata_line, dict) and + 'date_taken' in self.metadata_line): return time.gmtime(self.metadata_line['date_taken']) # If there's no date_taken in the metadata we return @@ -73,7 +75,8 @@ class Text(Base): def get_title(self): self.parse_metadata_line() - if not self.metadata_line or 'title' not in self.metadata_line: + if(not isinstance(self.metadata_line, dict) or + 'title' not in self.metadata_line): return None return self.metadata_line['title'] @@ -104,7 +107,7 @@ class Text(Base): return status def parse_metadata_line(self): - if self.metadata_line: + if isinstance(self.metadata_line, dict): return self.metadata_line source = self.source @@ -134,7 +137,7 @@ class Text(Base): # Check if self.metadata_line is set and use that instead metadata_line = {} has_metadata = False - if self.metadata_line: + if isinstance(self.metadata_line, dict): metadata_line = self.metadata_line has_metadata = True diff --git a/elodie/media/video.py b/elodie/media/video.py index d5652d4..c4b517d 100644 --- a/elodie/media/video.py +++ b/elodie/media/video.py @@ -390,7 +390,8 @@ class Video(Media): # clobbers it source_media = Base.get_class_by_file(source, [self.__class__]) source_metadata = source_media.get_metadata() - if(source_metadata['album'] is not None): + if(isinstance(source_metadata, dict) and + source_metadata['album'] is not None): check_media.set_album(source_metadata['album']) # Copy file information from original source to temporary file diff --git a/elodie/tests/media/text_test.py b/elodie/tests/media/text_test.py index 4e82e7c..2cc5bdd 100644 --- a/elodie/tests/media/text_test.py +++ b/elodie/tests/media/text_test.py @@ -57,6 +57,21 @@ def test_get_date_taken(): assert date_taken == helper.time_convert((2016, 4, 7, 11, 15, 26, 3, 98, 0)), date_taken +def test_get_date_taken_from_invalid(): + origin = helper.get_file('valid-without-header.txt') + text = Text(origin) + text.get_metadata() + + date_taken = text.get_date_taken() + + seconds_since_epoch = min( + os.path.getmtime(origin), + os.path.getctime(origin) + ) + expected_date_taken = time.gmtime(seconds_since_epoch) + + assert date_taken == expected_date_taken, date_taken + def test_set_album(): temporary_folder, folder = helper.create_working_folder()