2015-10-31 09:31:35 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2016-03-12 20:09:28 +01:00
|
|
|
from __future__ import print_function
|
2015-10-31 09:31:35 +01:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
2016-01-18 10:54:12 +01:00
|
|
|
|
|
|
|
import click
|
2016-01-08 19:04:45 +01:00
|
|
|
from send2trash import send2trash
|
2015-10-31 09:31:35 +01:00
|
|
|
|
2016-01-08 01:45:55 +01:00
|
|
|
# Verify that external dependencies are present first, so the user gets a
|
|
|
|
# more user-friendly error instead of an ImportError traceback.
|
|
|
|
from elodie.dependencies import verify_dependencies
|
|
|
|
if not verify_dependencies():
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-10-31 09:31:35 +01:00
|
|
|
from elodie import constants
|
|
|
|
from elodie import geolocation
|
2016-11-10 05:33:52 +01:00
|
|
|
from elodie import log
|
2016-11-11 05:44:06 +01:00
|
|
|
from elodie.filesystem import FileSystem
|
|
|
|
from elodie.localstorage import Db
|
2016-04-07 10:08:33 +02:00
|
|
|
from elodie.media.base import Base
|
2016-01-02 08:52:11 +01:00
|
|
|
from elodie.media.media import Media
|
2016-04-07 10:08:33 +02:00
|
|
|
from elodie.media.text import Text
|
2016-01-04 07:10:56 +01:00
|
|
|
from elodie.media.audio import Audio
|
2015-10-31 09:31:35 +01:00
|
|
|
from elodie.media.photo import Photo
|
|
|
|
from elodie.media.video import Video
|
2016-11-11 05:44:06 +01:00
|
|
|
from elodie.result import Result
|
2015-10-31 09:31:35 +01:00
|
|
|
|
2015-12-30 01:23:38 +01:00
|
|
|
|
2015-12-31 00:29:13 +01:00
|
|
|
DB = Db()
|
|
|
|
FILESYSTEM = FileSystem()
|
2016-11-11 05:44:06 +01:00
|
|
|
RESULT = Result()
|
2015-10-31 09:31:35 +01:00
|
|
|
|
|
|
|
|
2016-09-08 06:08:13 +02:00
|
|
|
def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
|
2015-12-31 00:29:13 +01:00
|
|
|
"""Set file metadata and move it to destination.
|
|
|
|
"""
|
2016-01-08 02:21:02 +01:00
|
|
|
if not os.path.exists(_file):
|
2016-11-10 05:33:52 +01:00
|
|
|
log.warn('Could not find %s' % _file)
|
2016-03-12 20:09:28 +01:00
|
|
|
print('{"source":"%s", "error_msg":"Could not find %s"}' % \
|
|
|
|
(_file, _file))
|
2016-01-08 02:21:02 +01:00
|
|
|
return
|
2016-11-08 08:09:23 +01:00
|
|
|
# Check if the source, _file, is a child folder within destination
|
|
|
|
elif destination.startswith(os.path.dirname(_file)):
|
2016-11-11 05:44:06 +01:00
|
|
|
print('{"source": "%s", "destination": "%s", "error_msg": "Source cannot be in destination"}' % (_file, destination))
|
2016-11-08 08:09:23 +01:00
|
|
|
return
|
|
|
|
|
2016-01-08 02:21:02 +01:00
|
|
|
|
2016-04-07 10:08:33 +02:00
|
|
|
media = Media.get_class_by_file(_file, [Text, Audio, Photo, Video])
|
2015-12-30 01:23:38 +01:00
|
|
|
if not media:
|
2016-11-10 05:33:52 +01:00
|
|
|
log.warn('Not a supported file (%s)' % _file)
|
2016-03-12 20:09:28 +01:00
|
|
|
print('{"source":"%s", "error_msg":"Not a supported file"}' % _file)
|
2015-12-30 01:23:38 +01:00
|
|
|
return
|
2015-10-31 09:31:35 +01:00
|
|
|
|
2015-12-31 00:29:13 +01:00
|
|
|
if album_from_folder:
|
2015-12-30 01:23:38 +01:00
|
|
|
media.set_album_from_folder()
|
2015-10-31 09:31:35 +01:00
|
|
|
|
2015-12-31 00:29:13 +01:00
|
|
|
dest_path = FILESYSTEM.process_file(_file, destination,
|
2016-09-08 06:08:13 +02:00
|
|
|
media, allowDuplicate=allow_duplicates, move=False)
|
2015-12-30 01:23:38 +01:00
|
|
|
if dest_path:
|
2016-03-12 20:09:28 +01:00
|
|
|
print('%s -> %s' % (_file, dest_path))
|
2016-01-08 19:04:45 +01:00
|
|
|
if trash:
|
|
|
|
send2trash(_file)
|
2015-12-03 08:47:54 +01:00
|
|
|
|
2016-04-21 07:23:57 +02:00
|
|
|
return dest_path or None
|
|
|
|
|
2015-12-19 05:08:12 +01:00
|
|
|
|
2016-01-18 10:54:12 +01:00
|
|
|
@click.command('import')
|
|
|
|
@click.option('--destination', type=click.Path(file_okay=False),
|
|
|
|
required=True, help='Copy imported files into this directory.')
|
|
|
|
@click.option('--source', type=click.Path(file_okay=False),
|
|
|
|
help='Import files from this directory, if specified.')
|
|
|
|
@click.option('--file', type=click.Path(dir_okay=False),
|
|
|
|
help='Import this file, if specified.')
|
|
|
|
@click.option('--album-from-folder', default=False, is_flag=True,
|
|
|
|
help="Use images' folders as their album names.")
|
|
|
|
@click.option('--trash', default=False, is_flag=True,
|
|
|
|
help='After copying files, move the old files to the trash.')
|
2016-09-08 06:08:13 +02:00
|
|
|
@click.option('--allow-duplicates', default=False, is_flag=True,
|
|
|
|
help='Import the file even if it\'s already been imported.')
|
2016-01-18 10:54:12 +01:00
|
|
|
@click.argument('paths', nargs=-1, type=click.Path())
|
2016-09-08 06:08:13 +02:00
|
|
|
def _import(destination, source, file, album_from_folder, trash, paths, allow_duplicates):
|
2016-10-08 19:02:16 +02:00
|
|
|
"""Import files or directories by reading their EXIF and organizing them accordingly.
|
2015-12-31 00:29:13 +01:00
|
|
|
"""
|
2016-11-08 08:09:23 +01:00
|
|
|
destination = os.path.abspath(os.path.expanduser(destination))
|
2015-12-19 05:08:12 +01:00
|
|
|
|
2016-01-08 02:21:02 +01:00
|
|
|
files = set()
|
2016-01-18 10:54:12 +01:00
|
|
|
paths = set(paths)
|
|
|
|
if source:
|
|
|
|
paths.add(source)
|
|
|
|
if file:
|
|
|
|
paths.add(file)
|
2016-01-08 18:50:14 +01:00
|
|
|
for path in paths:
|
2016-01-08 02:21:02 +01:00
|
|
|
path = os.path.expanduser(path)
|
|
|
|
if os.path.isdir(path):
|
|
|
|
files.update(FILESYSTEM.get_all_files(path, None))
|
|
|
|
else:
|
|
|
|
files.add(path)
|
2015-10-31 09:31:35 +01:00
|
|
|
|
2015-12-30 01:23:38 +01:00
|
|
|
for current_file in files:
|
2016-11-11 05:44:06 +01:00
|
|
|
dest_path = import_file(current_file, destination, album_from_folder,
|
2016-09-08 06:08:13 +02:00
|
|
|
trash, allow_duplicates)
|
2016-11-11 05:44:06 +01:00
|
|
|
RESULT.append((current_file, dest_path))
|
|
|
|
|
|
|
|
RESULT.write()
|
2015-12-31 00:29:13 +01:00
|
|
|
|
|
|
|
|
2015-12-30 23:52:01 +01:00
|
|
|
def update_location(media, file_path, location_name):
|
|
|
|
"""Update location exif metadata of media.
|
|
|
|
"""
|
|
|
|
location_coords = geolocation.coordinates_by_name(location_name)
|
|
|
|
|
|
|
|
if location_coords and 'latitude' in location_coords and \
|
|
|
|
'longitude' in location_coords:
|
|
|
|
location_status = media.set_location(location_coords[
|
|
|
|
'latitude'], location_coords['longitude'])
|
|
|
|
if not location_status:
|
2016-11-10 05:33:52 +01:00
|
|
|
log.error('Failed to update location')
|
2016-03-12 20:09:28 +01:00
|
|
|
print(('{"source":"%s",' % file_path,
|
|
|
|
'"error_msg":"Failed to update location"}'))
|
2015-12-30 23:52:01 +01:00
|
|
|
sys.exit(1)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def update_time(media, file_path, time_string):
|
|
|
|
"""Update time exif metadata of media.
|
|
|
|
"""
|
|
|
|
time_format = '%Y-%m-%d %H:%M:%S'
|
|
|
|
if re.match(r'^\d{4}-\d{2}-\d{2}$', time_string):
|
|
|
|
time_string = '%s 00:00:00' % time_string
|
|
|
|
elif re.match(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}\d{2}$', time_string):
|
|
|
|
msg = ('Invalid time format. Use YYYY-mm-dd hh:ii:ss or YYYY-mm-dd')
|
2016-11-10 05:33:52 +01:00
|
|
|
log.error(msg)
|
2016-03-12 20:09:28 +01:00
|
|
|
print('{"source":"%s", "error_msg":"%s"}' % (file_path, msg))
|
2015-12-30 23:52:01 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
time = datetime.strptime(time_string, time_format)
|
|
|
|
media.set_date_taken(time)
|
|
|
|
return True
|
2015-12-03 08:47:54 +01:00
|
|
|
|
2015-10-31 09:31:35 +01:00
|
|
|
|
2016-01-18 10:54:12 +01:00
|
|
|
@click.command('update')
|
|
|
|
@click.option('--album', help='Update the image album.')
|
|
|
|
@click.option('--location', help=('Update the image location. Location '
|
|
|
|
'should be the name of a place, like "Las '
|
|
|
|
'Vegas, NV".'))
|
|
|
|
@click.option('--time', help=('Update the image time. Time should be in '
|
|
|
|
'YYYY-mm-dd hh:ii:ss or YYYY-mm-dd format.'))
|
|
|
|
@click.option('--title', help='Update the image title.')
|
|
|
|
@click.argument('files', nargs=-1, type=click.Path(dir_okay=False),
|
|
|
|
required=True)
|
|
|
|
def _update(album, location, time, title, files):
|
2016-10-08 19:02:16 +02:00
|
|
|
"""Update a file's EXIF. Automatically modifies the file's location and file name accordingly.
|
2015-12-30 23:52:01 +01:00
|
|
|
"""
|
2016-11-11 05:44:06 +01:00
|
|
|
for current_file in files:
|
|
|
|
if not os.path.exists(current_file):
|
|
|
|
if constants.debug:
|
|
|
|
print('Could not find %s' % current_file)
|
2016-03-12 20:09:28 +01:00
|
|
|
print('{"source":"%s", "error_msg":"Could not find %s"}' % \
|
2016-11-11 05:44:06 +01:00
|
|
|
(current_file, current_file))
|
2015-10-31 09:31:35 +01:00
|
|
|
continue
|
|
|
|
|
2016-11-11 05:44:06 +01:00
|
|
|
current_file = os.path.expanduser(current_file)
|
2015-12-30 01:30:53 +01:00
|
|
|
destination = os.path.expanduser(os.path.dirname(os.path.dirname(
|
2016-11-11 05:44:06 +01:00
|
|
|
os.path.dirname(current_file))))
|
2015-12-02 23:07:23 +01:00
|
|
|
|
2016-11-11 05:44:06 +01:00
|
|
|
media = Media.get_class_by_file(current_file, [Text, Audio, Photo, Video])
|
2015-12-30 01:30:53 +01:00
|
|
|
if not media:
|
2015-10-31 09:31:35 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
updated = False
|
2016-01-18 10:54:12 +01:00
|
|
|
if location:
|
2016-11-11 05:44:06 +01:00
|
|
|
update_location(media, current_file, location)
|
2016-01-18 10:54:12 +01:00
|
|
|
updated = True
|
|
|
|
if time:
|
2016-11-11 05:44:06 +01:00
|
|
|
update_time(media, current_file, time)
|
2016-01-18 10:54:12 +01:00
|
|
|
updated = True
|
|
|
|
if album:
|
|
|
|
media.set_album(album)
|
2015-10-31 09:31:35 +01:00
|
|
|
updated = True
|
|
|
|
|
|
|
|
# Updating a title can be problematic when doing it 2+ times on a file.
|
2015-12-30 01:30:53 +01:00
|
|
|
# You would end up with img_001.jpg -> img_001-first-title.jpg ->
|
|
|
|
# img_001-first-title-second-title.jpg.
|
2015-10-31 09:31:35 +01:00
|
|
|
# To resolve that we have to track the prior title (if there was one.
|
2015-12-30 01:30:53 +01:00
|
|
|
# Then we massage the updated_media's metadata['base_name'] to remove
|
|
|
|
# the old title.
|
|
|
|
# Since FileSystem.get_file_name() relies on base_name it will properly
|
|
|
|
# rename the file by updating the title instead of appending it.
|
2015-10-31 09:31:35 +01:00
|
|
|
remove_old_title_from_name = False
|
2016-01-18 10:54:12 +01:00
|
|
|
if title:
|
2015-10-31 09:31:35 +01:00
|
|
|
# We call get_metadata() to cache it before making any changes
|
|
|
|
metadata = media.get_metadata()
|
2016-01-18 10:54:12 +01:00
|
|
|
title_update_status = media.set_title(title)
|
2015-10-31 09:31:35 +01:00
|
|
|
original_title = metadata['title']
|
2015-12-30 01:30:53 +01:00
|
|
|
if title_update_status and original_title:
|
2015-12-31 00:29:13 +01:00
|
|
|
# @TODO: We should move this to a shared method since
|
|
|
|
# FileSystem.get_file_name() does it too.
|
|
|
|
original_title = re.sub(r'\W+', '-', original_title.lower())
|
2015-10-31 09:31:35 +01:00
|
|
|
original_base_name = metadata['base_name']
|
|
|
|
remove_old_title_from_name = True
|
|
|
|
updated = True
|
2015-12-30 01:30:53 +01:00
|
|
|
|
|
|
|
if updated:
|
2016-11-11 05:44:06 +01:00
|
|
|
updated_media = Media.get_class_by_file(current_file,
|
2016-04-07 10:08:33 +02:00
|
|
|
[Text, Audio, Photo, Video])
|
2015-12-31 00:29:13 +01:00
|
|
|
# See comments above on why we have to do this when titles
|
|
|
|
# get updated.
|
2015-12-30 01:30:53 +01:00
|
|
|
if remove_old_title_from_name and len(original_title) > 0:
|
2015-10-31 09:31:35 +01:00
|
|
|
updated_media.get_metadata()
|
2015-12-30 01:30:53 +01:00
|
|
|
updated_media.set_metadata_basename(
|
|
|
|
original_base_name.replace('-%s' % original_title, ''))
|
2015-10-31 09:31:35 +01:00
|
|
|
|
2016-11-11 05:44:06 +01:00
|
|
|
dest_path = FILESYSTEM.process_file(current_file, destination,
|
2015-12-30 01:30:53 +01:00
|
|
|
updated_media, move=True, allowDuplicate=True)
|
2016-11-11 05:44:06 +01:00
|
|
|
log.info(u'%s -> %s' % (current_file, dest_path))
|
2016-03-12 20:09:28 +01:00
|
|
|
print('{"source":"%s", "destination":"%s"}' % (file_path,
|
|
|
|
dest_path))
|
2015-12-31 00:29:13 +01:00
|
|
|
# If the folder we moved the file out of or its parent are empty
|
|
|
|
# we delete it.
|
2016-11-11 05:44:06 +01:00
|
|
|
FILESYSTEM.delete_directory_if_empty(os.path.dirname(current_file))
|
2015-12-31 00:29:13 +01:00
|
|
|
FILESYSTEM.delete_directory_if_empty(
|
2016-11-11 05:44:06 +01:00
|
|
|
os.path.dirname(os.path.dirname(current_file)))
|
|
|
|
RESULT.append((current_file, dest_path))
|
|
|
|
else:
|
|
|
|
RESULT.append((current_file, None))
|
|
|
|
|
|
|
|
RESULT.write()
|
2015-10-31 09:31:35 +01:00
|
|
|
|
|
|
|
|
2016-01-18 10:54:12 +01:00
|
|
|
@click.group()
|
|
|
|
def main():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
main.add_command(_import)
|
|
|
|
main.add_command(_update)
|
2015-12-31 00:29:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-01-18 10:54:12 +01:00
|
|
|
main()
|