#!/bin/sh
#
#	Author: Timothy J. Luoma
#	Email:	luomat at gmail dot com
#	Date:	2009-12-06
#
#	Purpose: add a new user to a twitter list
#
# The latest version of this script should always be available here:
#
# http://dl.dropbox.com/u/18414/bin/twitter-add-to-list.sh

###########
## 	Definte the Twitter username (DO NOT INCLUDE THE @)
##	i.e. mine would be 
## EDIT AND UNCOMMENT THE NEXT LINE
#TWITTER_USERNAME=luomat

###########
## EDIT AND UNCOMMENT THE NEXT LINE
TWITTER_USERNAME=YourTwitterNameHere

###########
## This is the password you use to login to Twitter
## EDIT AND UNCOMMENT THE NEXT LINE
TWITTER_PASSWORD=YoUrPassWoRD

###########
##	This is the name of the Twitter list.
##	for example, if Twitter name was @tj and the list was "sf-tweetup"
##	you ought to be able to access the list from https://twitter.com/tj/sf-tweetup
##
##	THIS LIST MUST ALREADY EXIST AT TWITTER.COM. The script will NOT make it for you.
## EDIT AND UNCOMMENT THE NEXT LINE
LISTNAME=

###########
## NOTE: usually I would recommend putting your Twitter name and password in ~/.netrc however that 
## 			does NOT seem to work for list management
##			IF OTHER PEOPLE USE THE COMPUTER YOU RUN THIS SCRIPT ON
##			they may be able to see your username and password in 'ps'

###########
##
##	Usage: twitter-add-to-list.sh name1 [name2 name3 etc]
##
##	Twitter lists are limited to 500 members
##	Do NOT include @ in the twitter names


### TODO (maybe)
###	Check validity of list name?
###	Create list if needed?






## You should NOT have to edit anything below this line


# I use this to define variables without having to hard code
# them into scripts that I am going to share with the internet
if [ -r $HOME/.source ]
then

	. $HOME/.source
fi

## get the shortname of this script
NAME=`basename $0`

## We're going to need to store some local temp files
## if you are worried about security on a multiuser machine,
## don't use /tmp/
TEMP="/tmp/$NAME"

# make the temp directory
mkdir -p $TEMP


for TWIT in $*
do

# place to store information for each person
FILE=$TEMP/$NAME.$TWIT

# Get their user number (and verify that they exist)
curl --location --referer ";auto" -D - -s -u "${TWITTER_USERNAME}:${TWITTER_PASSWORD}"  "https://twitter.com/users/show.xml?screen_name=$TWIT" |cat -v > "$FILE"

# Assume that input is bad unless Twitter tells us it is good
VALID=NO

# look for a 'good response' from Twitter.
# This should also handle fail whales

egrep -q "^HTTP/1.1 200 OK" "$FILE" && VALID=yes

if [ "$VALID" = "NO" ]
then
	echo "$NAME: $TWIT is not a valid user"

else
	echo "$NAME: $TWIT is a valid user"

	# We need to add users bu their twitter database ID (which doesn't change)
	# not their twitter name (which can)
	TWITNUMBER=`egrep '^  <id>' "$FILE" | sed 's#.*<id>##g; s#</id>##g'`
			
		# Add to the list
		# The first line is there because... well, trust me, it needs to be
		# second line is your login info
		# third line is their Twitter number
		# the last line is the "add to the list" command
	curl --location --referer ";auto" -D - -s \
	-u "${TWITTER_USERNAME}:${TWITTER_PASSWORD}" \
	-d "id=$TWITNUMBER" \
	"http://api.twitter.com/1/$TWITTER_USERNAME/$LISTNAME/members.xml" > "$FILE"
	
	# Assume it failed unless twitter tell us it didn't
	ADDED=no

	# Look for good response, again, this should handle fail whales
	egrep -q "^HTTP/1.1 200 OK" "$FILE" && ADDED=yes
	
	# tell us whether they were added to the list or not
	if [ "$ADDED" = "yes" ]
	then
		echo "$NAME: I added $TWIT ($TWITNUMBER) to the $LISTNAME list"
	else
		echo "$NAME: I did not successfull add $TWIT ($TWITNUMBER) to the $LISTNAME list"
	fi
		
fi	# valid = no



done

exit 0
# EOF
