diff --git a/ordigi/collection.py b/ordigi/collection.py index 054e731..b4f2e9a 100644 --- a/ordigi/collection.py +++ b/ordigi/collection.py @@ -138,9 +138,8 @@ class FPath: elif item == 'name': # Remove date prefix added to the name. part = stem - date_filename, regex = utils.get_date_from_string(stem) + date_filename, regex, sep = utils.get_date_from_string(stem) if date_filename: - sep = re.findall(regex, stem)[0][3] part = re.sub(regex, sep, part) # Delete separator if re.search('^[-_ .]', part): diff --git a/ordigi/media.py b/ordigi/media.py index c4f1ad0..b0538e8 100644 --- a/ordigi/media.py +++ b/ordigi/media.py @@ -359,9 +359,9 @@ class Media(ReadExif): stem = os.path.splitext(filename)[0] date_original = self.metadata['date_original'] if self.metadata['original_name']: - date_filename, _ = utils.get_date_from_string(self.metadata['original_name']) + date_filename, _, _ = utils.get_date_from_string(self.metadata['original_name']) else: - date_filename, _ = utils.get_date_from_string(stem) + date_filename, _, _ = utils.get_date_from_string(stem) self.log.debug(f'date_filename: {date_filename}') date_original = self.metadata['date_original'] diff --git a/ordigi/utils.py b/ordigi/utils.py index 2a9529b..c5d2b53 100644 --- a/ordigi/utils.py +++ b/ordigi/utils.py @@ -96,9 +96,11 @@ def get_date_from_string(string): # Otherwise assume a filename such as IMG_20160915_123456.jpg as default. matches = [] + sep = '' for i, regex in DATE_REGEX.items(): match = re.findall(regex, string) if match != []: + sep = match[0][3] if i == 'c': match = [('20' + match[0][0], match[0][1], match[0][2])] elif i == 'd': @@ -120,11 +122,11 @@ def get_date_from_string(string): date_object = tuple(map(int, matches[0][0])) date = datetime(*date_object) except (KeyError, ValueError): - return None, matches[0][1] + return None, matches[0][1], sep - return date, matches[0][1] + return date, matches[0][1], sep - return None, None + return None, None, sep def match_date_regex(regex, value): diff --git a/tests/test_media.py b/tests/test_media.py index cff7e4b..fd6f442 100644 --- a/tests/test_media.py +++ b/tests/test_media.py @@ -90,10 +90,10 @@ class TestMedia: date_filename = None for tag in media.tags_keys['original_name']: if tag in exif_data: - date_filename, _ = get_date_from_string(exif_data[tag]) + date_filename, _, _ = get_date_from_string(exif_data[tag]) break if not date_filename: - date_filename, _ = get_date_from_string(file_path.name) + date_filename, _, _ = get_date_from_string(file_path.name) if media.metadata['date_original']: assert date_media == media.metadata['date_original']