Skip to content

Instantly share code, notes, and snippets.

@Siphonay
Last active August 27, 2018 19:30
Show Gist options
  • Save Siphonay/c6e42bde23dced5b34437f5f01ea6e20 to your computer and use it in GitHub Desktop.
Save Siphonay/c6e42bde23dced5b34437f5f01ea6e20 to your computer and use it in GitHub Desktop.
Updates Mutant Standard Emoji on a non-Dockerized Mastodon instance. If you use this, you'll need 'wget' and 'unzip'.
#!/bin/bash
_name='MastoMutant Emoji Import'
_author='https://monsterpit.net/@daggertooth'
_version=2018.05.23.1
### PATHS! ###
# Where to put the Mutant Standard archives and extracted files.
mutant_dir="$HOME/mutant"
# Extra custom emojis to add.
extra_dir="$HOME/extra_emoji"
# Mastodon directory, so we can use the Rails console.
mastodon_live="$HOME/live"
### MAIN! ###
# A temp file to dump our generated Rails console script to.
temp=$(mktemp -t mastomutant.XXXXXXXX)
# Make sure our working directories actually exists.
mkdir -p "$mutant_dir/tmp"
# Make sure we have everything we need installed.
echo "Making sure you have 'wget' and 'unzip'..."
if ! type -p wget &>/dev/null; then
echo " You need to install 'wget'."
exit 1
elif ! type -p unzip &>/dev/null; then
echo " You need to install 'unzip'."
exit 1
else
echo "...oh, good, you do."
fi
# Make sure the Mastodon deployment actually exists.
if [[ ! -f "$mastodon_live/.env.production" ]]; then
echo
echo "I need to know the path to your Mastodon deployment."
echo "(Your doesn't seem to live at '$mastodon_live'.)"
read -ep '> ' mastodon_live
[[ -z "$mastodon_live" ]] && exit 2
fi
# Fetch the URL of the latest Mutant Standard PNG archive using forbidden voodoo.
latest_version="https://mutant.tech$(wget -qO- 'https://mutant.tech/use' | sed -rn 's#^.*href=.(/dl/[0-9.]+/mutstd_\S+shortcode_png64\.zip)\b.*#\1#p' | head -1)"
latest_special='https://mutant.tech/dl/special/mutstd_special_s1_all.zip'
# If we don't already have it saved, download it.
dl_path="$mutant_dir/${latest_version##*/}"
sp_path="$mutant_dir/special.zip"
if [[ ! -f "$dl_path" ]]; then
wget -O "$dl_path" "$latest_version"
wget -O "$sp_path" "$latest_special"
fi
# Unzip the archive.
unzip -ud "$mutant_dir/tmp" "$dl_path"
unzip -ud "$mutant_dir/tmp" "$sp_path"
# Initialize!
echo > "$temp"
mkdir -p "$extra_dir"
# Find the roots of the emojos.
while IFS=$'\n' read -ru3 mdir; do
# Replaces brackets, dashes, and spaces in image names.
# This is slow and terrible, and I should feel bad.
while IFS=$'\t' read -ru4 p f; do
# Strip brackets.
nf=${f//[\[\]]/}
# Replace hypens and spaces with undescores.
nf=${nf//[- ]/_}
# Rename if the path actually changed.
[[ "$p/$f" != "$p/$nf" ]] && mv "$p/$f" "$p/$nf"
done 4< <(find "$mdir" -type f -name '*.png' -printf '%h\t%f\n')
# "Wow, Daggertooth, that's pretty fugly," you're probably thinking.
# Oh, but you've seen nothing yet! Get a load of this:
# Generate Ruby code to update the emojos in Mastodon.
find "$mdir" -type f -name '*.png' -printf '%p\t%f\n' | awk -F'\t' '
{
sub(/\.png$/, "", $2)
print "shortcode = \""tolower($2)"\""
print "old_shortcode = \""tolower($2)"\""
print "image = File.open(Pathname(\""$1"\"))"
print "emoji = CustomEmoji.find_by(domain: nil, shortcode: old_shortcode)"
print "if not emoji.nil? and not emoji.disabled? then"
print " emoji.image = image"
print " emoji.visible_in_picker = false"
print " emoji.disabled = false"
print " emoji.save!"
print "end"
print "emoji = CustomEmoji.find_by(domain: nil, shortcode: shortcode)"
print "if emoji.nil? then"
print " emoji = CustomEmoji.new(domain: nil, shortcode: shortcode, image: image)"
print "else"
print " emoji.image = image"
print "end"
print "emoji.save!"
}' >> "$temp"
done 3< <(find $extra_dir -print -type d)
echo quit >> "$temp"
# "Let me get this straight: you've written a Bash script that uses an Awk script to generate a Ruby script."
# Yes.
# "Last version ago, you said you were going to write this script in Rub--"
# Hush, you.
# _________________________
# _/ \_
# | A HORROR-TERROR |
# |_ ONCE LIVED HERE _|
# \_________________________/
#
# 2017.09.21.1 ~ 2017.12.23.1
# Set up the Mastodon environment.
cd "$mastodon_live"
eval "$(rbenv init -)"
export RAILS_ENV=production
# Tell our fluffy friend the good news in the Rails console.
less "$temp"
bundle exec rails c < "$temp"
# Clean up after ourselves.
rm -r "$mutant_dir/tmp" "$temp"
# "Daggertooth, you're (still) banned from writing shell script."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment