Skip to content

Instantly share code, notes, and snippets.

@rothgar
Created July 10, 2023 21:30
Show Gist options
  • Save rothgar/6cb0056fd58a41e1d2bb8d1b5378ab44 to your computer and use it in GitHub Desktop.
Save rothgar/6cb0056fd58a41e1d2bb8d1b5378ab44 to your computer and use it in GitHub Desktop.
Download the og:image from a URL
#!/bin/bash
set -eo pipefail
if [ $# -eq 0 ]; then
echo "Please provide a URL"
echo "$(basename $0) URL"
exit 1
fi
if ! command -v pup > /dev/null
then
echo "pup command is required"
echo "install from https://github.com/ericchiang/pup"
exit 1
elif ! command -v curl > /dev/null
then
echo "curl command is required"
exit 1
fi
download_img() {
curl -s --output "og-image-${FILENAME_INCREMENT}.${IMG_EXT}" "${IMG_URL}"
}
# main loop
FILENAME_INCREMENT=1
for URL in "$@"; do
# get <meta og:image> from page
IMG_URL=$(curl -s "${URL}" \
| pup 'meta[property="og:image"] attr{content}')
IMG_URL="${IMG_URL%\?*}"
IMG_EXT="${IMG_URL##*.}"
download_img
FILENAME_INCREMENT=$((FILENAME_INCREMENT+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment