resonite-photoexif: complete refactor, recursive now

This commit is contained in:
yosh 2024-02-11 20:58:28 -05:00
parent a6a6080df4
commit 2d2890e650
1 changed files with 53 additions and 59 deletions

View File

@ -6,89 +6,83 @@ set -euf
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
tmp_json="$(mktemp)"
tmp_recuri="$(mktemp)"
tmpfile="$(mktemp)"
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"
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")"
tmp_json="$(mktemp)"
curl -s "$asseturi" | tail -c +10 | brotli -d | bson2json > "$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="$(date -u -d "$(jq -r '.tags[] | select(contains("timestamp:")) |
sub("timestamp:(?<date>.+)";"\(.date)")' \
< "$tmp_recuri")" +'%Y-%m-%d_%H-%M-%S'
)" || timestamp="$(date -u -d "$(jq -r '.creationTime' < "$tmp_recuri")" +'%Y-%m-%d_%H-%M-%S')"
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/1000' < "$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
while read -r argline; do
echo "$argline"
eval "set -- $argline"
outfile="$outdir/$timestamp.webp"
imguri=$1
echo "downloading image $timestamp.webp"
curl -s -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 4
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 after 2nd pass, not saving"
continue
else
outfile="${outfile%.webp}_$(uuidgen).webp"
-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
fi
mv "$tmpfile" "$outfile"
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")
EOF
done
# note: when converting to png then jxl, sometimes tags get fucked up. do this
# alternatively: use vips pngsave instead of imagemagick
# 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