-
Notifications
You must be signed in to change notification settings - Fork 1
/
otto.sh
executable file
·339 lines (313 loc) · 10.8 KB
/
otto.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env bash
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
# Author: Dmitri Popov, dmpop@cameracode.coffee
# Source code: https://github.com/dmpop/otto
# Check whether the required packages are installed
if [ ! -x "$(command -v dialog)" ] || [ ! -x "$(command -v getopt)" ] ||
[ ! -x "$(command -v bc)" ] || [ ! -x "$(command -v jq)" ] ||
[ ! -x "$(command -v curl)" ] || [ ! -x "$(command -v exiftool)" ] ||
[ ! -x "$(command -v exiv2)" ] || [ ! -x "$(command -v rsync)" ] ||
[ ! -x "$(command -v wget)" ] || [ ! -x "$(command -v gpsbabel)" ]; then
echo "Make sure that the following tools are installed on your system:"
echo "dialog"
echo "getopt"
echo "bc"
echo "jq"
echo "curl"
echo "exiftool"
echo "exiv2"
echo "wget"
echo "rsync"
echo "gpsbabel"
exit 1
fi
# Usage prompt
usage() {
cat <<EOF
$0 [OPTIONS]
------
$0 transfers, geotags, adds metadata, organizes photos and RAW files, generates EXIF-based stats.
EXAMPLES:
------
$0 -d <dir> -b
$0 -d <dir> -u
$0 -d <dir> -g <location> -t "This is text" -k "keyword1 keyword2 keyword3"
$0 -d <dir> -c <dir>
$0 -d <dir> -l "34.704364,135.501887,161"
$0 -d <dir> -g <location> -r NEF
$0 -d <dir> -s <EXIF tag>
OPTIONS:
--------
-d Specifies the source directory
-g Geotag using coordinates of the specified location (city)
-l Geotag using the exact geographical coordinates in the "lat,lon,alt" format
-c path to a directory containing one or several GPX files
-b Perform backup only
-u Perform backup to an individual directory named after the current date
-r Transfer RAW files in the specified format only
-t Write the specified text into the UserComment field of EXIF metadata
-k Write the specified keywords into EXIF metadata
-e Generate stats for the given EXIF tag
EOF
exit 1
}
# Notification function
function notify() {
if [ ! -z "$NTFY_TOPIC" ]; then
curl \
-d "I'm done. Have a nice day!" \
-H "Title: Message from Otto" \
-H "Tags: monkey" \
"$NTFY_SERVER/$NTFY_TOPIC" >/dev/null 2>&1
fi
dialog --erase-on-exit --title "OTTO" --msgbox " ~\n o{°_°}o\n /(.)~[*O]\n / \\\n--------------------------\nAll done! Have a nice day.\n--------------------------" 11 30
}
CONFIG="$HOME/.otto.cfg"
# Obtain parameter values
while getopts "d:g:l:c:but:r:k:s:" opt; do
case ${opt} in
d)
src=$OPTARG
;;
g)
location=$OPTARG
;;
l)
latlonalt=$OPTARG
;;
c)
gpx=$OPTARG
;;
b)
backup=1
;;
u)
currd=1
;;
t)
text=$OPTARG
;;
r)
raw=$OPTARG
;;
k)
keywords=$OPTARG
;;
s)
exif_tag=$OPTARG
;;
\?)
usage
;;
esac
done
shift $((OPTIND - 1))
yyyy=$(date +%Y)
# Ask for the required info and write the obtained values into the configuration file
if [ ! -f "$CONFIG" ]; then
dialog --erase-on-exit --title "Otto configuration" \
--form "\n Specify the required settings" 13 65 5 \
" Destination:" 1 4 "$HOME/OTTO" 1 23 35 512 \
" Author:" 2 4 "Full Name" 2 23 35 512 \
" ntfy server:" 3 4 "ntfy.sh" 3 23 35 512 \
" ntfy topic:" 4 4 "unique-string" 4 23 35 512 \
" Note URL:" 5 4 "https://hello.xyz/path/to/notes" 5 23 35 1024 \
>/tmp/dialog.tmp \
2>&1 >/dev/tty
if [ -s "/tmp/dialog.tmp" ]; then
destination=$(sed -n 1p /tmp/dialog.tmp)
author=$(sed -n 2p /tmp/dialog.tmp)
ntfy_server=$(sed -n 3p /tmp/dialog.tmp)
ntfy_topic=$(sed -n 4p /tmp/dialog.tmp)
note_url=$(sed -n 5p /tmp/dialog.tmp)
echo "DESTINATION=\"$destination\"" >>"$CONFIG"
echo "AUTHOR=\"$author\"" >>"$CONFIG"
echo "NTFY_SERVER=\"$ntfy_server\"" >>"$CONFIG"
echo "NTFY_TOPIC=\"$ntfy_topic\"" >>"$CONFIG"
echo "NOTE_URL=\"$note_url\"" >>"$CONFIG"
echo "DATE_FORMAT=\"%Y%m%d-%H%M%S%%-c.%%e\"" >>"$CONFIG"
rm -f /tmp/dialog.tmp
else
exit 1
fi
fi
source "$CONFIG"
# Check whether the path to the source directory is specified
if [ -z "$src" ]; then
usage
exit 1
fi
# Check whether the source directory exists
if [ ! -d "$src" ]; then
dialog --erase-on-exit --backtitle "ERROR" --msgbox "Source directory not found." 5 31
exit 1
fi
if [ -z '$(ls -A "'$src'")' ]; then
dialog --erase-on-exit --backtitle "ERROR" --msgbox "Source directory is empty." 5 31
exit 1
fi
if [ -f "/tmp/otto.log" ]; then
rm "/tmp/otto.log"
fi
# Create the destionation directory
srcdir=$(basename "$src")
ENDPOINT="$DESTINATION/$srcdir"
mkdir -p "$ENDPOINT"
# If -s parameter specified, generate stats for the given EXIF tag
if [ ! -z "$exif_tag" ]; then
dialog --infobox "Generating $exif_tag stats..." 3 41
results=$(mktemp)
exiftool -r -T "-$exif_tag" "$src" >"$results"
if [ -f "$HOME/$exif_tag.csv" ]; then
rm "$HOME/$exif_tag.csv"
fi
echo "$exif_tag, Count," >>"$HOME/$exif_tag.csv"
sort "$results" | uniq -c | awk '{print $0 ", " $1 ","}' | awk '{$1=""; print $0}' | awk '{$1=$1;print}' >>"$HOME/$exif_tag.csv"
clear
notify
exit 1
fi
# If -b parameter specified, perform a simple backup
if [ ! -z "$backup" ]; then
dialog --infobox "Transferring files..." 3 26
rsync -avh "$src/" "$ENDPOINT" >>"/tmp/otto.log" 2>&1
clear
notify
exit 1
fi
# If -u parameter specified, perform a simple backup
# to a dedicated directory named after the current date
if [ ! -z "$currd" ]; then
d=$(date -d "today" +"%Y-%m-%d")
dialog --infobox "Transferring files..." 3 26
rsync -avh "$src/" "$ENDPOINT/$d" >>"/tmp/otto.log" 2>&1
clear
notify
exit 1
fi
dialog --infobox "Transferring and renaming files..." 3 39
cd "$src"
if [ ! -z "$raw" ]; then
exiftool -q -q -m -r -o "$ENDPOINT" -d "$DATE_FORMAT" '-FileName<DateTimeOriginal' -ext $raw . >>"/tmp/otto.log" 2>&1
else
exiftool -q -q -m -r -o "$ENDPOINT" -d "$DATE_FORMAT" '-FileName<DateTimeOriginal' . >>"/tmp/otto.log" 2>&1
fi
dialog --infobox "Writing EXIF metadata..." 3 28
cd "$ENDPOINT"
# Obtain and write copyright camera model, lens, and note
for file in *.*; do
date=$(exiftool -q -q -m -DateTimeOriginal -d %Y-%m-%d "$file" | cut -d":" -f2 | tr -d " ")
wf=$date".txt"
if [ ! -z "$text" ]; then
note="$text"
elif [ ! -z "$NOTE_URL" ]; then
wget "$NOTE_URL/$wf" -O "/tmp/$wf" >>"/tmp/otto.log" 2>&1
if [ -f "/tmp/$wf" ]; then
note=$(<"/tmp/$wf")
fi
else
note=""
fi
camera=$(exiftool -Model "$file" | cut -d":" -f2 | tr -d " ")
lens=$(exiftool -q -q -m -LensID "$file" | cut -d":" -f2)
if [ -z "$lens" ]; then
lens=$(exiftool -q -q -m -LensModel "$file" | cut -d":" -f2)
fi
exiv2 --Modify "set Exif.Image.ImageDescription $camera $lens $note" "$file" >>"/tmp/otto.log" 2>&1
exiv2 --Modify "set Xmp.exif.UserComment $camera $lens $note" "$file" >>"/tmp/otto.log" 2>&1
exiv2 --Modify "set Exif.Image.Copyright $yyyy $AUTHOR" "$file" >>"/tmp/otto.log" 2>&1
# Check whether keywords are specified
if [ ! -z "$keywords" ]; then
keywords_arr=($(echo "$keywords" | tr ', ' '\n'))
for k in ${keywords_arr[@]}; do
exiv2 --Modify "set Xmp.dc.subject $k" "$file" >>"/tmp/otto.log" 2>&1
done
fi
done
# Geotag if $latlon is not empty
if [ ! -z "$latlonalt" ]; then
IFS=,
read lat lon alt <<<$latlonalt
if (($(echo "$lat > 0" | bc -l))); then
latref="N"
else
latref="S"
fi
if (($(echo "$lon > 0" | bc -l))); then
lonref="E"
else
lonref="W"
fi
dialog --infobox "Geotagging..." 3 17
exiftool -q -q -m -overwrite_original -GPSLatitude=$lat -GPSLatitudeRef=$latref -GPSLongitude=$lon -GPSLongitudeRef=$lonref -GPSAltitude=$alt . >>"/tmp/otto.log" 2>&1
fi
# Geotag files if $location is not empty
if [ ! -z "$location" ]; then
# Check whether the Photon service is reachable
check=$(curl -Is https://photon.komoot.io/ | head -n 1)
if [ -z "$check" ]; then
dialog --erase-on-exit --backtitle "ERROR" --msgbox "Photon is not reachable. Geotagging skipped." 6 28
else
# Obtain latitude and longitude for the specified location
geo="$(curl -k "https://photon.komoot.io/api/?q=$location")"
lat=$(echo "$geo" | jq '.features | .[0] | .geometry | .coordinates | .[1]')
lon=$(echo "$geo" | jq '.features | .[0] | .geometry | .coordinates | .[0]')
if (($(echo "$lat > 0" | bc -l))); then
latref="N"
else
latref="S"
fi
if (($(echo "$lon > 0" | bc -l))); then
lonref="E"
else
lonref="W"
fi
dialog --infobox "Geotagging..." 3 17
exiftool -q -q -m -overwrite_original -GPSLatitude=$lat -GPSLatitudeRef=$latref -GPSLongitude=$lon -GPSLongitudeRef=$lonref . >>"/tmp/otto.log" 2>&1
fi
fi
if [ ! -z "$gpx" ]; then
# Count GPX files in the specified directory
cd "$gpx"
fcount=$(ls -1 | wc -l)
# Check for GPX files and GPSBabel
if [ "$fcount" -eq "0" ]; then
dialog --erase-on-exit --backtitle "ERROR" --msgbox "No GPX files found." 5 23
exit 1
fi
# Geocorrelate with a single GPX file
if [ "$fcount" -eq "1" ]; then
dialog --infobox "Geocorrelating..." 3 21
track=$(ls "$gpx")
exiftool -q -q -m -overwrite_original -geotag "$track" -geosync=180 "$ENDPOINT" >>"/tmp/otto.log" 2>&1
elif [ "$fcount" -gt "1" ]; then
ff=""
for f in *.gpx; do
ff="$ff -f $f"
done
gpsbabel -i gpx $ff -o gpx -F "/tmp/merged-track.gpx"
track="/tmp/merged-track.gpx"
exiftool -q -q -m -overwrite_original -geotag "$track" -geosync=180 "$ENDPOINT" >>"/tmp/otto.log" 2>&1
else
dialog --erase-on-exit --backtitle "ERROR" --msgbox "Something went wrong. Geotagging skipped." 6 25
fi
fi
dialog --infobox "Organizing files..." 3 23
cd "$ENDPOINT"
exiftool -q -q -m '-Directory<CreateDate' -d ./%Y-%m-%d . >>"/tmp/otto.log" 2>&1
cd
clear
notify