Skip to content

Instantly share code, notes, and snippets.

@m039
Last active December 12, 2015 03:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m039/4710629 to your computer and use it in GitHub Desktop.
Save m039/4710629 to your computer and use it in GitHub Desktop.
Shell script of usage http://iheartquotes.com/api and imagemagic to generate the quote.png
#!/bin/sh
##
## iheartquotes api
##
alias getfortune='wget --timeout=3 -O - -q'
alias fortune='getfortune http://www.iheartquotes.com/api/v1/random'
alias myfortune='getfortune http://www.iheartquotes.com/api/v1/random?source=humorix_misc'
##
## Generate image
##
print_debug() {
echo == $1 ==
echo ${@:2}
}
#
# Image parameters
#
BACKGROUND=black
# BACKGROUND=none
FILL=green
FONT="/home/m039/Desktop/wallpaper/tmp/fonts/LED.ttf"
OUTPUT_FILE=quote.png
#
# Get response
#
response=`myfortune`
print_debug "Response" $response
SYMBOL_QUOTE='"'
AUTHOR=$(echo $response | sed -n 's/\(-- .*\)*.*\(-- .*\)\s*\[.*\]\s*http:.*/\2/p')
if [ -n "$AUTHOR" ]; then
TEXT=$(echo $response | sed -n 's/\(.*\)\(-- .*\)\s*\[.*\]\s*http:.*/\1/p')
else
TEXT=$response
fi
#
# Filter text
#
TEXT=$(echo $TEXT | sed 's/ *$//g') # remove trailing whitespaces (like perl's chomp)
TEXT=$(echo $TEXT | sed 's/^"//g') # remove first and last quote will be replaced with custome one
TEXT=$(echo $TEXT | sed 's/"$//g')
TEXT=$(echo $TEXT | sed 's/http.*//g') # why if the image is not clickable
TEXT=$(echo $TEXT | recode html..ascii)
if [ -n "$TEXT" ]; then
TEXT+=${SYMBOL_QUOTE}
fi
#
# Filter author
#
AUTHOR=$(echo $AUTHOR | sed 's/http.*//g')
AUTHOR=$(echo $AUTHOR | recode html..ascii)
print_debug "Filtered text" $TEXT
print_debug "Filtered author" $AUTHOR
TDIR=`mktemp -d`
pushd $TDIR > /dev/null
alias convert_caption="convert -font ${FONT} -background ${BACKGROUND} -fill $FILL -pointsize 30 -interline-spacing 0"
convert_caption -size 10x caption:"${SYMBOL_QUOTE}" quote.png
convert_caption -size 400 caption:"${TEXT}" text.png
convert_caption -size 400 -gravity East caption:"${AUTHOR}" author.png
convert -background $BACKGROUND quote.png text.png +append result.png
convert -background $BACKGROUND result.png author.png -append result.png
popd > /dev/null
cp $TDIR/result.png $OUTPUT_FILE
rm -rf $TDIR
@m039
Copy link
Author

m039 commented Feb 4, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment