#!/usr/bin/ev python # -*- coding:utf-8 -*- # # Hack on http://codeviewer.org/view/code:9af;view:text # First, we're going to use a more intuitive menu system; # Check dependencies try: import sys, getpass, twitter except Exception,e: print """Dependency load failed, and needs installing Please double check all dependecies; Error message: %s""" % ( e ) raise SystemExit class Feeder: def __init__( self, twit ): self.twit = twit self.twit.SetSource( "Twit-Twoo" ) # Menu system while(1): a = raw_input("Enter Command; ? for help\n> ") opt = a.split(" ") if len(opt) == 1: opt.append( 'null' ) if a == 'q' : self.quit( 'null' ) try: { '?' : self.help, 'viewall' : self.view_all_user, 'view' : self.view_latest_user, 'viewfriends' : self.view_latest_friends, 'post' : self.post_status }[ opt[0] ]( opt[1:] ) except: self.help("null") def view_latest_user( self, who ): """ Just return the last tweet from a twit (Twat?) """ stat = self.twit.GetUser( who[0] ) print "%s on %s (%s):\n%s\n" %( stat.name, stat.GetStatus().created_at, stat.GetStatus().relative_created_at, stat.GetStatus().text ) def view_all_user( self, who ): """ View last 20 tweets from a user (No more jokes as above, wasn't funny then """ for user in self.twit.GetUserTimeline( who[0] ): print user.text def view_latest_friends( self, nul ): """ Essentially, get through each friend and get their latest tweet S-L-O-W, Avoid!!! """ friends = self.twit.GetFriends() for friend in friends: self.view_latest_user( friend.id ) def view_latest_order( self ): """ Just as looking on your User Home """ #TODO pass def post_status( self, status ): """ Posts status/Tweets. Simple enough, the basic want and need (Not wanton need- thats too carnal) """ # Did the user specify one? Don't want to be sending 'null' # Check length, too stat_str = "" for word in status: stat_str = "%s%s " % (stat_str, word ) if stat_str == 'null' or len( stat_str ) >= 140: print "You must specify a message, of which the length must be 140 chars or fewer" else: self.twit.PostUpdate( stat_str ) def help( self, nul ): """ Return the options we're giving the user """ #TODO pass def quit( self, nul ): """ Does what it says on the Tin """ print "Now Exiting..." raise SystemExit if __name__ == '__main__': def usage(): print """Usage: %s ( Login with specified user ) or: %s ( Prompts for user ) Please do not enter your password on the command line """ % ( sys.argv[0], sys.argv[0] ) raise SystemExit try: if len( sys.argv ) == 1: uname, pw = raw_input( "Twitter client\nUsername:\n> " ), getpass.getpass( "Password:\n> " ) elif len( sys.argv ) == 2: uname, pw = sys.argv[1], getpass.getpass( "Twitter client\nPassword:\n> ") else: usage() except: print "Some error occurred" raise SystemExit twit = twitter.Api( username=uname, password=pw ) try: Feeder( twit ) except: print "Could not create Twitter object" raise SystemExit