Skip to content

Instantly share code, notes, and snippets.

@andyjpb
Forked from DRMacIver/fetch.rb
Created July 2, 2010 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyjpb/461228 to your computer and use it in GitHub Desktop.
Save andyjpb/461228 to your computer and use it in GitHub Desktop.
#!/bin/sh
# ./twit-followers <screen_name>
# Based on http://gist.github.com/459828 by DRMacIver
# Script by Andy Bennett <andyjpb@ashurst.eu.org>
API_USER=username
API_PASS=password
DOT_FILE=~/.twit-followers
TMP_FILE=./$$
STDERR=2
# #############################################################################
function global_usage() {
echo "Usage: `basename $0` -n [user]" >&$STDERR
echo -e "\tuser - User to analyse followers for" >&$STDERR
echo -e "\t-n - Don't update $DOT_FILE" >&$STDERR
echo -e "\t-v - Be verbose when there's nothing to report" >&$STDERR
echo "" >&$STDERR
exit 1
}
NO_SAVE=0
VERBOSE=0
# Parse global command line options: "bats <global options> command <command options>"
while getopts ":nv" OPT; do
case "$OPT" in
n)
NO_SAVE=1
;;
v)
VERBOSE=1
;;
*)
global_usage
;;
esac
done
shift $(($OPTIND-1))
OPTIND=1
unset OPT
NAME=${1:-$API_USER}
DATE=`date +"%Y/%m/%d %H:%M:%S"`
# use -f flag to curl to give exit codes on errors
# -S flag doesn't seem to work
CURL="curl --basic -u $API_USER:$API_PASS"
# fetch a list of previous followers
if [ -f $DOT_FILE ]; then
OLD_LIST=`grep ^$NAME: $DOT_FILE | tail -n1`
else
OLD_LIST=""
fi
OLD_DATE=`echo "$OLD_LIST" | cut -f2-4 -d :`
OLD_LIST=`echo "$OLD_LIST" | cut -f5- -d :`
# fetch a list of current followers
CUR_LIST=`$CURL -f -S http://twitter.com/followers/ids.json?screen_name=$NAME 2>/dev/null`
if [ $? -ne 0 ]; then
echo "Twitter API Error" >&$STDERR
exit 3
fi
if [ $NO_SAVE = 0 ]; then
echo $NAME:$DATE:$CUR_LIST >> ~/.twit-followers
fi
# Get rid of the '[' and ']' and sort the lists
CUR_LIST=`echo "$CUR_LIST" | sed -e 's/^\[//' -e 's/\]$//' | tr , '\n' | sort -n > $TMP_FILE.CUR`
OLD_LIST=`echo "$OLD_LIST" | sed -e 's/^\[//' -e 's/\]$//' | tr , '\n' | sort -n > $TMP_FILE.OLD`
#OLD_LIST=`echo "$OLD_LIST" | sed -e 's/^\[//' -e 's/\]$//' | tr , '\n' | sort -n | tr '\n' , | sed -e 's/,$//'`
DIFF=`diff $TMP_FILE.OLD $TMP_FILE.CUR`
EC=$?
rm $TMP_FILE.OLD $TMP_FILE.CUR
if [ $EC -eq 0 ]; then
if [ $VERBOSE = 1 ]; then
echo "Nothing to report since $OLD_DATE" >&$STDERR
fi
exit 0
fi
NEW_FOLLOW=`echo "$DIFF" | grep "^> " | sed -e 's/^> //' | tr '\n' ,`
UN_FOLLOW=`echo "$DIFF" | grep "^< " | sed -e 's/^< //' | tr '\n' ,`
function resolve() {
# break the list into lines of 100 ids
LIST=`echo "$1" | sed -e 's/\(\([0-9]*,\)\{100\}\)/\1\n/g'`
# http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users-lookup
for followers in $LIST; do
R=`$CURL http://api.twitter.com/1/users/lookup.json?user_id=$followers 2>/dev/null`
R=`echo "$R" | sed -e 's/},{/}\n{/g'`
# Remove subparts: -e 's/,"[a-z_]*":{[^}]*},/,/g'
IFS="
"
for user in $R; do
echo "$user" | sed -e 's/.*"screen_name":"\([^"]*\)".*"name":"\([^"]*\)".*"id":\([0-9]*\).*/\3:\1:\2/g' -e "s/^/$2:/"
done
done
}
if [ "$NEW_FOLLOW" != "," ]; then
resolve "$NEW_FOLLOW" "+"
fi
if [ "$UN_FOLLOW" != "," ]; then
resolve "$UN_FOLLOW" "-"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment