fixup! Fix date detection in filenames

This commit is contained in:
Cédric Leporcq 2022-07-27 03:11:11 +02:00
parent ed9fd7cc21
commit 501ab496ff
4 changed files with 10 additions and 9 deletions

View File

@ -138,9 +138,8 @@ class FPath:
elif item == 'name': elif item == 'name':
# Remove date prefix added to the name. # Remove date prefix added to the name.
part = stem part = stem
date_filename, regex = utils.get_date_from_string(stem) date_filename, regex, sep = utils.get_date_from_string(stem)
if date_filename: if date_filename:
sep = re.findall(regex, stem)[0][3]
part = re.sub(regex, sep, part) part = re.sub(regex, sep, part)
# Delete separator # Delete separator
if re.search('^[-_ .]', part): if re.search('^[-_ .]', part):

View File

@ -359,9 +359,9 @@ class Media(ReadExif):
stem = os.path.splitext(filename)[0] stem = os.path.splitext(filename)[0]
date_original = self.metadata['date_original'] date_original = self.metadata['date_original']
if self.metadata['original_name']: 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: 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}') self.log.debug(f'date_filename: {date_filename}')
date_original = self.metadata['date_original'] date_original = self.metadata['date_original']

View File

@ -96,9 +96,11 @@ def get_date_from_string(string):
# Otherwise assume a filename such as IMG_20160915_123456.jpg as default. # Otherwise assume a filename such as IMG_20160915_123456.jpg as default.
matches = [] matches = []
sep = ''
for i, regex in DATE_REGEX.items(): for i, regex in DATE_REGEX.items():
match = re.findall(regex, string) match = re.findall(regex, string)
if match != []: if match != []:
sep = match[0][3]
if i == 'c': if i == 'c':
match = [('20' + match[0][0], match[0][1], match[0][2])] match = [('20' + match[0][0], match[0][1], match[0][2])]
elif i == 'd': elif i == 'd':
@ -120,11 +122,11 @@ def get_date_from_string(string):
date_object = tuple(map(int, matches[0][0])) date_object = tuple(map(int, matches[0][0]))
date = datetime(*date_object) date = datetime(*date_object)
except (KeyError, ValueError): 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): def match_date_regex(regex, value):

View File

@ -90,10 +90,10 @@ class TestMedia:
date_filename = None date_filename = None
for tag in media.tags_keys['original_name']: for tag in media.tags_keys['original_name']:
if tag in exif_data: if tag in exif_data:
date_filename, _ = get_date_from_string(exif_data[tag]) date_filename, _, _ = get_date_from_string(exif_data[tag])
break break
if not date_filename: 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']: if media.metadata['date_original']:
assert date_media == media.metadata['date_original'] assert date_media == media.metadata['date_original']