Automating Countdown Tweets with Bash ¬

2009-11-02

Modern Warfare 2 will be out in a few days, so I felt @cowardswayout should count down to the release day. I could spend a few minutes at some point during each of the next few days—assuming I can remember to—posting a nearly-the-same message to Twitter or I could automate it. Yeah, better automate.

In trying to keep my bash-fu hightened, I tossed together the following bash script:

#!/bin/bash

# 
# mw2_countdown
# 
# Post countdown to Modern Warfare 2 release to Twitter every day
# 

release_year=2009
release_month=11
release_day=10
year=10#$(date +%Y)
month=10#$(date +%m)
day=10#$(date +%d)
username='cowardswayout'
password='somethingiwontleakhere'

if (( $release_year == $year && $release_month == $month && $day <= $release_day )); then
	if (( $day == $release_day )); then
		printf -v message "Modern Warfare 2 (http://bit.ly/dQMPz) is out! Go get your copy!"
	else
		printf -v message "Modern Warfare 2 (http://bit.ly/dQMPz) in %s..." $(( $release_day - $day ))
	fi	
	curl -u $username:$password -d status="$message" http://twitter.com/statuses/update.xml
fi

Since I host with Mac OS X Server and am anal about doing things “The Mac Way”, I whipped up a launchd job to run it every morning at 1am:

<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.cowardswayout.mw2_countdown</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/local/bin/mw2_countdown</string>
	</array>
	<key>StartCalendarInterval</key>
	<dict>  
		<key>Hour</key>
		<integer>1</integer>
		<key>Minute</key>
		<integer>0</integer>
	</dict>
</dict>
</plist>

As you can see, I installed the bash script in /usr/local/bin/mw2_countdown and the launchd job went in /Library/LaunchDaemons/com.cowardswayout.mw2_countdown.plist.

I changed the permissions so that only root has read/execute access to the bash script, since the Twitter account password is stored in plain text:

sudo chmod 700 /usr/local/bin/mw2_countdown

And loaded the launchd job:

sudo launchctl load /Library/LaunchDaemons/com.cowardswayout.mw2_countdown.plist

Now I only have to remember to remove the bash script and launchd job sometime after 11/10/09. Naturally, this script can be easily tailored to your own needs.

Update: I’ve updated the script to prepend 10# to each call like $(date +%y) to force it to be evaluated as base 10 and also switched from using test (square brackets) to using the correct arithmetic evaluations (double parentheses).

  Textile help