Add in some safety for when there is unexpected issues with jq

This commit is contained in:
Joe Ipson 2024-04-02 07:27:10 -06:00 committed by GitHub
parent a656019893
commit f25b712ff8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,20 +12,32 @@ for FONT in "${FONTS[@]}"; do
FONT="${FONT%"${FONT##*[![:space:]]}"}" # Trim trailing whitespace
mkdir -p "${DEST}/${FONT}"
FILE_REFS=()
readarray -t "FILE_REFS" < <(
curl -s "https://fonts.google.com/download/list?family=${FONT// /%20}" | # spaces are replaced with %20 for the URL
tail -n +2 | # remove first line, which as of March 2024 contains ")]}'" and breaks JSON parsing
jq -c '.manifest.fileRefs[]' # -c option makes output bash parsable
if output=$(curl -s "https://fonts.google.com/download/list?family=${FONT// /%20}" | tail -n +2); then
if FILE_REFS=$(echo "$output" | jq -c '.manifest.fileRefs[]' 2>/dev/null); then
echo "$FILE_REFS"
fi
fi
)
for FILE_REF in "${FILE_REFS[@]}"; do
FILENAME=$(echo "${FILE_REF}" | jq -r '.filename')
URL=$(echo "${FILE_REF}" | jq -r '.url')
echo "Downloading ${FILENAME} from ${URL}"
curl "${URL}" -o "${DEST}/${FONT}/${FILENAME##*/}" # everything before the last / is removed to get the filename
done
if [ ${#FILE_REFS[@]} -eq 0 ]; then
echo "Could not find download information for ${FONT}"
else
for FILE_REF in "${FILE_REFS[@]}"; do
if FILENAME=$(echo "${FILE_REF}" | jq -er '.filename' 2>/dev/null); then
if URL=$(echo "${FILE_REF}" | jq -er '.url' 2>/dev/null); then
echo "Downloading ${FILENAME} from ${URL}"
curl "${URL}" -o "${DEST}/${FONT}/${FILENAME##*/}" # everything before the last / is removed to get the filename
else
echo "Failed to extract URLs for: ${FONT}" >&2
fi
else
echo "Failed to extract filenames for: ${FONT}" >&2
fi
done
fi
done
fc-cache -f "${DEST}"