Use fbcmd and bash to publish on your Facebook wall


I’m not very active on Facebook, just writing a little now and then. So I wanted to auto-publish something randomly on my wall just for fun.
I had a wordlist of Swedish words laying around for other fun purposes. Why not just pick a word from that list, and pick the first picture out of a image search on the internet.
I started to look around for a way to publish on Facebook, and found fbcmd. Installation is pretty straight forward. The only dependency you need is PHP which is pretty standard anyway. Follow the above installation, and modify the the bash script to your needs, and start randomly publish stuff!
Here is an example of how to pick a word from a word list, and search for a matching image. You can find all the fbcmd commands listed here.

[sourcecode language=”bash”]
#!/bin/bash
#
# Pick a random word from a wordlist
# Search Bing for a matching image
# and publish on Facebook.
#

# Set your locale for nice date output
export LC_ALL=sv_SE.UTF-8

TODAY=$(date +"%A")
TMPFILE="/tmp/tmp-`basename $0`"
WORDLIST="/path/to/wordlist.txt"

function GenerateWord {
# Find out how many words we can play with
TOTALWORDS="`cat ${WORDLIST} | wc -l`"
# Generate a number from 1 to ${WORDS}
RAND="`shuf -i 1-${TOTALWORDS} -n 1`"
# Return the word
echo "`sed -n "${RAND}p" ${WORDLIST}`"
}

function FetchImageUrl {
# Generate a word
WORD="$(GenerateWord)"
# Get first page of Bing’s image-search of ${WORD}
curl –silent –user-agent "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.7) Gecko/20100105 Shiretoko/3.5.7" -L "https://www.bing.com/images/search?q=${WORD}" > ${TMPFILE}
# Parse the ${TMPFILE} into a more parsable file
cat ${TMPFILE} | sed -e $’s/imgurl/\\nimgurl/g’ -e $’s/:&quot//g’ -e $’s/&quot//g’ > ${TMPFILE}
# Get the image-url from first search result
IMGURL="`grep ‘imgurl;’ ${TMPFILE} | head -1 | cut -d’;’ -f2`"
}

# Since my wordlist had words that does not come up with an image.
# I need to loop until I have one.
while [ 1 ]
do
FetchImageUrl
if [ -z "${IMGURL}" ]; then
echo "${WORD} did not return any image from Bing, retrying"
else
/usr/local/bin/fbcmd FEEDLINK "${IMGURL}" "${TODAY^}ens ord med bild: ${WORD^}"
echo "Dagens ord: ${WORD^} returned ${IMGURL}"
exit
fi
done
[/sourcecode]