Skip to content

Instantly share code, notes, and snippets.

@crawford
Created July 18, 2020 03:04
Show Gist options
  • Save crawford/56f1ecdf92cea18f78dc15f58364b998 to your computer and use it in GitHub Desktop.
Save crawford/56f1ecdf92cea18f78dc15f58364b998 to your computer and use it in GitHub Desktop.
Script for combining photos and metadata from Facebook data dumps
#!/usr/bin/env bash
set -euo pipefail
export LANG=C
ensure_valid() {
if [[ -z "${!1}" ]]
then
echo "Variable ${1} is empty"
exit 1
fi
if [[ "${!1}" == "null" ]]
then
echo "Variable ${1} is 'null'"
exit 1
fi
}
for album in $(ls photos_and_videos/album/*.json)
do
name=$(jq -r .name < $album)
ensure_valid name
photos_count=$(($(jq '.photos | length' < $album) - 1))
for i in $(seq 0 $photos_count)
do
filename=$(jq -r ".photos[${i}].uri" < $album)
output="out/${name}/$(basename "${filename}")"
[[ -f "${output}" ]] && continue
description=$(jq -r ".photos[${i}].description" < $album)
timestamp=$(jq -r ".photos[${i}].creation_timestamp" < $album)
comments_json=$(jq -r ".photos[${i}].comments" < $album)
ensure_valid filename
ensure_valid timestamp
if [[ "${description}" == "null" ]]
then
description=
fi
comments=
if [[ "${comments_json}" != "null" ]]
then
comments_count=$(($(jq length <<< $comments_json) - 1))
for i in $(seq 0 $comments_count)
do
ctimestamp=$(jq -r ".[${i}].timestamp" <<< "${comments_json}")
cauthor=$(jq -r ".[${i}].author" <<< "${comments_json}")
ccomment=$(jq -r ".[${i}].comment" <<< "${comments_json}")
comments="${comments}"$'\n'"\"${ccomment}\" -${cauthor} ($(date --date="@${ctimestamp}" '+%F %r'))"
done
fi
exiftool -m -out "${output}" -ImageDescription="${description}${comments}" -CreateDate="$(date --rfc-3339=seconds --date="@${timestamp}")" "${filename}"
done
done
video_count=$(($(jq -r '.videos | length' < photos_and_videos/your_videos.json) - 1))
for i in $(seq 0 $video_count)
do
filename=$(jq -r ".videos[${i}].uri" < photos_and_videos/your_videos.json)
timestamp=$(jq -r ".videos[${i}].creation_timestamp" < photos_and_videos/your_videos.json)
echo $filename
out="out/videos/$(basename "${filename}")"
cp "${filename}" "${out}"
touch --date "$(date --rfc-3339=seconds --date="@${timestamp}")" "${out}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment