Skip to content

Instantly share code, notes, and snippets.

@capocasa
Created January 6, 2020 07:24
Show Gist options
  • Save capocasa/6c1073b2d8f2c078f62b7341517d55a9 to your computer and use it in GitHub Desktop.
Save capocasa/6c1073b2d8f2c078f62b7341517d55a9 to your computer and use it in GitHub Desktop.
Dirty bash script to hardlink photos based on exif4
#!/bin/bash
V=true
SCRIPT=$(basename $0)
SRC="/srv/beatrice/iphone11 /srv/beatrice/iPhoneCamera /srv/syncthing/Beatrice_Pictures /srv/syncthing/Camera"
DEST=/srv/sigal/src
find $SRC -type f | while read FILE; do
KEY=$(echo "$FILE" | base32 -w0)
CACHE_DIR=~/.cache/$SCRIPT
CACHE=$CACHE_DIR/$KEY
if [ -e "$CACHE" ]; then
TARGET="$(cat $CACHE)"
TARGET_DIR="$(dirname $TARGET)"
else
export IFS="|"
read -r LAT LON MOD_DATE ORIG_DATE <<<$(exiftool -m -n -p '$GPSLatitude|$GPSLongitude|$FileModifyDate|$DateTimeOriginal' "$FILE")
if [ "" = "$LAT" ]; then
CITY=
TOWN=
LOCALITY=
else
XML=$(curl -s "https://nominatim.openstreetmap.org/reverse?lat=$LAT&lon=$LON")
TOWN=$(echo $XML| grep -oPm1 "(?<=<town>)[^<]+")
CITY=$(echo $XML| grep -oPm1 "(?<=<city>)[^<]+")
LOCALITY=${CITY:-$TOWN}
fi
DATE=${ORIG_DATE:-$MOD_DATE}
DATE=${DATE/ /T}
DATE=${DATE/:/-}
DATE=${DATE/:/-}
DATE=${DATE/+00:00/Z}
EXT=${FILE/*./}
EXT=${EXT,,}
if [ "$LOCALITY" = "" ]; then
LS=
else
LS=_
fi
YEAR=${DATE/-*/}
MONTH=${DATE/-/|}
MONTH=${MONTH/*|/}
MONTH=${MONTH/-*/}
if [ "$MONTH" = "01" ]; then
MONTH_=Januar
elif [ "$MONTH" = "02" ]; then
MONTH_=Februar
elif [ "$MONTH" = "03" ]; then
MONTH_=März
elif [ "$MONTH" = "04" ]; then
MONTH_=April
elif [ "$MONTH" = "05" ]; then
MONTH_=Mai
elif [ "$MONTH" = "06" ]; then
MONTH_=Juni
elif [ "$MONTH" = "07" ]; then
MONTH_=Juli
elif [ "$MONTH" = "08" ]; then
MONTH_=August
elif [ "$MONTH" = "09" ]; then
MONTH_=September
elif [ "$MONTH" = "10" ]; then
MONTH_=Oktober
elif [ "$MONTH" = "11" ]; then
MONTH_=November
elif [ "$MONTH" = "12" ]; then
MONTH_=Dezember
fi
TARGET_DIR="${DEST}/${YEAR}-${MONTH}_${MONTH_}${LS}${LOCALITY}"
TARGET="$TARGET_DIR/${DATE}_${LOCALITY}_$(< /dev/urandom tr -dc 'a-z' | head -c6).$EXT"
mkdir -p $CACHE_DIR
echo "$TARGET" > $CACHE
fi
$V&&echo "$FILE" "$TARGET"
if [ ! -e "$TARGET" ]; then
mkdir -p "$TARGET_DIR"
ln "$FILE" "$TARGET"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment