How to Code a Twitter Bot in Python on Dreamhost

Here’s the source code to my @YHobosHaveDogs bot. It basically searches for the text ‘”homeless people” dogs’ and then replies to them (if they posted the message in the last 24 hours, the bot hasn’t replied to them before, and if this isn’t a reply to someone the bot has replied to.) My initial testing shows that the bot’s reply to these search results is relevant most of the time.

Download Twitter bot’s code: yhobos_script.py

It’s very important that you test your bot before putting it out in the world, especially if it messages strangers. You don’t want to accidentally spam people. The PostUpdate() method will give you an error if you try to post the exact same message as one you’ve posted before, but this won’t prevent all problems.

What I do to test is comment out the PostUpdate() calls and slip in a print statement instead, so I can see what the program would post without it actually posting anything. Twitter has rate-limiting for a max of 350 calls per hour.

Once you have your bot working, you’ll want to have the bot’s script regularly called. If you have a desktop that is always connected to the Internet, you can use Windows’ Scheduled Tasks or on Mac you can use iCal, cron, or launchd to have the bot’s script run every hour or so.

If you have a webhost like Dreamhost, you can use cron to schedule repeated calls to the bot script. But first you will have to set up the Python twitter module on Dreamhost. This requires a few extra steps because you cannot install modules to the system’s Python binary. Instead we will use virtualenv to create our own Python to install the twitter module to.

Installing Modules to Python on Dreamhost

SSH into your Dreamhost account (I use PuTTY on Windows. Linux and Mac OS X have the command line “ssh” program that you can run from a terminal screen.)

run the following commands to install VirtualEnv: (you might want to read the Dreamhost wiki’s Python page for details. I also found this page to be helpful.)

wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.4.3.tar.gz tar xzvf virtualenv-1.4.3.tar.gz cd virtualenv-1.4.3/ python virtualenv.py $HOME/local mv ~/local/lib/python2.5 ~/local/lib/python

(The last command is to rename the python2.5 directory to python, which the twitter module’s installer seems to demand.)

Then install the python-twitter module (which will also install the oauth module it needs):

wget http://python-twitter.googlecode.com/files/python-twitter-0.8.2.tar.gz tar xzvf python-twitter-0.8.2.tar.gz cd python-twitter-0.8.2/ /home/yourusername/local/python setup.py install --home=~/local

Setting Up Cron on Dreamhost for your Bot

You will have to set the bot’s script file as executable, which you can do by entering this command:

chmod 774 mytwitterbot.py

And add this code to the very top of the bot script so that Dreamhost will use your virtualenv Python to run the script in /home/yourusername/local/bin instead of the system’s Python in /usr/bin:

#!/home/yourusername/local/bin/python

Also, I found that I had to change the Python path at the beginning of the script since Dreamhost’s cron daemon didn’t have it set up. There’s probably a more graceful way to handle this, but I simply added this code to the beginning of my twitter bot code:

sys.path = ['/home/yourusername/yhoboshavedogs', '/home/yourusername/local/lib/python/python_twitter-0.8.2-py2.5.egg', '/home/yourusername/local/lib/python/oauth2-1.5.211-py2.5.egg', '/home/yourusername/local/lib/python/httplib2-0.7.4-py2.5.egg', '/home/yourusername/local/lib/python/simplejson-1.9.2-py2.5-linux-x86_64.egg', '/home/yourusername/local/lib/python/python_twitter-0.8.2-py2.5.egg', '/home/yourusername/local/lib/python/oauth2-1.5.211-py2.5.egg', '/home/yourusername/local/lib/python/simplejson-1.9.2-py2.5-linux-x86_64.egg', '/home/yourusername/local/lib/python2.5', '/home/yourusername/local/lib/python', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/local/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/Numeric', '/usr/lib/python2.5/site-packages/PIL', '/var/lib/python-support/python2.5', '/usr/lib/site-python', '/home/yourusername/local/lib/python2.5']

Page 2 of 3 | Previous page | Next page