2024-09-25 15:28:30 +00:00
|
|
|
#!/bin/sh
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
if [ "$#" -lt 2 ] ; then
|
|
|
|
echo "Usage: $0 <base domain> <emoji pack name> [<conditional addition>]" >&2
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
DOMAIN="$1"
|
|
|
|
CATEGORY="$2"
|
|
|
|
ADDCOND="${3:-}"
|
|
|
|
|
|
|
|
OVERWRITE_IMG="${OVERWRITE_IMG:-NO}"
|
|
|
|
OVERWRITE_METAKEYS="${OVERWRITE_METAKEYS:-NO}"
|
|
|
|
|
|
|
|
json_str_escape() {
|
|
|
|
echo "$1" | sed -e 's/"/\\"/g'
|
|
|
|
}
|
|
|
|
|
|
|
|
CACHE_DIR="$(dirname "$0")"/.cache
|
|
|
|
cached_list="$CACHE_DIR"/"$(date -I)_${DOMAIN}"
|
|
|
|
|
|
|
|
if [ ! -e "$cached_list" ] ; then
|
|
|
|
mkdir -p "$CACHE_DIR"
|
|
|
|
curl https://"$DOMAIN"/api/v1/custom_emojis | jq > "$cached_list"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Track additions in "pack.json".files format
|
|
|
|
META_ADD="$(mktemp)"
|
|
|
|
meta_prefix="{"
|
|
|
|
|
|
|
|
# @tsv doesn't actually directly produce TSV, but for each line a string which escapes tabs as \t...
|
|
|
|
cat "$cached_list" \
|
|
|
|
| jq '.[] | select(.category == "'"$CATEGORY"'"'"${ADDCOND:+ }${ADDCOND}"') | [.shortcode, .static_url] | @tsv' \
|
|
|
|
| awk '1 {gsub(/^"|"$/, ""); sub(/\\t/, "\t"); print}' \
|
|
|
|
| while IFS="$(printf '\t')" read -r name url; do
|
|
|
|
urlfile="$(basename "$url")"
|
|
|
|
ext="${urlfile##*.}";
|
|
|
|
if [ "$ext" = "$urlfile" ] ; then
|
|
|
|
ext=""
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$name" != "$(basename "$name")" ] ; then
|
|
|
|
echo "ERROR: unsafe, potentially malicious shortcode '$name'; aborting!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
out="${name}${ext:+.}${ext}"
|
|
|
|
if [ "$OVERWRITE_IMG" = "YES" ] || [ ! -e "$out" ] ; then
|
|
|
|
echo "Downloading $out ..."
|
|
|
|
wget --quiet --output-document="$out" "$url"
|
|
|
|
printf '%s\n\t"%s": "%s"' "$meta_prefix" \
|
|
|
|
"$(json_str_escape "$name")" "$(json_str_escape "$out")" \
|
|
|
|
>> "$META_ADD"
|
|
|
|
meta_prefix=","
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2024-09-26 00:21:58 +00:00
|
|
|
if [ "$(wc -l "$META_ADD" | cut -d ' ' -f 1)" = "0" ] ; then
|
2024-09-25 15:28:30 +00:00
|
|
|
echo "No emoji matched search criteria"
|
|
|
|
rm "$META_ADD"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
printf '\n}\n' >> "$META_ADD"
|
|
|
|
|
|
|
|
cnt_dl="$(jq '. | length' "$META_ADD")"
|
|
|
|
echo "Finished downloading $cnt_dl images."
|
|
|
|
|
|
|
|
if [ ! -e pack.json ] ; then
|
|
|
|
printf '{"files": {}, "files_count": 0, "pack": {"description": "%s: %s%s%s"}}' \
|
|
|
|
"$(json_str_escape "$DOMAIN")" \
|
|
|
|
"$(json_str_escape "$CATEGORY")" \
|
|
|
|
"${ADDCOND:+ }" \
|
|
|
|
"$(json_str_escape "$ADDCOND")" \
|
|
|
|
> pack.json
|
|
|
|
fi
|
|
|
|
|
|
|
|
cnt_old="$(jq '.files | length' pack.json)"
|
|
|
|
echo "Found $cnt_old preëxisting emoji."
|
|
|
|
|
|
|
|
new_files="$(
|
|
|
|
if [ "$OVERWRITE_METAKEYS" = "YES" ] ; then
|
|
|
|
jq --sort-keys -s '.[0].files + .[1]' pack.json "$META_ADD"
|
|
|
|
else
|
|
|
|
jq --sort-keys -s '.[0] + .[1].files' "$META_ADD" pack.json
|
|
|
|
fi
|
|
|
|
)"
|
|
|
|
echo "$new_files" > "$META_ADD"
|
|
|
|
|
|
|
|
cnt_new="$(jq '. | length' "$META_ADD")"
|
|
|
|
cnt_diff=$((cnt_new - cnt_old))
|
|
|
|
echo "After merging there are a total of $cnt_new emoji."
|
|
|
|
if [ "$cnt_diff" != "$cnt_dl" ] ; then
|
|
|
|
echo "WARNING: expected a gain of $cnt_dl but got $cnt_diff additional entries!"
|
|
|
|
echo " (the existing pack may not follow the expected filename scheme)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
jq --argjson new_files "$new_files" \
|
|
|
|
--arg cnt_new "$cnt_new" \
|
|
|
|
'.files = $new_files | .files_count = $cnt_new' \
|
|
|
|
pack.json \
|
|
|
|
> "$META_ADD"
|
|
|
|
|
|
|
|
mv "$META_ADD" pack.json
|