diff --git a/scripts/get_emojis_mastodon.sh b/scripts/get_emojis_mastodon.sh new file mode 100755 index 0000000..0a37825 --- /dev/null +++ b/scripts/get_emojis_mastodon.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +set -eu + +# Get mastodon instance from MASTODON_INSTANCE +if [ -z ${MASTODON_INSTANCE+x} ]; then + # set from $1 if available + if [ -z ${1+x} ]; then + echo "You need to provide a Mastodon instance." + exit 1 + else + mastodon_instance="$1" + fi +else + mastodon_instance="$MASTODON_INSTANCE" +fi + +echo "Mastodon instance: $mastodon_instance" + +# Get emoji folder +emojiFolder="$(readlink -f "$(dirname $0)/../frontend/emojis/")" + +echo "Downloading emojis to $emojiFolder" + +echo "Creating $emojiFolder if it doesn't exist" + +mkdir -p "$emojiFolder" + +# load $MASTODON_INSTANCE/api/v1/custom_emojis with curl into a variable + +customEmojisJson=$(curl -s "$MASTODON_INSTANCE/api/v1/custom_emojis") + + +shortcodes=$(echo "$customEmojisJson" | jq -r '.[].shortcode') +readarray -t shortcodesArray <<< "$shortcodes" +urls=$(echo "$customEmojisJson" | jq -r '.[].url') +readarray -t urlsArray <<< "$urls" +visible_in_pickers=$(echo "$customEmojisJson" | jq -r '.[].visible_in_picker') +readarray -t visible_in_pickersArray <<< "$visible_in_pickers" + +# read through the urls and download the emojis +for ((i=0; i<${#shortcodesArray[@]}; i++)); do + # if not visible_in_pickers, continue + if [ "${visible_in_pickersArray[$i]}" != "true" ]; then + echo "Skipping ${shortcodesArray[$i]} because it is not visible in the picker" + continue + fi + url="${urlsArray[$i]}" + # get only extension from the url + fileext=$(echo "$url" | sed 's/.*\///' | sed 's/.*\.//') + # filename is shortcode + extension + filename="${shortcodesArray[$i]}.$fileext" + # download the emoji + # if file already exists, skip + if [ -f "$emojiFolder/$filename" ]; then + echo "Skipping $filename because it already exists" + continue + fi + echo "Downloading $filename" + curl -s "$url" -o "$emojiFolder/$filename" +done \ No newline at end of file