sticker-tools/stickers.sh

410 lines
11 KiB
Bash
Executable File

#!/bin/bash
function func_exists () {
if [ -n "$(type -t mode_$1)" ] && [ "$(type -t mode_$1)" == "function" ] ; then
return 0
fi
return 1
}
function get_config () {
config=$1
if [ ! -n "$config" ] ; then
config="default.json"
if [ -f config.json ] ; then
config="config.json"
fi
fi
echo $config
}
function check_dep () {
echo -n "checking for $1 "
result=$(which "$1" | grep -ci "not found")
if [ "$result" == "0" ] ; then
echo "[ OK ]"
else
echo "[MISS]"
fi
}
function append_json () {
mkdir -p build
cp $1 build/temp.json
jq -c "$2" build/temp.json > $1
rm build/temp.json
}
function check_pleroma () {
staticDir=$(jq -r '.pleroma.static' $config)
if [ ! -d "$staticDir" ] ; then
echo "pleroma not found in '$staticDir'"
echo "check if pleroma is installed and .pleroma.static hints to the correct directory"
exit
fi
if [ ! -d "$staticDir/static/stickers/" ] || [ ! -f "$staticDir/static/stickers.json" ] ; then
echo "pleroma isn't ready for stickers yet"
echo "use './stickers.sh prepare' to prepare it"
exit
fi
}
function mode_help () {
command=$1
if [ ! -z $command ] ; then
shift
fi
if func_exists help_$command ; then
mode_help_$command $@
else
echo "stickers.sh command options"
echo "does operations on sticker stuff for pleroma-fe"
echo "commands:"
echo " help [command]"
echo " check [section]"
echo " prepare"
echo " grab [source] [options]"
echo " build [source] [name] [artist]"
echo " install [pack]..."
echo " recreate"
fi
}
function mode_help_check () {
echo "./stickers.sh check [SECTION]"
echo "does various checks"
echo "SECTIONS:"
echo " deps : checks dependencies for this script"
}
function mode_check () {
command=$1
shift
if func_exists check_$command ; then
mode_check_$command $@
else
echo "unknown section $command"
mode_help_check
fi
}
function mode_help_check_deps () {
echo "./stickers.sh check deps"
echo "checks dependencies this script uses"
}
function mode_check_deps () {
check_dep "convert"
check_dep "ffmpeg"
check_dep "jq"
check_dep "recode"
check_dep "wget"
check_dep "dwebp"
}
function mode_help_prepare () {
echo "./stickers.sh prepare"
echo "prepares your pleroma installation for stickers"
}
function mode_prepare () {
staticDir=$(jq -r '.pleroma.static' $config)
if [ ! -d "$staticDir" ] ; then
echo "pleroma not found in $staticDir"
echo "check if pleroma is installed and .pleroma.static hints to the correct directory"
exit
fi
echo -n "preparing pleroma..."
mkdir -p "$staticDir/static/stickers/"
if [ ! -f "$staticDir/static/stickers.json" ] ; then
echo "{}" > "$staticDir/static/stickers.json"
fi
chown -R pleroma:pleroma "$staticDir/static/stickers.json" "$staticDir/static/stickers/"
echo "done"
}
function mode_help_build () {
echo "./stickers.sh build [PATH_TO_IMAGES]"
echo "creates a new sticker pack and places it into the build folder"
echo "options:"
echo " PATH_TO_IMAGES : path where the images for the pack is located"
}
function mode_build () {
path=$1
if [ ! -n "$path" ] || [ ! -d "$path" ] ; then
echo "invalid path $path"
exit
fi
fileCount=$(ls -l "$path" | wc -l)
echo "$fileCount files found"
name=""
author=""
if [ -f "$path/productInfo.meta" ] ; then
echo "LINE Stickerpack detected"
name=$(jq -r '.title.en' "$path/productInfo.meta")
author=$(jq -r '.author.en' "$path/productInfo.meta")
fi
echo -n "Name [$name]:"
read newName
echo -n "Author [$author]:"
read newAuthor
if [ -n "$newName" ] ; then
name="$newName"
fi
if [ -n "$newAuthor" ] ; then
author="$newAuthor"
fi
id=$(echo "${name,,}" | sed 's/[ :;]/_/ig')
releasePath=$(jq -r '.workspace.release' $config)
echo "writing pack..."
mkdir -p "$releasePath/$id/"
echo "{}" > "$releasePath/$id/pack.json"
append_json "$releasePath/$id/pack.json" ".\"author\" = \"$author\""
append_json "$releasePath/$id/pack.json" ".\"title\" = \"$name\""
append_json "$releasePath/$id/pack.json" ".\"stickers\" = []"
index=0
for file in "$path"/*.webp ; do
filename=$(basename "$file")
echo "converting $file..."
output=$(echo "$filename" | sed 's/\.webp$/.png/g')
dwebp "$file" -o "$path/$output"
rm -Rf "$file"
done
for file in "$path"/*.png ; do
isLineKey=$(echo "$file" | grep -P '_key|tab_' | wc -l)
if [ "$isLineKey" -eq "0" ] ; then
filename=$(basename "$file")
echo "converting $filename..."
cp "$file" "$releasePath/$id/$filename"
if [ "$index" -eq "0" ] ; then
convert "$file" -resize 96x74 "$releasePath/$id/tab.png"
append_json "$releasePath/$id/pack.json" ".\"tabIcon\" = \"tab.png\""
fi
append_json "$releasePath/$id/pack.json" ".\"stickers\" += [\"$filename\"]"
(( index ++ ))
fi
done
echo "pack stored in $releasePath/$id"
}
function mode_help_grab () {
command=$1
if [ ! -z $command ] ; then
shift
fi
if func_exists help_grab_$command ; then
mode_help_grab_$command $@
else
echo "./stickers.sh grab [SOURCE]"
echo "grabs stickers from a defined source"
echo "SOURCE:"
echo " instance : grabs stickerpack from another instance"
echo " line : grabs stickerpack from LINE"
fi
}
function mode_grab () {
command=$1
if [ ! -z $command ] ; then
shift
fi
if func_exists grab_$command ; then
mode_grab_$command $@
else
mode_help_grab
fi
}
function mode_help_grab_instance () {
echo "./stickers.sh grab instance [INSTANCE] [PACK]..."
echo "grabs a stickerpack from an instance"
echo "options:"
echo " INSTANCE : the instance to grab a pack from"
echo " PACK : the pack to grab"
}
function mode_grab_instance () {
instance=$1; shift
packs=$@
if [ ! -n "$instance" ] ; then
echo "no instance"
exit
fi
releasePath=$(jq -r '.workspace.release' $config)
tmpPath=$(jq -r '.workspace.tmp' $config)
mkdir -p $tmpPath
wget -q "https://$instance/static/stickers.json" -O "$tmpPath/stickers.json"
if [ ! -n "$packs" ] ; then
keys=$(jq -r 'keys[]' "$tmpPath/stickers.json")
echo "listing packs:"
for key in $keys ; do
echo "$key"
done
echo -n "choose a pack to download: "
read packpick
if [ ! -n "$packpick" ] ; then
echo "no pack picked"
exit
fi
packs=$packpick
fi
for pack in $packs; do
echo "downloading $pack..."
packPath=$(jq -r ".\"$pack\"" "$tmpPath"/stickers.json)
mkdir -p $releasePath/"$pack"/
echo -n "getting metadata..."
wget -q "https://$instance${packPath}pack.json" -O "$releasePath/$pack/pack.json"
echo "ok"
echo -n "getting tab icon..."
tabIcon=$(jq -r '.tabIcon' "$releasePath/$pack/pack.json")
wget -q "https://$instance${packPath}$tabIcon" -O "$releasePath/$pack/$tabIcon"
echo "ok"
echo "getting stickers..."
stickers=$(jq -r '.stickers[]' "$releasePath/$pack/pack.json")
for sticker in $stickers ; do
echo -n "downloading $sticker..."
wget -q "https://$instance${packPath}$sticker" -O "$releasePath/$pack/$sticker"
echo "ok"
done
done
echo -n "cleaning up..."
rm -Rf "$tmpPath"
echo "ok"
}
function mode_help_grab_line () {
echo "./stickers.sh grab line [PACK_ID]"
echo "grabs a pack from LINE"
echo "options:"
echo " PACK-ID : the id of the pack"
}
function mode_grab_line () {
pack=$1
if [ ! -n "$pack" ] ; then
echo "no pack id"
mode_help_grab_line
exit
fi
tmpDir=$(jq -r '.workspace.tmp' $config)
mkdir -p "$tmpDir"
echo "parsing stickerpack..."
wget -q -L "https://store.line.me/stickershop/product/$pack/en?from=sticker" -O "$tmpDir/line.html"
title=$(grep "Item01Ttl" "$tmpDir/line.html" | sed -n '/^$/!{s/<[^>]*>//g;p;}' | sed 's/^ *//ig' | sed 's/ *$//gi')
echo "sticker pack: $title"
meta=$(grep "data-preview" "$tmpDir/line.html" | grep "$pack" | sed "s/^ *data-preview='//" | sed "s/'> *$//" | recode html..utf8)
type="$(echo $meta | jq -r '.type')"
echo "type: $type"
echo "downloading pack..."
if [ "$type" == "animation" ] ; then
wget -q --no-check-certificate -L "https://dl.stickershop.line.naver.jp/products/0/0/1/$1/iphone/stickerpack@2x.zip" -O "$tmpDir/sticker.zip"
else
wget -q --no-check-certificate -L "https://dl.stickershop.line.naver.jp/products/0/0/1/$1/iphone/stickers@2x.zip" -O "$tmpDir/sticker.zip"
fi
buildDir=$(jq -r '.workspace.build' $config)
mkdir -p "$buildDir/$title/"
echo "unzipping archive..."
unzip -q "$tmpDir/sticker.zip" -d "$buildDir/$title/"
if [ "$type" == "animation" ] ; then
echo "converting animations..."
animDir="$buildDir/$title/animation@2x"
mkdir -p "$animDir/_build"
mkdir -p "$animDir/_old"
for i in "$animDir/"*.png; do
echo "converting $i..."
name=$(basename "$i" | cut -d'.' -f1)
ffmpeg -loglevel panic -i "$i" -plays 0 "$animDir/_build/$name.apng"
mv "$i" "$animDir/_old/${name}.png"
mv "$animDir/_build/${name}.apng" "$i"
done
mkdir -p "$buildDir/$title/static/"
mv "$buildDir/$title/"*.png "$buildDir/$title/static/"
cp "$buildDir/$title/static/"tab_*.png "$buildDir/$title/"
mv "$animDir/"*.png "$buildDir/$title/"
rm -Rf "$animDir"
fi
rm -Rf "$tmpDir"
mode_build "$buildDir/$title/"
}
function mode_help_install () {
echo "./stickers.sh install [PACK]..."
echo "installs sticker pack to pleroma"
echo "options:"
echo " PACK: paths to packs to install"
exit
}
function mode_install () {
packs=$*
if [ -z "$packs" ] ; then
echo "no directory"
mode_help_install
fi
staticDir=$(jq -r '.pleroma.static' $config)
check_pleroma
for pack in $packs; do
echo "installing $pack..."
if [ ! -f "$pack/pack.json" ] ; then
echo "not a valid pleroma sticker pack"
echo "use './stickers.sh build $pack' to create one"
else
cp -r "$pack" "$staticDir/static/stickers/"
echo "pack installed"
fi
done
mode_recreate
}
function mode_help_recreate () {
echo "./stickers.sh recreate"
echo "recreates sticker.json in pleroma dir"
exit
}
function mode_recreate () {
check_pleroma
echo "recreating stickers.json..."
installed=$(ls -d "$staticDir/static/stickers/"*)
for pack in $installed ; do
packName=$(basename $pack)
echo "$packName found"
append_json "$staticDir/static/stickers.json" ".\"$packName\" = \"/static/stickers/$packName/\""
done
echo "stickers.json recreated"
}
conf=""
while getopts ":c:" opt; do
case ${opt} in
c )
conf=$OPTARG
;;
: )
echo "invalid argument: $OPTARG requires and argument" 1>&2
;;
esac
done
shift $((OPTIND - 1))
command=$1
if [ -z $command ] ; then
command="help"
else
shift
fi
config=$(get_config $conf)
if func_exists $command ; then
mode_$command $@
else
echo "unknown command $command"
mode_help "*"
fi