twitpie: Twitter Commandline Client

Monday, June 16th, 2008 @ 5:24 pm | Internet, Programming, Python

A simple need, solved in a simple way - a twitter client for those wanting command-line access and auto-refresh + posting tweets from the same place. It does not implement anything fancy. Just the basic features.

You can choose to expand on the source code and/or leave comments so that I can update it and let everyone else also enjoy a better client.

#!/usr/bin/env python
#
#           twitpie
#
#           Twitter Command Line Client
#
#           A simplistic client useful to get a auto-refreshed feed of your
#           + friends timeline and to post tweets.
#
#
# Author:   Harshad Sharma
#           http://www.twitter.com/hiway
#           harshad (dot) sharma @ gmail (dot) com
#
#
# Usage:    Edit this file --> put in username and password
#           Start program "python twit.py" - it will automatically start showing
#           the latest tweets. To update or quit, press Ctrl+C
#           Type in a tweet and press enter to post the update.
#           simply type 'q' to quit.
#
# Dependency:
#           http://pypi.python.org/pypi/python-twitter/
#
# License:
#           This program is free software: you can redistribute it and/or modify
#           it under the terms of the GNU General Public License as published by
#           the Free Software Foundation, either version 3 of the License, or
#           (at your option) any later version.
#
#           This program is distributed in the hope that it will be useful,
#           but WITHOUT ANY WARRANTY; without even the implied warranty of
#           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#           GNU General Public License for more details.
#
#           You should have received a copy of the GNU General Public License
#           along with this program.  If not, see .

import twitter
import time

# Add your username and password here…
api = twitter.Api(username=’twitterusername’, password=’twitterpassword’)     # <--- EDIT HERE

version = 0.1
finished = False

# Loop till finished is changed to True
while finished == False:
    try:
        # Create an empty list.
        tweets = []

        # iterate throgh the friends timeline and gather data.
        for i in api.GetFriendsTimeline():

            #-------- Choose your fave formatting: --------
            # keep only one line uncommented at a time.

            #tweets.append( "%s (%s) \n%s\n" %(i.user.name, i.relative_created_at, i.text))
            #tweets.append( "%s | %s (%s)\n" %(i.user.name, i.text, i.relative_created_at))
            #tweets.append( "(%s) %s" %(i.user.name, i.text))
            #tweets.append( "%s: %s" %(i.user.name, i.text))
            tweets.append( "%s\n%s\n" %(i.user.name, i.text))

        # Since we want the latest tweets to appear near the prompt instead of
        # scrolling up... (if you want it the other way round, comment the line)
        tweets.reverse()

        # Iterate over the list and print
        for tweet in tweets:
            print tweet

        # Wait for some time...
        time.sleep(3*60) # 3*60 = 3 minutes

    except KeyboardInterrupt: # If user presses Ctrl-C, do the following:

        # Get imput - either tweet or command to exit.
        tweet = raw_input("\n\nWhat are you doing (q=quit | r=refresh): ")

        if tweet.lower() == 'q':
            # Exit loop by setting the flag
            finished = True
        if tweet.lower() == 'r':
            pass
        else:
            # User wants to post a tweet... do it :-)
            api.PostUpdate(tweet)

Leave a Reply