Skip to content

Instantly share code, notes, and snippets.

@bhalash bhalash/instasort.sh Secret
Last active Sep 16, 2017

Embed
What would you like to do?
instasort 4.0.1
#!/usr/bin/env zsh
# ____ __ _____ __
# / _/___ _____/ /_____ _/ ___/____ _____/ /_
# / // __ \/ ___/ __/ __ `/\__ \/ __ \/ ___/ __/
# _/ // / / (__ ) /_/ /_/ /___/ / /_/ / / / /_
# /___/_/ /_/____/\__/\__,_//____/\____/_/ \__/
#
# Instagram Sorter Script v4.0.1.
#
# This script:
#
# 1. Gives each image a shell-friendly name.
# 2. Moves detected Instagram images to their own folder.
# 3. Removes detected screenshots.
# 4. Otherwise moves all media to a dated folder.
#
# TODO: Derived image date from EXIF. Using filename isn't dependable.
#
# @author Mark Grealish <mark@bhalash.com>
#
# Functions
#
# Was File Created by Instagram?
#
# @example
#
# exif:Software: Instagram
# exif:Software: Layout from Instagram
# exif:Software: 10.3.1
#
# Files created by Instagram will be marked so in EXIF. I downcase the exif:software
# value and test it against 'instagram'.
function is_instagram {
local SOFTWARE=$(identify -format '%[exif:software]' $1)
[[ ${SOFTWARE:l} =~ 'instagram' ]]
echo $?
}
# Create Directory from Date in Filename
#
# Images imported from Dropbox are named in format "2015-12-27 22.13.26.jpg"
function send_to_dated_directory {
local FILENAME_DATE=${1:0:10}
local DATE_REGEX='^[0-9]{4}-[0-9]{2}-[0-9]{2}$'
if [[ $FILENAME_DATE =~ $DATE_REGEX ]]; then
mkdir $FILENAME_DATE > /dev/null 2>&1
mv "$1" $FILENAME_DATE
fi
}
# Move File to Appropriate Location
#
# 1. To a dated folder if not an Instagram image.
# 2. To the Instagram folder otherwise.
function move_file {
local EXTENSION_REGEX='^jpg$'
if [[ ${1##*.} =~ $EXTENSION_REGEX ]] && [[ $(is_instagram $1) == 0 ]]; then
mv $1 "$INSTAGRAM_FOLDER"
else
send_to_dated_directory $1
fi
}
#
# Variables
#
UPLOADS_FOLDER="${HOME}/Dropbox/Camera Uploads/"
INSTAGRAM_FOLDER="${HOME}/Dropbox/Photos/Instagram/"
cd "$UPLOADS_FOLDER"
for FILE in *.*; do
if [[ ! -f "$FILE" ]]; then
continue
fi
# NICENAME is A-Z downcased and spaces replaced with _.
# 2016 Foobar Image.JPG => 2016_foobar_image.jpg
NICENAME=$((tr '[A-Z]' '[a-z]' | tr ' ' '_') <<< "$FILE")
mv "$FILE" $NICENAME && FILE=$NICENAME
case ${FILE##*.} in
gif|png) rm $FILE;;
*) move_file $FILE;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.