Created
July 10, 2023 21:30
-
-
Save rothgar/6cb0056fd58a41e1d2bb8d1b5378ab44 to your computer and use it in GitHub Desktop.
Download the og:image from a URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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