resonite-photoexif: add

This commit is contained in:
yosh 2023-11-07 19:04:04 -05:00
parent 0ee7312993
commit 873ef1fe32
1 changed files with 95 additions and 0 deletions

95
resonite-photoexif Executable file
View File

@ -0,0 +1,95 @@
#!/bin/sh
set -euf
# $@ are resrec urls to things
cleanup() {
[ -f "${tmp_recuri:-}" ] && rm "$tmp_recuri"
[ -f "${tmp_json:-}" ] && rm "$tmp_json"
[ -f "${tmp_json2:-}" ] && rm "$tmp_json2"
[ -f "${tmpfile:-}" ] && rm "$tmpfile"
true
}
trap 'cleanup' INT HUP QUIT EXIT
outdir="$HOME/pics/screenshots/Resonite"
for resrec; do
cleanup
resrec="${resrec#resrec:///}"
recuri="https://api.resonite.com/users/${resrec%%/*}/records/${resrec##*/}"
tmp_recuri="$(mktemp)"
curl -s -o "$tmp_recuri" "$recuri"
# asset uri stuff
asseturi="$(jq -r '.assetUri | sub("resdb:///(?<x>[^.]+).*";"https://assets.resonite.com/\(.x)")' < "$tmp_recuri")"
tmp_json="$(mktemp)"
curl -s "$asseturi" | tail -c +10 | brotli -d | bson2 print > "$tmp_json"
# save the image itself
imguri="$(jq -r '.Object.Children[0].Components.Data.[] |
select(.Type=="FrooxEngine.StaticTexture2D").Data.URL.Data |
sub("@resdb:///(?<x>[^.]+).*";"https://assets.resonite.com/\(.x)")' \
< "$tmp_json"
)"
timestamp="$(jq -r '.tags[] | select(contains("timestamp:")) |
sub("timestamp:(?<d>[^T]+)T(?<h>[^:]+):(?<m>[^:]+):(?<s>[^.]+).*";"\(.d)_\(.h)-\(.m)-\(.s)")' \
< "$tmp_recuri"
)"
[ -z "$timestamp" ] && timestamp="$(jq -r '.creationTime | sub("$(?<d>[^T]+)T(?<h>[^:]+):(?>,>[^:]+):(?<s>[^.]+).*";"\(.d)_\(.h)-\(.m)-\(.s)")' < "$tmp_recuri")"
echo "downloading image $timestamp.webp"
tmpfile="$(mktemp)"
curl -s -o "$tmpfile" "$imguri"
outfile="$outdir/$timestamp.webp"
if [ -f "$outfile" ]; then
if cmp -s "$tmpfile" "$outfile"; then
echo "Duplicate file, not saving"
continue
else
echo "WARNING: filename exists in 1st pass, making unique name as safeguard"
outfile="${outfile%.webp}_$(uuidgen).webp"
fi
fi
# check if photometadata even exists
if [ "$(jq '.TypeVersions."FrooxEngine.PhotoMetadata"' < "$tmp_json")" = "null" ]; then
echo "no photometadata"
else
# if it does, we're in business
tmp_json2="$(mktemp)"
jq '.Object.Children[0].Components.Data.[] | select(.Type=="FrooxEngine.PhotoMetadata")' "$tmp_json" > "$tmp_json2"
meta_time="$(date -u -d "@$(jq -r '.Data.TimeTaken.Data | sub("{\\$date: (?<x>[0-9]+)[0-9][0-9][0-9]}"; "\(.x)")' < "$tmp_json2")" +'%Y:%m:%d %H:%M:%S')"
meta_host="$(jq -r '.Data.LocationHost._userId.Data' < "$tmp_json2")"
meta_place="$(jq -r '.Data.LocationName.Data | gsub("<[^<]+>";"")' < "$tmp_json2")"
meta_photographer="$(jq -r '.Data.TakenBy._userId.Data' < "$tmp_json2")"
meta_presentusers="$(jq -r '[.Data.UserInfos.Data[].User._userId.Data] | @tsv | gsub("\t";", ")' < "$tmp_json2")"
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 after 2nd pass, not saving"
continue
else
outfile="${outfile%.webp}_$(uuidgen).webp"
fi
fi
fi
mv "$tmpfile" "$outfile"
done
# note: when converting to png then jxl, sometimes tags get fucked up. do this
# for i in *.jxl; do exiftool -tagsfromfile "${i%.jxl}.webp" -usercomment -datetimeoriginal -m "$i"; done