Skip to content

Instantly share code, notes, and snippets.

@dogtopus
Created January 4, 2020 19:26
Show Gist options
  • Save dogtopus/7c428c32f300550b7d08cade52863cbc to your computer and use it in GitHub Desktop.
Save dogtopus/7c428c32f300550b7d08cade52863cbc to your computer and use it in GitHub Desktop.
Tieba pseudo-"dazzle" post generator
#!/bin/bash
# 20131221 - Add new font type - 1040006, 104900{1,2,3}
getglyphname(){
if [ ! $1 == ' ' ]
then {
printf "%s.jpg\n" \
$({
LC_CTYPE=C.UTF-8 printf "baidu_dazz%x" "'$1" \
| md5sum \
| sed -r 's/^(.{4}).{24}(.{4}).*$/\1\2/g'
})
}; fi
}
render(){
RES_PATH=/tmp/dazz_glyphs-$(cat /dev/urandom | tr -dc 'A-Za-z0-9' | head -c6)
mkdir -p "$RES_PATH"
echo 'Downloading texture...' 1>&2
for (( i=0; i<${#2}; i++ )); do {
# special effect
case $1 in
1040001)
color=$(($RANDOM % 8 + 1));;
1040013)
if [ -z $color ] || [ $color == 2 ]; then
color=1
else
color=2
fi;;
esac
glyph=$(getglyphname ${2:$i:1})
if [ -z $glyph ]
then {
echo 'null:' >> "$RES_PATH/glyphs.lst"
}
else {
echo "$RES_PATH/$glyph" >> "$RES_PATH/glyphs.lst"
printf "http://tb1.bdstatic.com/tb/fonts/$1$color/%s\n" $glyph
}; fi
}; done | wget -q -nc -i - -P $RES_PATH
echo 'Generating image...' 1>&2
montage @$RES_PATH/glyphs.lst \
-tile "${3}x" -geometry "1x1+0+0<" "$4"
rm -rf $RES_PATH
echo 'Done' 1>&2
}
fontid(){
case $1 in
color)
echo 1040001;;
square)
echo 1040002;;
cat)
echo 1040003;; # animate
cute)
echo 1040004;;
bulb)
echo 1040005;;
xmas)
echo 1040006;;
ny)
echo 1040007;;
train)
echo 1040008;;
ticket)
echo 1040009;;
fff)
echo 1040010;;
beer)
echo 1040011;; # animate
candle)
echo 1040012;; # animate
flower)
echo 1040013;;
star)
echo 1049001;;
crown)
echo 1049002;;
wing)
echo 1049003;;
*)
font_id=$(echo $1 | sed -r 's/ID=(.*)/\1/g')
if [ -z $font_id ] || [ -z $(echo $1 | grep 'ID=') ]; then
echo 'Unknown font' 1>&2
exit 1
else
echo $font_id
fi
;;
esac
}
usage(){
echo "Usage: $0 [-f <font>] [-o <output>] [-l <line_width>] <text>" 1>&2
cat 1>&2 << "EOS"
Fonts list: (default: color)
color
cat
square
bulb
cute
xmas
ny
train
ticket
fff
beer
candle
flower
star
crown
wing
EOS
}
if [ $# == 0 ]; then usage; exit 0; fi
while getopts ':f:o:l:' opt; do {
case $opt in
f)
font=$(fontid $OPTARG) || exit 1;;
o)
output="$OPTARG";;
l)
lw=$OPTARG;;
*)
usage
exit 1
;;
esac
}; done
shift $((OPTIND-1))
if [ -z $font ] ; then font=$(fontid 'color'); fi
if [ -z $lw ] ; then lw=30 ; fi
if [ -z $output ]; then output='-' ; fi
if [ -z "$1" ] ; then usage; exit 1 ; fi
render $font "$1" $lw $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment