misc-scripts/resonite-photoexif

86 lines
3.1 KiB
Bash
Executable File

#!/bin/sh
set -euf
NL="
"
cleanup() {
trap 'exit' INT HUP QUIT TERM EXIT
[ -f "${tmp_recuri:-}" ] && rm "$tmp_recuri"
[ -f "${tmp_json:-}" ] && rm "$tmp_json"
[ -f "${tmpfile:-}" ] && rm "$tmpfile"
}
trap 'cleanup' INT HUP QUIT TERM EXIT
tmp_json="$(mktemp)"
tmp_recuri="$(mktemp)"
tmpfile="$(mktemp)"
outdir="$HOME/pics/screenshots/Resonite"
if [ -z "$*" ]; then # default folder thingy
read -r folder < "$outdir/secret.txt" # contains the public folder 4 screenshots
user=$(printf '%s' "$folder" | awk -v RS='/' '$0 ~ /^U-/')
# get all screenshot record ids
# these never contain a space so like, lol
set -- $(curl "$folder" | jq -r '.[].id')
opt_folder=1
fi
for record; do
if [ -z "${opt_folder:=}" ]; then
record=${record#resrec:///}
user=${record%%/*}
record=${record##*/}
fi
recuri="https://api.resonite.com/users/$user/records/$record"
curl -s -o "$tmp_recuri" "$recuri"
timestamp="$(date -u -d "$(jq -r '.creationTime' < "$tmp_recuri")" +'%Y-%m-%d_%H-%M-%S')"
# asset uri stuff
asseturi="$(jq -r '.assetUri | sub("resdb:///(?<x>[^.]+).*";"https://assets.resonite.com/\(.x)")' < "$tmp_recuri")"
curl -s "$asseturi" | tail -c +10 | brotli -d | bson2json > "$tmp_json"
while read -r argline; do
exifdata="" exifdate=""
eval "set -- $argline"
outfile="$outdir/$timestamp.webp"
imguri=$1; shift
echo "downloading image $timestamp.webp"
aria2c -q -d / --allow-overwrite=true --auto-file-renaming=false -o "$tmpfile" "$imguri"
[ -n "$1" ] && exifdata="$exifdata${NL}World: $1"; shift
[ -n "$1" ] && exifdata="$exifdata${NL}Host: $1"; shift
[ -n "$1" ] && exifdata="$exifdata${NL}Photographer: $1"; shift
[ -n "$1" ] && exifdate="$(date -u -d "@$1" +'%Y:%m:%d %H:%M:%S')"; shift
[ -n "$1" ] && exifdata="$exifdata${NL}Present Users: $(printf '%s' "$*" | sed 's/ /, /g')"
exiftool -overwrite_original -UserComment="${exifdata#$NL}" -datetimeoriginal="$exifdate" -- \
"$tmpfile"
outfile="$outdir/$(printf '%s' "${exifdate:-${timestamp}}" | sed 's/:/-/g ; s/ /_/g').webp"
if [ -f "$outfile" ]; then
if cmp -s "$tmpfile" "$outfile"; then
echo "WARNING: Duplicate file, not saving"
continue
else
outfile="${outfile%.webp}_$(uuidgen).webp"
fi
fi
mv "$tmpfile" "$outfile"
done <<-EOF
$(jq -r '.Object | .. | select(.[].Type=="FrooxEngine.StaticTexture2D")? |
[ .[] |
select(.Type=="FrooxEngine.StaticTexture2D"),
select(.Type=="FrooxEngine.PhotoMetadata")
] | [
( .[0].Data.URL.Data | sub("@(resdb:///(?<x>[^.]+))?.*";"https://assets.resonite.com/\(.x)") ),
try (.[1].Data.LocationName.Data | gsub("\\n"; " ")) catch "",
.[1].Data.LocationHost._userId.Data // "",
.[1].Data.TakenBy._userId.Data // "",
try (.[1].Data.TimeTaken.Data/1000) catch "",
try (.[1].Data.UserInfos.Data[].User._userId.Data) catch ""
] | @sh' < "$tmp_json")
EOF
done
# note: when converting to png then jxl, sometimes tags get fucked up. do this
# alternatively: use vips pngsave instead of imagemagick, since that preserves tags
# for i in *.jxl; do exiftool -tagsfromfile "${i%.jxl}.webp" -usercomment -datetimeoriginal -m "$i"; done