From d459ad1657e5ac24c3eb40b57f27ac2ca8237b60 Mon Sep 17 00:00:00 2001 From: George Rawlinson Date: Tue, 15 Nov 2016 09:39:07 +1300 Subject: [PATCH] Add photo scripts --- bin/bin/count-extdir | 31 ++++++++++++++ bin/bin/error | 11 +++++ bin/bin/import-photos | 98 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100755 bin/bin/count-extdir create mode 100755 bin/bin/error create mode 100755 bin/bin/import-photos diff --git a/bin/bin/count-extdir b/bin/bin/count-extdir new file mode 100755 index 0000000..d25aef0 --- /dev/null +++ b/bin/bin/count-extdir @@ -0,0 +1,31 @@ +#!/bin/bash -e + +# Name : count-extdir +# Summary : Count number of files with specific extension in a given directory +# Author : George Rawlinson +# Depends : error + +# check number of arguments - should be 2 +if [ ! $# -eq 2 ]; then + error "Expected 2 arguments: directory & extension" + exit 1 +fi + +# check if directory exists +if [ ! -d "$1" ]; then + error "Directory does not exist" + exit 1 +fi + +# recursively find EXT in DIR +find "$1" -type f -iname "*.$2" -printf '\n'| wc -l +# find flags +# type -f - limit to files +# iname - case insensitive +# printf - print new line after each result +# | - pipe results to next command +# wc -l - count number of new lines +# Source : http://unix.stackexchange.com/questions/146760/count-files-in-a-directory-by-extension + + +exit 0 diff --git a/bin/bin/error b/bin/bin/error new file mode 100755 index 0000000..8704575 --- /dev/null +++ b/bin/bin/error @@ -0,0 +1,11 @@ +#!/bin/bash -e + +# Name : error +# Summary : prints strings in red +# Author : George Rawlinson + +readonly RED='\033[0;31m' +readonly RESET='\033[0m' +printf "${RED}%s${RESET}\n" "$@" + +exit 0 diff --git a/bin/bin/import-photos b/bin/bin/import-photos new file mode 100755 index 0000000..2476d22 --- /dev/null +++ b/bin/bin/import-photos @@ -0,0 +1,98 @@ +#!/bin/bash -e + +# Name : import-photos +# Summary : Import & sort photos in a given directory +# Author : George Rawlinson +# Depends : error, exiftool, mktemp + +# constants +readonly LIBRARY_DIRECTORY="$HOME/Photos" # location of photo library +readonly PHOTO_EXTENSION="cr2" # photo extension (Canon RAW) +readonly PHOTO_LIMIT=9999 # limit on photos to process +readonly DEBUG_FLAG=0 # used to test exiftool + +# check dependencies +count=0 +for dependency in error exiftool mktemp +do + # list missing dependencies + if [ ! -x "$(command -v $dependency)" ]; then + echo "$dependency missing" + count=$((count+1)) + fi +done + +# exit script if any dependencies missing +if [ $count -ne 0 ]; then + exit 1 +fi + +# check number of arguments - should be 1 +if [ ! $# -eq 1 ]; then + error "Expected 1 argument: directory" + exit 1 +fi + +# get source directory +source_directory="$1" + +# check if source directory exists +if [ ! -d "$source_directory" ]; then + error "Directory does not exist" + exit 1 +fi + +# get number of photos +num_photos=$(count-extdir "$source_directory" "$PHOTO_EXTENSION") + +# check numbers +if [ "$num_photos" -gt "$PHOTO_LIMIT" ]; then + error "Too many photos to import" + exit 1 +elif [ "$num_photos" -eq 0 ]; then + echo "No photos to import" + exit 0 +fi + +# create temporary directory +echo "Creating temporary directory" +temporary_directory=$(mktemp -d -p /tmp photos.XXXXXX) +# mktemp flags +# u - dry run +# d - make directory +# p - make in given dir +# photos.XXXXXX - create folder titled photos.XXXXXX where X = random characters + +# copy photos to temporary directory +echo "Copying $num_photos photos to temporary directory" +cp -R "$source_directory" "$temporary_directory" + +# import photos to library +echo "Importing $num_photos photos" + +if [ "$DEBUG_FLAG" -eq 1 ]; then + # shellcheck disable=SC2016 + exiftool -fileOrder DateTimeOriginal -recurse -verbose '-testname<${DateTimeOriginal}-${SerialNumber}%-.4nc.%e' -d "$LIBRARY_DIRECTORY"/%Y/%Y-%m-%d/%Y%m%d -ext cr2 "$temporary_directory" +else + # shellcheck disable=SC2016 + exiftool -fileOrder DateTimeOriginal -recurse -quiet '-FileName<${DateTimeOriginal}-${SerialNumber}%-.4nc.%e' -d "$LIBRARY_DIRECTORY"/%Y/%Y-%m-%d/%Y%m%d -ext cr2 "$temporary_directory" +fi +# exiftool flags +# fileOrder DateTimeOriginal - before moving, sort in chronological order +# recurse + verbose/quiet - recursive search + verbose/quiet output +# testname/FileName - name of the output file (testname used for dry runs) + # DateTimeOriginal - YYYYMMDD (date format set in -d flag below) + # SerialNumber - camera serial number + # -.4nc.%e - sequential 4 digit number starting at 0001, followed by extension + # d - directory to save to + # - DIR/YYYY/YYYY-MM-DD/FileName (set using this flag + testname/FileName) +# ext only match if ext=cr2 (Canon RAW) + +# delete temporary directory +echo "Deleting temporary directory" +rm -rf "$temporary_directory" + +# done! +echo "All done!" + +exit 0