Created
June 21, 2016 08:45
-
-
Save bhalash/245b85ab970102f4099755e00ef605cf to your computer and use it in GitHub Desktop.
Sort Dropbox-uploaded photographs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env zsh | |
| # | |
| # Functions | |
| # | |
| # | |
| # Check if Photograph is Square | |
| # | |
| is_square() { | |
| [[ $(identify -format '%w' "$1") == $(identify -format '%h' "$1") ]] | |
| echo $? | |
| } | |
| # | |
| # Check Mimetype | |
| # | |
| is_image() { | |
| grep 'image' <<< $(file --mime-type "$1") > /dev/null 2>&1 | |
| echo $? | |
| } | |
| # | |
| # Create Directory from Date in Filename | |
| # Images imported from Dropbox are named in format "2015-12-27 22.13.26.jpg" | |
| # | |
| send_to_dated_directory() { | |
| local filename_date=${1:0:10} | |
| if [[ $filename_date =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then | |
| mkdir $filename_date > /dev/null 2>&1 | |
| mv "$1" $filename_date | |
| fi | |
| } | |
| # | |
| # Variables | |
| # | |
| uploads_folder="${HOME}/Dropbox/Camera Uploads/" | |
| instagram_folder="${HOME}/Dropbox/Photos/Instagram/" | |
| # | |
| # Main Loop | |
| # | |
| if [[ ! -d $uploads_folder ]] || [[ ! -d $instagram_folder ]]; then | |
| exit 1 | |
| fi | |
| cd "$uploads_folder" | |
| for file in *.*; do | |
| case ${file##*.} in | |
| gif|png) rm "$file";; | |
| mov|jpg) mv "$file" ${file// /_} && file=${file// /_};; | |
| esac | |
| if [[ -f $file ]]; then | |
| if [[ $(is_image $file) == 0 ]] && [[ $(is_square $file) == 0 ]]; then | |
| mv $file "$instagram_folder" | |
| else | |
| send_to_dated_directory $file | |
| fi | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment