Twitter diff – bash script

Recently I’ve been planning a small twitter contest, but I had to find which new followers has joined, and who have left. So, I came up with this little script (which initially was ment to be a one-liner… yea right), I hope it will assist others as well.

#!/bin/bash
#
# twitter_diff.sh by z4ziggy
#
# handy yet dangerous utility - followers with weird chars in their names can
# cause unpredicted results but i'm too lazy to fix this. so be warned.
#
# change username to match desired twitter account to retrieve, or use 1st
# command line parameter as username.
#
username=z4ziggy

[ $# == 1 ] && username=$1

echo "[+] fetching followers for $username"
wget --quiet -O - http://api.twitter.com/1/followers/ids/$username.xml | grep "<id>" | sort > new.$username.xml
if [ -f $username.xml ]; then
        diff=$(diff -u0 $username.xml new.$username.xml)
        for id in $diff; do
                [ "${id:0:2}" != "+<" -a "${id:0:2}" != "-<" ] && continue

                user=$(echo ${id:1} | sed 's/<[^>]*>//g')
                wget http://api.twitter.com/1/users/show/$user.xml -O /tmp/$user.xml >/dev/null 2>&1
                f_sname=$(grep "<screen_name>" /tmp/$user.xml | sed 's/<[^>]*>//g;s/^[ ]*//')
                f_name=$(grep "<name>" /tmp/$user.xml | sed 's/<[^>]*>//g;s/^[ ]*//')
                f_count=$(grep "<followers_count>" /tmp/$user.xml | sed 's/<[^>]*>//g;s/^[ ]*//')

                [ "${id:0:2}" == "+<" ] && echo "[+] $f_sname ($f_name) joined (has $f_count followers) - http://twitter.com/#!/$f_sname"
                [ "${id:0:2}" == "-<" ] && echo "[-] $f_sname ($f_name) left (has $f_count followers) - http://twitter.com/#!/$f_sname"
        done
fi
mv new.$username.xml $username.xml
echo "[+] done"

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s