Last active
January 1, 2016 09:35
-
-
Save bhalash/2c59347d56d0769a3fb8 to your computer and use it in GitHub Desktop.
Instagram Sorter
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
| #!/bin/bash | |
| # https://www.bhalash.com/archives/13544793955 | |
| # April 23, 2013 | |
| # Need to escape the source path both as when we declare and use it, or Errors Occur. | |
| IFS=' ' | |
| src="/home/bhalash/Dropbox/Camera Uploads/" | |
| destination="/home/bhalash/Dropbox/Photos/Instagram" | |
| temp=/tmp/instafind | |
| trim_spaces() { | |
| # Safely remove spaces in the file name. | |
| x=$(echo "$1" | sed -e 's/ /\_/g') | |
| mv "$1" $x echo $x | |
| } | |
| check_if_square() { | |
| # 1. Get the width and height of the image file. | |
| x=$(identify -format '%w' $1) | |
| y=$(identify -format '%h' $1) | |
| # 2. Check whether the file is landscape or portrait (x > y or y > x). | |
| if [[ $x -ge $y ]]; then | |
| # If width > height. | |
| x=$(echo "scale=0; $x * $2" | bc -l) | |
| elif [[ $y -ge $x ]]; then | |
| # If height > width. | |
| y=$(echo "scale=0; $y * $2" | bc -l) | |
| fi | |
| # If the image fits the provided aspect ration, we return true, else false. | |
| if [[ $x == $y ]]; then | |
| mv $1 $3 | |
| fi | |
| return 0 | |
| } | |
| make_directory() { | |
| # Creates a dated folder and moves the image into it. | |
| a=$(echo "$1" | cut -c1-10) | |
| if [[ ! -d $a ]]; then | |
| mkdir $a | |
| fi | |
| mv $1 $a | |
| return 0 | |
| } | |
| # Two similar loops to different ends. First loop looks at all files that need to be renamed. | |
| # Second loop evaluates the most recent $checkCount files. | |
| # It was necessary to pipe the list to a temp file, and read one line a time with sed. | |
| # Directly piping ls lead to filenames with spaces being broken over two lines. | |
| cd "$src" | |
| for n in $(ls *.mov | grep " "); do | |
| trim_spaces "$n" | |
| done | |
| # Remove crud from iMessages. | |
| for n in $(ls . | grep "png\\|gif"); do | |
| rm "$n" | |
| done | |
| ls -1t *.jpg | grep " " > $temp | |
| count=$(wc -l < $temp) | |
| for n in $(seq $count); do | |
| file="$(sed -n "$n"p $temp)" | |
| file=$(trim_spaces "$file") | |
| check_if_square $file 1 $destination | |
| if [[ -e $file ]]; then | |
| make_directory $file | |
| fi | |
| done | |
| rm $temp | |
| exit 0 |
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 bash | |
| # https://www.bhalash.com/archives/13544803320 | |
| # December 14, 2014 | |
| # | |
| # Sort automatically-uploaded Dropbox photographs and videos into correct | |
| # folders after removing crud like gifs and screenshots: | |
| # | |
| # 1. A dated folder (e.g. 1970-01-01 for an image taken on January, 1970) for | |
| # non-square (non-Instagram) images. | |
| # 2. My dump folder for square Instagram images. | |
| # | |
| # Blame Mark ([email protected]) for this. | |
| # | |
| # Debug | |
| # set -x | |
| uploads_folder=$HOME'/Dropbox/Camera Uploads/' | |
| instagram_folder=$HOME'/Dropbox/Photos/Instagram/' | |
| IFS=' | |
| ' | |
| if [[ $UID == 0 ]]; then | |
| exit 1 | |
| elif [[ ! -d "$uploads_folder" ]] || [[ ! -d "$instagram_folder" ]]; then | |
| exit 2 | |
| fi | |
| function trim_spaces() { | |
| # Safely remove spaces in the file name. | |
| new_name=$(echo "$1" | sed -e 's/ /_/g') | |
| mv "$1" $new_name && echo $new_name | |
| } | |
| function test_aspect_ratio() { | |
| # Test whether the aspect ratio of image $1 == $2:1 | |
| x=$(identify -format '%w' $1) | |
| y=$(identify -format '%h' $1) | |
| if [[ ! $file =~ \.jpg$ ]]; then | |
| echo 1 | |
| return 1 | |
| fi | |
| if [[ $x -ge $y ]]; then | |
| x=$(echo "scale=0; $x * $2" | bc -l) | |
| elif [[ $y -ge $x ]]; then | |
| y=$(echo "scale=0; $y * $2" | bc -l) | |
| fi | |
| if [[ $x -eq $y ]]; then | |
| echo 0 | |
| else | |
| echo 1 | |
| fi | |
| } | |
| function make_dated_directory() { | |
| # Creates a dated folder and moves the image into it. | |
| directory_name=${1:2:10} | |
| if [[ ! -d $directory_name ]]; then | |
| mkdir $directory_name | |
| fi | |
| mv $1 $directory_name | |
| } | |
| # Find (well, shell regex) behaves differently on OS X. See: | |
| # https://stackoverflow.com/questions/5905493/why-isnt-this-regex-working-find-regex-m-h | |
| # https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man7/re_format.7.html#//apple_ref/doc/man/7/re_format | |
| cd "$uploads_folder" | |
| # find . -maxdepth 1 -type f -regex "^.*\(png\|gif\)$" -exec rm {} \; | |
| find . -maxdepth 1 -type f -name ".*png" -o -name "*.gif" -exec rm {} \; | |
| # for file in $(find . -maxdepth 1 -type f -regex '^.*\s.*\(jpg\|mov\)$'); do | |
| for file in $(find . -maxdepth 1 -type f -name "* *.jpg" -o -name "* *.mov"); do | |
| file=$(trim_spaces "$file") | |
| if [[ $(test_aspect_ratio $file 1) -eq 0 ]]; then | |
| mv $file "$instagram_folder" | |
| else | |
| make_dated_directory $file | |
| fi | |
| done | |
| exit 0 |
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 | |
| # https://www.bhalash.com/archives/13544805950 | |
| # December 28, 2015 | |
| # | |
| # Functions | |
| # | |
| is_square() { | |
| # | |
| # Check if Photograph is Square | |
| # | |
| [[ $(identify -format '%w' "$1") == $(identify -format '%h' "$1") ]] | |
| echo $? | |
| } | |
| is_image() { | |
| # | |
| # Check Mimetype | |
| # | |
| grep 'image' <<< $(file --mime-type "$1") > /dev/null 2>&1 | |
| echo $? | |
| } | |
| send_to_dated_directory() { | |
| # | |
| # Create Directory from Date in Filename | |
| # Images imported from Dropbox are named in format "2015-12-27 22.13.26.jpg" | |
| # | |
| 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