Add tool to retroactively add OriginalFileName to all files in library #107 (#190)

This commit is contained in:
Jaisen Mathai 2017-01-22 23:18:28 -08:00 committed by GitHub
parent 0864f58a12
commit 15cfe61e03
5 changed files with 120 additions and 4 deletions

View File

@ -234,7 +234,7 @@ class Media(Base):
return status return status
def set_original_name(self): def set_original_name(self, name=None):
"""Sets the original name EXIF tag if not already set. """Sets the original name EXIF tag if not already set.
:returns: True, False, None :returns: True, False, None
@ -247,7 +247,10 @@ class Media(Base):
return None return None
source = self.source source = self.source
name = os.path.basename(source)
if not name:
name = os.path.basename(source)
tags = {self.original_name_key: name} tags = {self.original_name_key: name}
status = self.__set_tags(tags) status = self.__set_tags(tags)
self.reset_cache() self.reset_cache()

View File

@ -111,7 +111,7 @@ class Text(Base):
self.reset_cache() self.reset_cache()
return status return status
def set_original_name(self): def set_original_name(self, name=None):
"""Sets the original name if not already set. """Sets the original name if not already set.
:returns: True, False, None :returns: True, False, None
@ -124,7 +124,10 @@ class Text(Base):
return None return None
source = self.source source = self.source
name = os.path.basename(source)
if not name:
name = os.path.basename(source)
status = self.write_metadata(original_name=name) status = self.write_metadata(original_name=name)
self.reset_cache() self.reset_cache()
return status return status

View File

@ -117,6 +117,25 @@ def test_set_original_name_when_does_not_exist():
assert metadata_after['original_name'] == 'plain.jpg', metadata_after assert metadata_after['original_name'] == 'plain.jpg', metadata_after
assert result is True, result assert result is True, result
def test_set_original_name_with_arg():
temporary_folder, folder = helper.create_working_folder()
origin = '%s/%s' % (folder, 'plain.jpg')
file = helper.get_file('plain.jpg')
shutil.copyfile(file, origin)
new_name = helper.random_string(15)
media = Media.get_class_by_file(origin, [Photo])
metadata_before = media.get_metadata()
result = media.set_original_name(new_name)
metadata_after = media.get_metadata()
assert metadata_before['original_name'] is None, metadata_before
assert metadata_after['original_name'] == new_name, metadata_after
assert result is True, result
def test_set_original_name(): def test_set_original_name():
files = ['plain.jpg', 'audio.m4a', 'photo.nef', 'video.mov'] files = ['plain.jpg', 'audio.m4a', 'photo.nef', 'video.mov']

View File

@ -297,3 +297,22 @@ def test_set_original_name():
assert metadata['original_name'] is None, metadata['original_name'] assert metadata['original_name'] is None, metadata['original_name']
assert metadata_updated['original_name'] == random_file_name, metadata_updated['original_name'] assert metadata_updated['original_name'] == random_file_name, metadata_updated['original_name']
def test_set_original_name_with_arg():
temporary_folder, folder = helper.create_working_folder()
random_file_name = '%s.txt' % helper.random_string(10)
origin = '%s/%s' % (folder, random_file_name)
shutil.copyfile(helper.get_file('valid.txt'), origin)
new_name = helper.random_string(15)
text = Text(origin)
metadata = text.get_metadata()
text.set_original_name(new_name)
metadata_updated = text.get_metadata()
shutil.rmtree(folder)
assert metadata['original_name'] is None, metadata['original_name']
assert metadata_updated['original_name'] == new_name, metadata_updated['original_name']

View File

@ -0,0 +1,72 @@
from __future__ import print_function
import os
import re
import sys
from elodie import constants
from elodie import geolocation
from elodie import log
from elodie.compatability import _decode
from elodie.filesystem import FileSystem
from elodie.localstorage import Db
from elodie.media.base import Base, get_all_subclasses
from elodie.media.media import Media
from elodie.media.text import Text
from elodie.media.audio import Audio
from elodie.media.photo import Photo
from elodie.media.video import Video
from elodie.result import Result
def main(argv):
filesystem = FileSystem()
result = Result()
subclasses = get_all_subclasses()
paths = argv[1:]
for path in paths:
path = os.path.expanduser(path)
if os.path.isdir(path):
for source in filesystem.get_all_files(path, None):
status = add_original_name(source, subclasses)
result.append((source, status))
else:
status = add_original_name(source, subclasses)
result.append((source, status))
result.write()
def add_original_name(source, subclasses):
media = Media.get_class_by_file(source, subclasses)
metadata = media.get_metadata()
if metadata['original_name'] is not None:
print('{} already has OriginalFileName...Skipping'.format(source))
return
original_name = parse_original_name_from_media(metadata)
return media.set_original_name(original_name)
def parse_original_name_from_media(metadata):
# 2015-07-23_04-31-12-img_9414-test3.jpg
base_name = metadata['base_name']
title = metadata['title']
extension = metadata['extension']
date_regex = r'^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}-'
if not re.match(date_regex, base_name):
print("File name did not match date pattern...Skipping")
return
trimmed_base_name = re.sub(date_regex, '', base_name)
if title:
trimmed_base_name = trimmed_base_name.replace(
'-{}'.format(title),
''
)
return '{}.{}'.format(trimmed_base_name, extension)
if __name__ == "__main__":
main(sys.argv)