111 lines
4.3 KiB
Python
Executable File
111 lines
4.3 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
"""
|
|
Import photos from SD card into folder with today's date + nickname
|
|
Use: import_media.py (--jpg|--raw|--both) <nickname of folder (optional)>
|
|
Add script to path
|
|
|
|
TODO:
|
|
8. Optionally allow specification of a backup location on another disk
|
|
or NAS to ship a 3rd copy to
|
|
10. Every config option has an arg override
|
|
11. Optionally rename file if EVENT name was passed in
|
|
-- STRETCH --
|
|
12. Make a graphical interface
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
from tqdm import tqdm
|
|
|
|
### Local Imports
|
|
from configure import CONFIG_FILE, Configure, files
|
|
from file_stuff import cleanup_sd, validate_config_dir_access, is_file
|
|
from hashing import gen_xxhashes, validate_xx_checksums
|
|
from bitmover import copy_files
|
|
from lumberjack import timber
|
|
from media import process_file
|
|
|
|
c = Configure(CONFIG_FILE)
|
|
config = c.load_config()
|
|
log = timber(__name__)
|
|
log.info("Starting import_media")
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("-e", "--event", help = "Event Name")
|
|
parser.add_argument("-s", "--source", help = "Source Directory to search for files")
|
|
parser.add_argument("-d", "--destination", help = "Destination Directory to put files")
|
|
parser.add_argument("-o", "--create-originals", help = "For images only, create an originals \
|
|
folder for safe keeping")
|
|
parser.add_argument("-b", "--backup-destination", help = "Create a backup of everything at the \
|
|
specified location")
|
|
parser.add_argument("-D", "--delete-source-files", help = "Delete files from SD after validating \
|
|
checksum of copied files")
|
|
parser.add_argument("-v", "--verify", help = "[True|False] Verify the checksum of \
|
|
the copied file")
|
|
parser.add_argument("-c", "--config", help = "Load the specified config file instead \
|
|
of the default " + CONFIG_FILE)
|
|
parser.add_argument("-g", "--generate-config", help = "Generate config file based on options \
|
|
passed from command arguments")
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.event:
|
|
EVENT = args.event
|
|
else:
|
|
EVENT = False
|
|
if args.source:
|
|
config['folders']['source']['base'] = args.source
|
|
if args.destination:
|
|
config['folders']['destination']['base'] = args.source
|
|
#if args.create-originals:
|
|
# pass
|
|
#if args.backup-destination:
|
|
# pass
|
|
#if args.delete-source-files:
|
|
# pass
|
|
#if args.config:
|
|
# pass
|
|
#if args.generate-config:
|
|
# pass
|
|
|
|
|
|
def find_files(directory):
|
|
""" find files to build a dictionary out of """
|
|
log.debug(f'find_files({directory})')
|
|
os.system('clear')
|
|
for folder, subfolders, filename in os.walk(directory):
|
|
log.debug(f'{folder},{filename}')
|
|
for f_type in config['file_types']:
|
|
log.debug(f'Type: {f_type}')
|
|
for ext in config['file_types'][f_type]:
|
|
log.debug(f'Extension: {ext}')
|
|
os.system('clear')
|
|
for file in tqdm(filename,
|
|
desc = 'Finding ' + ext + ' Files in ' + folder):
|
|
log.debug(f'File: {file}')
|
|
if file.lower().endswith(ext):
|
|
current_file = os.path.join(folder,file)
|
|
log.debug(f'Current File: {current_file}')
|
|
if is_file(current_file):
|
|
log.debug(f'Is File: {current_file}')
|
|
log.debug(f'Call function: process_file({folder}, {file}, {EVENT}, {config})')
|
|
#process_file(folder, f_type, file, ext)
|
|
process_file(folder, file, EVENT, config)
|
|
else:
|
|
log.warn(f"Skipping {current_file} as it does not look like a real file.")
|
|
|
|
GO = validate_config_dir_access(config)
|
|
if GO is True:
|
|
find_files(config['folders']['source']['base'])
|
|
copy_files(files,config)
|
|
gen_xxhashes(files)
|
|
validate_xx_checksums(files)
|
|
cleanup_sd(files,config)
|
|
else:
|
|
log.critical('There was a problem accessing one or more directories defined in the configuration.')
|
|
|
|
|
|
# dump_yaml(files, 'files_dict.yaml')
|
|
log.info('done.')
|