misc-scripts/resonite-photoexif

95 lines
3.1 KiB
Bash
Executable File

#!/bin/sh
set -euf
# $@ are resrec urls to things
cleanup() {
[ -f "${tmp_recuri:-}" ] && rm "$tmp_recuri"
[ -f "${tmp_json:-}" ] && rm "$tmp_json"
[ -f "${tmpfile:-}" ] && rm "$tmpfile"
true
}
trap 'cleanup' INT HUP QUIT EXIT
tmp_json="$(mktemp)"
tmp_recuri="$(mktemp)"
tmpfile="$(mktemp)"
outdir="$HOME/pics/screenshots/Resonite"
for resrec; do
resrec="${resrec#resrec:///}"
recuri="https://api.resonite.com/users/${resrec%%/*}/records/${resrec##*/}"
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
eval "set -- $argline"
outfile="$outdir/$timestamp.webp"
imguri=$1
echo "downloading image $timestamp.webp"
aria2c -q -d / --allow-overwrite=true --auto-file-renaming=false -o "$tmpfile" "$imguri"
if [ $# -gt 1 ]; then
meta_place=$2
meta_host=$3
meta_photographer=$4
meta_time=$(date -u -d "@$5" +'%Y:%m:%d %H:%M:%S')
shift 5
meta_presentusers=$(sed 's/ /, /g' <<-EOF
$*
EOF
)
exiftool -overwrite_original -UserComment="World: $meta_place
Host: $meta_host
Photographer: $meta_photographer
Present Users: $meta_presentusers" "$tmpfile" \
-datetimeoriginal="$meta_time"
# outfile will be in yyyy-mm-dd_hh-mm-ss, UTC TIME!!!
outfile="$outdir/$(printf '%s' "$meta_time" | sed -e 's/:/-/g' -e '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
else
echo "image "$imguri" doesn't have metadata"
fi
mv "$tmpfile" "$outfile"
done <<-EOF
$(jq -r '.Object.Children | .. | objects | .Components.Data | select(. != null)
| [ .[]
| ( select(.Type=="FrooxEngine.StaticTexture2D").Data.URL.Data
| sub("@(resdb:///(?<x>[^.]+))?.*";"https://assets.resonite.com/\(.x)")
),
( select(.Type=="FrooxEngine.PhotoMetadata").Data.LocationName.Data
| gsub("\\n"; " ")
),
select(.Type=="FrooxEngine.PhotoMetadata").Data.LocationHost._userId.Data,
select(.Type=="FrooxEngine.PhotoMetadata").Data.TakenBy._userId.Data,
select(.Type=="FrooxEngine.PhotoMetadata").Data.TimeTaken.Data/1000,
select(.Type=="FrooxEngine.PhotoMetadata").Data.UserInfos.Data[].User._userId.Data
]
| select(. != []) | @sh' < "$tmp_json" \
|| jq -r '.Object.Children | .. | objects | .Components.Data | select(. != null)
| [ .[]
| ( select(.Type=="FrooxEngine.StaticTexture2D").Data.URL.Data
| sub("@(resdb:///(?<x>[^.]+))?.*";"https://assets.resonite.com/\(.x)")
)
]
| select(. != []) | @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