Retrieve date from filename using get_date_fron_string function
This commit is contained in:
parent
b883d9ca36
commit
114187415f
|
@ -37,7 +37,13 @@ class ExifMetadata:
|
|||
'QuickTime:CreationDate-und-US',
|
||||
'QuickTime:MediaCreateDate',
|
||||
]
|
||||
tags_keys['date_modified'] = ['File:FileModifyDate', 'QuickTime:ModifyDate']
|
||||
tags_keys['date_modified'] = [
|
||||
'EXIF:ModifyDate',
|
||||
'QuickTime:ModifyDate',
|
||||
]
|
||||
tags_keys['file_modify_date'] = [
|
||||
'File:FileModifyDate',
|
||||
]
|
||||
tags_keys['camera_make'] = ['EXIF:Make', 'QuickTime:Make']
|
||||
tags_keys['camera_model'] = ['EXIF:Model', 'QuickTime:Model']
|
||||
tags_keys['album'] = ['XMP-xmpDM:Album', 'XMP:Album']
|
||||
|
@ -80,9 +86,13 @@ class ExifMetadata:
|
|||
|
||||
try:
|
||||
# correct nasty formated date
|
||||
regex = re.compile(r'(\d{4}):(\d{2}):(\d{2})')
|
||||
if re.match(regex, value) is not None: # noqa
|
||||
value = re.sub(regex, r'\g<1>-\g<2>-\g<3>', value)
|
||||
regex = re.compile(r'(\d{4}):(\d{2}):(\d{2})[-_ .]')
|
||||
if re.match(regex, value):
|
||||
value = re.sub(regex, r'\g<1>-\g<2>-\g<3> ', value)
|
||||
else:
|
||||
regex = re.compile(r'(\d{4})(\d{2})(\d{2})[-_ .]?(\d{2})?(\d{2})?(\d{2})?')
|
||||
if re.match(regex, value):
|
||||
value = re.sub(regex, r'\g<1>-\g<2>-\g<3> \g<4>:\g<5>:\g<6>', value)
|
||||
return parser.parse(value)
|
||||
except BaseException or parser._parser.ParserError as e:
|
||||
self.log.warning(e.args, value)
|
||||
|
@ -338,13 +348,15 @@ class Media(ReadExif):
|
|||
stem = os.path.splitext(filename)[0]
|
||||
date_original = self.metadata['date_original']
|
||||
if self.metadata['original_name']:
|
||||
date_filename = self.get_date_format(self.metadata['original_name'])
|
||||
date_filename = utils.get_date_from_string(self.metadata['original_name'])
|
||||
else:
|
||||
date_filename = self.get_date_format(stem)
|
||||
date_filename = utils.get_date_from_string(stem)
|
||||
self.log.debug(f'date_filename: {date_filename}')
|
||||
|
||||
date_original = self.metadata['date_original']
|
||||
date_created = self.metadata['date_created']
|
||||
date_modified = self.metadata['date_modified']
|
||||
file_modify_date = self.metadata['file_modify_date']
|
||||
if self.metadata['date_original']:
|
||||
if date_filename and date_filename != date_original:
|
||||
self.log.warning(
|
||||
|
@ -372,6 +384,8 @@ class Media(ReadExif):
|
|||
self.log.warning(
|
||||
f"{filename} time mark is more recent than {date_created}"
|
||||
)
|
||||
return date_created
|
||||
|
||||
if self.interactive:
|
||||
choices = [
|
||||
(f"date filename:'{date_filename}'", date_filename),
|
||||
|
@ -383,7 +397,6 @@ class Media(ReadExif):
|
|||
|
||||
return date_filename
|
||||
|
||||
if self.use_file_dates:
|
||||
if date_created:
|
||||
self.log.warning(
|
||||
f"use date created:{date_created} for {self.file_path}"
|
||||
|
@ -396,6 +409,13 @@ class Media(ReadExif):
|
|||
)
|
||||
return date_modified
|
||||
|
||||
if self.use_file_dates:
|
||||
if file_modify_date:
|
||||
self.log.warning(
|
||||
f"use date modified:{file_modify_date} for {self.file_path}"
|
||||
)
|
||||
return file_modify_date
|
||||
|
||||
elif self.interactive:
|
||||
choices = []
|
||||
if date_filename:
|
||||
|
@ -404,6 +424,10 @@ class Media(ReadExif):
|
|||
choices.append((f"date created:'{date_created}'", date_created))
|
||||
if date_modified:
|
||||
choices.append((f"date modified:'{date_modified}'", date_modified))
|
||||
if file_modify_date:
|
||||
choices.append(
|
||||
(f"date modified:'{file_modify_date}'", file_modify_date)
|
||||
)
|
||||
choices.append(("custom", None))
|
||||
default = date_filename
|
||||
return self._get_date_media_interactive(choices, default)
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
from math import radians, cos, sqrt
|
||||
from datetime import datetime
|
||||
import hashlib
|
||||
import os
|
||||
import platform
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
|
||||
def checksum(file_path, blocksize=65536):
|
||||
|
@ -83,14 +86,17 @@ def get_date_regex(user_regex=None):
|
|||
return regex
|
||||
|
||||
|
||||
def get_date_from_string(string, user_regex=None):
|
||||
DATE_REGEX = get_date_regex()
|
||||
|
||||
|
||||
def get_date_from_string(string):
|
||||
"""Retrieve date stamp from string"""
|
||||
# If missing datetime from EXIF data check if filename is in datetime format.
|
||||
# For this use a user provided regex if possible.
|
||||
# Otherwise assume a filename such as IMG_20160915_123456.jpg as default.
|
||||
|
||||
matches = []
|
||||
for i, regex in get_date_regex(user_regex).items():
|
||||
for i, regex in DATE_REGEX.items():
|
||||
match = re.findall(regex, string)
|
||||
if match != []:
|
||||
if i == 'c':
|
||||
|
@ -118,6 +124,13 @@ def get_date_from_string(string, user_regex=None):
|
|||
return date
|
||||
|
||||
|
||||
def match_date_regex(regex, value):
|
||||
if re.match(regex, value) is not None:
|
||||
return re.sub(regex, r'\g<1>-\g<2>-\g<3>-', value)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def split_part(dedup_regex, path_part, items=None):
|
||||
"""
|
||||
Split part from regex
|
||||
|
@ -161,9 +174,6 @@ def camel2snake(name):
|
|||
r'(?!^)[A-Z]', lambda x: '_' + x.group(0).lower(), name[1:]
|
||||
)
|
||||
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
def open_file(path):
|
||||
if platform.system() == "Windows":
|
||||
|
|
|
@ -24,6 +24,7 @@ class TestSqlite:
|
|||
'DateOriginal': datetime(2013, 3, 27),
|
||||
'DateCreated': 'date_created',
|
||||
'DateModified': 'date_modified',
|
||||
'FileModifyDate': 'file_modify_date',
|
||||
'CameraMake': 'camera_make',
|
||||
'CameraModel': 'camera_model',
|
||||
'OriginalName':'original_name',
|
||||
|
@ -64,7 +65,24 @@ class TestSqlite:
|
|||
def test_add_metadata_data(self):
|
||||
result = tuple(self.sqlite.cur.execute("""select * from metadata where
|
||||
rowid=1""").fetchone())
|
||||
assert result == ('file_path', 'checksum', 'album', 'title', 2, '2012-03-27 00:00:00', '2013-03-27 00:00:00', 'date_created', 'date_modified', 'camera_make', 'camera_model', 'original_name', 'src_path', 'subdirs', 'filename')
|
||||
assert result == (
|
||||
'file_path',
|
||||
'checksum',
|
||||
'album',
|
||||
'title',
|
||||
2,
|
||||
'2012-03-27 00:00:00',
|
||||
'2013-03-27 00:00:00',
|
||||
'date_created',
|
||||
'date_modified',
|
||||
'file_modify_date',
|
||||
'camera_make',
|
||||
'camera_model',
|
||||
'original_name',
|
||||
'src_path',
|
||||
'subdirs',
|
||||
'filename'
|
||||
)
|
||||
|
||||
def test_get_checksum(self):
|
||||
assert not self.sqlite.get_checksum('invalid')
|
||||
|
|
|
@ -103,6 +103,8 @@ class TestMedia:
|
|||
assert date_media == media.metadata['date_created']
|
||||
elif media.metadata['date_modified']:
|
||||
assert date_media == media.metadata['date_modified']
|
||||
elif media.metadata['file_modify_date']:
|
||||
assert date_media == media.metadata['file_modify_date']
|
||||
|
||||
# Will be changed to get_metadata
|
||||
# check if metatadata type are correct
|
||||
|
|
Loading…
Reference in New Issue