2015-10-07 07:35:11 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from elodie import arguments
|
2015-10-10 09:02:53 +02:00
|
|
|
from elodie.media.photo import Photo
|
2015-10-07 07:35:11 +02:00
|
|
|
from elodie.media.video import Video
|
|
|
|
from elodie.filesystem import FileSystem
|
2015-10-10 09:02:53 +02:00
|
|
|
from elodie.localstorage import Db
|
2015-10-07 07:35:11 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
db = Db()
|
|
|
|
filesystem = FileSystem()
|
2015-10-10 09:02:53 +02:00
|
|
|
|
2015-10-11 09:12:53 +02:00
|
|
|
def process_file(_file, destination, media):
|
2015-10-11 03:24:50 +02:00
|
|
|
checksum = db.checksum(_file)
|
|
|
|
if(checksum == None):
|
|
|
|
print 'Could not get checksum for %s. Skipping...' % _file
|
|
|
|
return
|
2015-10-07 07:35:11 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
if(db.check_hash(checksum) == True):
|
|
|
|
print '%s already exists at %s. Skipping...' % (_file, db.get_hash(checksum))
|
|
|
|
return
|
2015-10-07 07:35:11 +02:00
|
|
|
|
2015-10-11 09:12:53 +02:00
|
|
|
metadata = media.get_metadata()
|
2015-10-07 07:35:11 +02:00
|
|
|
|
2015-10-12 09:37:57 +02:00
|
|
|
directory_name = filesystem.get_folder_path(date=metadata['date_taken'], latitude=metadata['latitude'], longitude=metadata['longitude'])
|
2015-10-07 07:35:11 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
dest_directory = '%s/%s' % (destination, directory_name)
|
|
|
|
# TODO remove the day prefix of the file that was there prior to the crawl
|
|
|
|
file_name = filesystem.get_file_name(media)
|
|
|
|
dest_path = '%s/%s' % (dest_directory, file_name)
|
2015-10-07 07:35:11 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
filesystem.create_directory(dest_directory)
|
2015-10-10 09:02:53 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
print '%s -> %s' % (_file, dest_path)
|
|
|
|
shutil.copy2(_file, dest_path)
|
|
|
|
#shutil.move(_file, dest_path)
|
|
|
|
db.add_hash(checksum, dest_path)
|
2015-10-07 07:35:11 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
def main(argv):
|
|
|
|
args = arguments.parse(argv, None, ['file=','type=','source=','destination='], './import.py --type=<photo or video> --source=<source directory> -destination=<destination directory>')
|
2015-10-07 07:35:11 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
if('destination' not in args):
|
|
|
|
print 'No destination passed in'
|
|
|
|
sys.exit(2)
|
2015-10-10 09:02:53 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
destination = args['destination']
|
|
|
|
if('type' in args and args['type'] == 'photo'):
|
|
|
|
media_type = Photo
|
|
|
|
else:
|
|
|
|
media_type = Video
|
2015-10-10 09:02:53 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
if('source' in args):
|
|
|
|
source = args['source']
|
2015-10-10 09:02:53 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
write_counter = 0
|
|
|
|
for current_file in filesystem.get_all_files(source, media_type.get_valid_extensions()):
|
2015-10-11 09:12:53 +02:00
|
|
|
media = media_type(current_file)
|
|
|
|
|
|
|
|
if(media_type.__name__ == 'Video'):
|
|
|
|
filesystem.set_date_from_path_video(media)
|
|
|
|
|
|
|
|
process_file(current_file, destination, media)
|
2015-10-11 03:24:50 +02:00
|
|
|
# Write to the hash database every 10 iterations
|
|
|
|
write_counter += 1
|
|
|
|
if(write_counter % 10 == 0):
|
|
|
|
db.update_hash_db()
|
2015-10-10 09:02:53 +02:00
|
|
|
|
2015-10-11 03:24:50 +02:00
|
|
|
# If there's anything we haven't written to the hash database then write it now
|
|
|
|
if(write_counter % 10 != 10):
|
|
|
|
db.update_hash_db()
|
|
|
|
elif('file' in args):
|
2015-10-11 09:12:53 +02:00
|
|
|
media = media_type(args['file'])
|
|
|
|
if(media_type.__name__ == 'Video'):
|
|
|
|
filesystem.set_date_from_path_video(media)
|
|
|
|
|
|
|
|
process_file(args['file'], destination, media)
|
2015-10-10 09:02:53 +02:00
|
|
|
db.update_hash_db()
|
2015-10-07 07:35:11 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv[1:])
|
2015-10-11 03:24:50 +02:00
|
|
|
sys.exit(0)
|
2015-10-11 09:12:53 +02:00
|
|
|
|