twits /

#! /usr/bin/env python
"""twits is a simple command-line interface to show you the 
latest tweets without distracting you"""

import datetime
import sys
import twitter

from optparse import OptionParser, OptionGroup

def getPublicTweets():
    api = twitter.Api()
    return api.GetPublicTimeline()

def getUserTweets(user):
    api = twitter.Api()
    return api.GetUserTimeline(user)

def printTweets(tweets, count):
    currDate = datetime.datetime.min
    for tweet in tweets[:count]:
        tDate = datetime.datetime.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y')

        if ( != or (tDate.month != currDate.month) or (tDate.year != currDate.year):
            print ""
            print "%s" % tDate.strftime('%b %d')
            print "------"

        print "%s - %s" % (tDate.strftime('%I:%M'), tweet.text)
        currDate = tDate

def _build_parser():
    """Return a parser for the command-line interface."""
    usage = "Usage: %prog [-u USERNAME] [-c COUNT]"
    parser = OptionParser(usage=usage)
    config = OptionGroup(parser, "Configuration Options")
    config.add_option("-u", "--user", dest="user", default="",
                      help="Show a specific USER")
    config.add_option("-c", "--count", dest="count", default="10",
                      help="Show COUNT number of tweets")
    return parser

def _main():
    """Run the twits program."""
    (options, args) = _build_parser().parse_args()

    # print options
    # print args

    if options.user:
	    tweets = getUserTweets(options.user)
	    tweets = getPublicTweets()

    printTweets(tweets, int(options.count))

if __name__ == "__main__":