Python Tweeter Library

Following on from the Accessing Twitter with Python post, I’ve made the code in that post more reusable by wrapping it in a Python class and making a distributable library module that I can install on my Raspberry Pi.

The class only serves one method at the moment, that’s to post updates to twitter.  I’ll add more as I need them.

The code includes some basic error checking, logging and some test cases at the bottom which run if the tweeter.py class is executed rather than imported.

To use this you’ll need a twitter account and create a tweeter.cfg file as defined in the comments in the source code.  A word of warning, the test cases will post to the twitter account, you may wan to clean up afterwards and/or create a test account.

Below is the tweeter.py library code:

# Contents of configuration file: tweeter.cfg
#
# [DEFAULT]
# consumer_key = *********************
# consumer_secret = *******************************************
# access_token_key = **************************************************
# access_token_secret = ******************************************

import twitter
import ConfigParser
import logging

class tweeter:

    __client = None

    def __init__(self, __configFile = "tweeter.cfg"):

        __config = ConfigParser.RawConfigParser()
        logging.basicConfig(filename='tweeter.log',level=logging.DEBUG,
                            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                            datefmt='%m/%d/%Y %I:%M:%S %p')

        try:
            __config.readfp(open(__configFile))
        except IOError as e:
            logging.debug("I/O Error ({0}): ({1})".format(e.errno, e.strerror))
            return
        try:
            __consumerKey = __config.get('DEFAULT', 'consumer_key')
            __consumerSecret = __config.get('DEFAULT', 'consumer_secret')
            __accessTokenKey = __config.get('DEFAULT', 'access_token_key')
            __accessTokenSecret = __config.get('DEFAULT', 'access_token_secret')
        except ConfigParser.NoSectionError as e:
            logging.debug("ConfigParser Error ({0})".format(e))
            return
        except ConfigParser.NoOptionError as e:
            logging.debug("ConfigParser Error ({0})".format(e))
            return
        self.__client = twitter.Api(consumer_key=__consumerKey,
                                    consumer_secret=__consumerSecret,
                                    access_token_key=__accessTokenKey,
                                    access_token_secret=__accessTokenSecret)

    def tweet(self, msg = None):
        if msg == None:
            logging.debug("Twitter Error Message is Empty")
            return
        if type(msg) is not str:
            logging.debug("Twitter Error Message is not a String")
            return
        if len(msg) > 140:
            logging.debug("Twitter Error Message Exceeds 140 Chars")
            return
        try:
            update = self.__client.PostUpdate(msg)
            return update.text
        except AttributeError as e:
            logging.debug("Attributer Error ({0})".format(e))
        except twitter.TwitterError as e:
            logging.debug("Twitter Error ({0})".format(e))

if __name__ == "__main__":

    longMesg = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
    tooLongMesg = "901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
    emptyMesg = None
    errTweeter = tweeter("error.cfg")
    print errTweeter.tweet("Test1")
    myTweeter = tweeter()
    print myTweeter.tweet(123)
    print myTweeter.tweet(emptyMesg)
    print myTweeter.tweet(tooLongMesg)
    print myTweeter.tweet(longMesg)
    print myTweeter.tweet("Test1")

Below is the setup.py file:

from distutils.core import setup
setup(name='tweeter',
      version='0.3.2',
      description='Twitter Wrapper',
      author='Jamie Jackson',
      author_email='jamie at jacobean.net',
      url='http://blog.jacobean.net',
      py_modules=['tweeter'],
      )

On Linux, use the following to install:

# sudo python setup.py install

Leave a Reply

Your email address will not be published. Required fields are marked *