#!/bin/sh
#
#	Author: Timothy J. Luoma
#	Email:	luomat at gmail dot com
#	Date:	2008-07-03
#
#	Purpose: Figure out current Twitter API limit


# This script, as designed, doesn't store your password.
# This is more secure, in that it doesn't leave your password unattended
# and easier, if you want to check more than one account
# however, if someone else is logged into your computer
# and runs 'ps auxwww' they might get to see your password.

# If you want to store your username and password, 
# put them below, remove the "# " at the beginning of the line
# And delete the OPTIONAL section below
# 
# TWITTER_USERNAME="MyTwitterName"
# 
# TWITTER_PASSWORD="MyTwitterPassword"



##################	BEGIN OPTIONAL SECTION
###
if [ "$#" = "2" ]
then

	# If we were given two arguments, assume the first is the username 
	# and the second is the password

	TWITTER_USERNAME="$1" 

	TWITTER_PASSWORD="$2" 

elif [ "$#" = "1" ]
then

	TWITTER_USERNAME="$1" 

	echo "`basename $0`: You gave me the Twitter username \"$TWITTER_USERNAME\". Now I need the password (this is not stored anywhere)"
	/bin/echo -n "Password for $TWITTER_USERNAME ? "

	read TWITTER_PASSWORD


else

	# If we were NOT given two arguments, try to educate user as 
	# to how they might use this script properly.
	# note: as if 2008-07-09 there is no Twitter user 'RealCoolGuy'

	echo "

`basename $0`: ERROR: 
	You must supply your username and password with a space between each, like this:
	
`basename $0` username password
	
	so if your Twitter login is 'RealCoolGuy' and your password is 'ilovenuts' then you'd use
	
`basename $0` RealCoolGuy ilovenuts
	
"
	exit 1

fi
###
##################	END OPTIONAL SECTION




# This is the official URL we use to get the results
# Don't change this unless Twitter does, and they shouldn't
# but they might, so we'll put it here
API_URL="http://twitter.com/account/rate_limit_status.xml"

# Local temp file
TEMP="/tmp/twitterapi.txt"

# Clear out old temp files before we begin
rm -f "${TEMP}*"

# Grab the info above
curl -s --output "$TEMP" -u "$TWITTER_USERNAME":"$TWITTER_PASSWORD" "$API_URL"

	# The result seems to add a "%" at the EOL which we want to get rid of
	# or at least, I do. So this next part is a bit hacky, but works

	# cat the output file, showing control codes, and dump the % at EOL
	cat -v "$TEMP" | sed 's#%$##g' > "$TEMP.1"

	# rename the file to the original
	mv -f "$TEMP.1" "$TEMP"

# Check the result we received...
CHECK=`cat "$TEMP"`

# . . .vs the known authentication error as of 2008-07-03
FAIL="Could not authenticate you."


# If they are the same, die with an error
if [ "$CHECK" = "$FAIL" ]
then
	echo "$0: Authentication failed. Check your username ($TWITTER_USERNAME) and password ($TWITTER_PASSWORD)"
	exit 1
fi	

#
# This is where we parse / scrape the results file for the info we want
#

# here is where we find what the current limit is on API hits
LIMIT=`fgrep 'hourly-limit' "$TEMP" |\
sed 's#  <hourly-limit type="integer">##g; s#</hourly-limit>##g'`

# here is where we find out how many we have left
REMAINING=`fgrep 'remaining-hits' "$TEMP" |\
sed 's#  <remaining-hits type="integer">##g; s#</remaining-hits>##g'`

# here is where we find out the "seconds since epoch" until reset
# this is easier than trying to parse the date they give
# if not quite as exact
RESET_TIME=`fgrep '<reset-time-in-seconds type="integer">' "$TEMP" |\
sed 's#  <reset-time-in-seconds type="integer">##g; s#</reset-time-in-seconds>##g'`

# current time in epoch seconds
CURRENT_TIME=`date '+%s'`

# calculate the number of seconds difference
TIME_DIFF=`expr $RESET_TIME - $CURRENT_TIME`

# divide by 60 to get minutes, because really, who wants to get it in seconds
# unless it's less than 1 minute?
MINUTES_LEFT=`expr $TIME_DIFF / 60` 



# If we didn't find one or the other of what we wanted, tell the user, but keep going
if [ "$LIMIT" = "" ]
then
	echo "$0: Warning: was not able to determine current limit. Twitter may be down, or may have changed format of API call response"
	echo "You can see the response we received from Twitter at: $TEMP"
	
fi

if [ "$REMAINING" = "" ]
then
	echo "$0: Warning: was not able to determine API hits remaining. Twitter may be down, or may have changed format of API call response"
	echo "You can see the response we received from Twitter at: $TEMP"
fi


# this should give us a nice clear report


echo "
Twitter API totals for account:		$TWITTER_USERNAME

Number of API Hits Allowed Per Hour:	$LIMIT
Number of Hits You Have Remaining:	$REMAINING
"


# If they haven't used any API hits, there is no countdown timer
if [ "$LIMIT" != "$REMAINING" ]
then

	if [ "$MINUTES_LEFT" -gt "1" ]
	then
		# there are 2-59 minutes remaining
		echo "Time remaining until API count reset:	$MINUTES_LEFT minutes"
	elif  [ "$MINUTES_LEFT" = "1" ]
	then
		# If there's 1 minute remaining, I don't want to see "1 minutes"
		echo "Time remaining until API count reset:	1 minute"
	else
		# I've actually seen this hit "0" and "-1" so we'll 
		# cover our bases with an "other"
		# I suppose if Twitter comes back with "Cupper" as a response, 
		# this will fail here
		# but we've tried to cover for other cases
		#
		echo "Time remaining until API count reset:	$TIME_DIFF seconds"
	fi
fi



exit 0
# EOF

