Python Twitter-bot Framework

I’m working on a Twitter bot to be used for the upcoming episode of Mock The Movie this Thursday, and I have every intention of participating as often as humanly possible, if not the least way by running this bot when necessary.

If you’re a Python developer and are interested in building a Twitter bot, go get Tweepy and feel free to start with this framework. You’ll need to register your application with Twitter first, of course. Read the Twitter OAuth FAQ for more details.

The code for authenticating against Twitter was originally borrowed from Nessy’s Blog, but the site design that Nessy was regex’ing against to try to scrape the auth tokens out of has since changed. I’ve updated it, and included caching these tokens in plaintext files in the program folder. Nessy’s code will only run the first time.

Additionally, my bot will work in daemon mode, performing work every so many seconds according to the configuration of the iterationtime variable. You end it by hitting Ctrl-C.

To get started, enter your application consumer_key and consumer_secret, as well as the username/password for the account you’d like to connect it with. Put all the actual work you’d like to perform at the bottom, where marked with a comment. The Tweepy API specifications are very well documented, but if you’re having specific problems, I might be able to help. By the time anyone reads this and gets to that point, I’m sure I’ll have my own bot done. Good luck! Code below the fold.

Update: Well. Trying to include the code into the post directly, even in preformat tags, causes my web host to flip out and provide 406 errors as though they were protecting me from being hacked. Until I can figure out what’s going on with it, you’ll have to go to a zipped text version of the code instead. Sorry about that folks.
Update 2: Still erroring, just on this page. Something about this specific post is throwing mod_security for a wobbly. You might not be able to click on links, but if you copy them and paste them into the address bar, they’ll work fine. Unfortunately this means comments are broken since they require a Post action that you can’t do while Hostpapa’s mod_security configuration is being stupid.
Update 3: You have GOT to be kidding me. One of these words in the URL caused the issue: “python”, “twitter” or “bot”. I’m experimenting further.
Update 4: But not if there’s no dashes between them. Okay. That’s progress.

Trying adding the code again:

#Python Twitter bot framework
#Requires tweepy ( )
#Auth code heavily modified from - 
#   caches credentials in text files, fixed regex's for Twitter site scraping 
#   as of time of release. Nessy's code runs only on first login.
#All original code by Jason Thibeault -
#v0.4 - August 14, 2011

import sys
import tweepy
import re
import urllib2
import urllib
import time
from time import gmtime, strftime

#application secrets!
consumer_key = "xxxxxxxxxxxxxxx"
consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

#account information
username = "username"
password = "password"

iterationtime = "60" #time in seconds of "rest" between loop iterations

#do not edit these variables
running = True
access_key = ""
access_secret = ""

while running:
        #here we try to load from files
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        if access_key=="":
                f = open('config-accesskey', 'r')
                access_key =
                f = open('config-accesssecret', 'r')
                access_secret =

            #if there's no files, we have to ask Twitter for authentication then write the files
                print "Do not have authorization information on file! Automatically obtaining permission for this application."
                print "Getting authorization URL..."
                auth_url = auth.get_authorization_url()
                print "URL is: %s" % auth_url

                print "Requesting authorization..."
                request = urllib2.urlopen(auth_url)
                data =
                authtoken = re.compile('<form action="" id="oauth_form" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="(.*)" /></div>').findall(data)
                oauthtoken = re.compile('<input id="oauth_token" name="oauth_token" type="hidden" value="(.*)"').findall(data)

                print "authtoken: %s" % authtoken[0]
                print "oauthtoken: %s" % oauthtoken[0]

                print "Logging in..."
                post = {'authenticity_token': authtoken[0], 'oauth_token': oauthtoken[0], 'session[username_or_email]': username, 'session[password]': password}
                request = urllib2.Request('')

                data = urllib2.urlopen(request).read()
                code = re.compile('<kbd aria-labelledby="code-desc"><code>(.*)</code></kbd>').findall(data)
                print "pin: %s" % code[0]
                access_key = auth.access_token.key
                access_secret = auth.access_token.secret
                print "access key: %s" % access_key
                    f = open('config-accesskey', 'a')
                    pin = f.write(access_key)
                    print "Couldn't write to config-accesskey file!"
                print "access secret: %s" % access_secret
                    f = open('config-accesssecret', 'a')
                    pin = f.write(access_secret)
                    print "Couldn't write to config-accesskey file!"
            print "I have all auth tokens necessary, connecting to Twitter API..."
            auth.set_access_token(access_key, access_secret)
            api = tweepy.API(auth)
            apistatus = str(api.test())
            print "API connected: %s" % apistatus
    except KeyboardInterrupt:
        print "Ctrl-C pressed, quitting."
        running = False
    except apistatus=="False":
        print "Could not connect to Twitter API, aborting daemon."
        running = False
        print "Abnormal program termination. Sorry!"
        running = False
            #this is where all the actual heavy lifting goes!
            #since we don't have anything in this routine, let's just show a timestamp on the console and 
            print "[%s] - Nothing to do right now!" % strftime("%Y-%m-%d %I:%M:%S %p %Z")
            #wait until next iteration
        except KeyboardInterrupt:
            print "Ctrl-C pressed, quitting."
            running = False
Python Twitter-bot Framework

3 thoughts on “Python Twitter-bot Framework

Comments are closed.