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':
# 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):

View File

@ -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']

View File

@ -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):

View File

@ -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']