Twittertastic three
January 29th, 2007 by DeWitt Clinton

I just uploaded the first release of python-twitter. Python twitter is a pure python wrapper around the Twitter API and data model.

Python twitter is as complete as I could make it. I spent the better part of a full day writing it, and coded it hoping to design something that will be a pleasant surprise to someone if they stumble across it some day. Too often one goes searching for a library or utility, only to find half-finished and abandoned project lying around.

With luck, python-twitter will be different. I implemented everything I could discover about the Twitter API, wrote a full python object wrapper around the data model, added a caching layer, documented the entire class library, coded some example scripts, and wrote a full suite of unit tests.

Is it good code? Well, not entirely. It feels a little heavy as I hand coded a number of things that could be done at runtime via introspection. And I’m completely baffled by how one is really supposed to use distutils and/or setuptools. I chose setuptools because I wanted to integrate the unit tests into the build and release process, but beyond that I’m at a loss. Perhaps someone can help me out with getting bdist builds (rpm, egg, etc.) working?

I’d also really appreciate a code review. If you have a minute and some python skills, please checkout the trunk and give it a once over. Please feel free to email me or file a ticket with any suggestions.

The code is licensed under the Apache License 2.0 and is copyright Google.

And as a bonus, I just checked a script called tweet.py into the head.

I’ll copy it below and highlight the lines that use the python-twitter library.


#!/usr/bin/python

'''Post a message to twitter'''

__author__ = 'dewitt@google.com'

import ConfigParser
import getopt
import os
import sys
import twitter

USAGE = ”’Usage: tweet [options] message

  This script posts a message to Twitter.

  Options:

    -h –help : print this help
    –username : the twitter username [optional]
    –password : the twitter password [optional]

  Documentation:

  If the –username or –password command line arguments are present they
  will be used to authenticate to Twitter.

  If either of the command line flags are not present, the environment
  variables TWEETUSERNAME and TWEETPASSWORD will then be checked for your
  username or password, respectively.

  If neither the command line flags nor the enviroment variables are
  present, the .tweetrc file, if it exists, can be used to set the
  default username and password.  The file should contain the
  following three lines, replacing *username* with your username, and
  *possword* with your password:

  A skeletal .tweetrc file:

    [Tweet]
    username: *username*
    password: *password*

”’

def PrintUsageAndExit():
  print USAGE
  sys.exit(2)

def GetUsernameEnv():
  return os.environ.get(”TWEETUSERNAME”, None)

def GetPasswordEnv():
  return os.environ.get(”TWEETPASSWORD”, None)

class TweetRc(object):
  def __init__(self):
    self._config = None

  def GetUsername(self):
    return self._GetOption(’username’)

  def GetPassword(self):
    return self._GetOption(’password’)

  def _GetOption(self, option):
    try:
      return self._GetConfig().get(’Tweet’, option)
    except:
      return None

  def _GetConfig(self):
    if not self._config:
      self._config = ConfigParser.ConfigParser()
      self._config.read(os.path.expanduser(’~/.tweetrc’))
    return self._config

def main():
  try:
    shortflags = ‘h’
    longflags = ['help', 'username=', 'password=']
    opts, args = getopt.gnu_getopt(sys.argv[1:], shortflags, longflags)
  except getopt.GetoptError:
    PrintUsageAndExit()
  usernameflag = None
  passwordflag = None
  for o, a in opts:
    if o in (”-h”, “–help”):
      PrintUsageAndExit()
    if o in (”–username”):
      usernameflag = a
    if o in (”–password”):
      passwordflag = a
  message = ‘ ‘.join(args)
  if not message:
    PrintUsageAndExit()
  rc = TweetRc()
  username = usernameflag or GetUsernameEnv() or rc.GetUsername()
  password = passwordflag or GetPasswordEnv() or rc.GetPassword()
  if not username or not password:
    PrintUsageAndExit()
  status = twitter.Api().PostUpdate(username, password, message)
  print “%s just posted: %s” % (status.user.name, status.text)

if __name__ == “__main__”:
  main()

Enjoy!

One Response to “Twittertastic three”

  1. DeWitt Clinton Says:

    Also, if anyone has extensive PyPI/Cheese Shop experience, I’d love if you could help walk me through the initial upload process. Thanks!