#!/usr/bin/python # Twitmon: monitor your Twitter followees from the command line. # Uses Twyt: http://andrewprice.me.uk/projects/twyt/ # Set the username and password below to use. # # by Tony Gambone: http://twitter.com/mogrify import sys, re, time, threading, readline import twyt.twitter as twitter import twyt.data as data # authentication username = 'SET ME' password = 'SET ME' # time to pause between checks # note that no more than 70 requests per hour are allowed pause_seconds = 2 * 60 # whether to output color text use_color = True # color escape sequences esc = chr(27) codes = { 'reset': esc + '[0m', 'bold': esc + '[1m', 'black': esc + '[30m', 'red': esc + '[31m', 'green': esc + '[32m', 'yellow': esc + '[33m', 'blue': esc + '[34m', 'magenta': esc + '[35m', 'cyan': esc + '[36m', 'white': esc + '[37m' } # regular expressions for things to color re_username = re.compile(r'(\A|\s)@\w*') re_url = re.compile(r'(\A|\s)https?://\S*') re_my_username = re.compile(r'(\A|\s)@' + username + r'\b') def style(txt, *names): if use_color: return "".join([codes[name] for name in names]) + txt + codes['reset'] else: return txt def format_status(txt): txt = re_my_username.sub( style('\g<0>', 'red', 'bold'), txt) txt = re_username.sub( style('\g<0>', 'bold'), txt) txt = re_url.sub( style('\g<0>', 'blue', 'bold'), txt) return txt def format_screen_name(name): if name == username: return style(name, 'red', 'bold') else: return style(name, 'cyan', 'bold') class FetchThread(threading.Thread): def __init__(self, twitter_object): super(FetchThread, self).__init__() self.t = twitter_object self.stop_event = threading.Event() def stop(self): self.stop_event.set() def run(self): since = False while not self.stop_event.isSet(): try: list = data.StatusList(self.t.status_friends_timeline(False, since)) if len(list) > 0: since = list[0].created_at list.reverse() # latest last for s in list: print "%s %s" % ( format_screen_name(s.user.screen_name), format_status(s.text)) print '--------------' except twitter.TwitterException, e: if str(e) == 'Server returned HTTP Error 304: Not Modified': pass # OK: this just means there are no new tweets else: print e try: time.sleep(pause_seconds) except KeyboardInterrupt, e: print "Goodbye!" self.stop() if __name__ == "__main__": t = twitter.Twitter() try: t.setauth(username, password) except AttributeError: t.set_auth(username, password) fetcher = FetchThread(t) fetcher.start() try: while (True): text = raw_input() if len(text) > 140 or len(text) <= 0: print "A tweet must be less than 140 characters! That was %d." % len(text) else: try: t.status_update(text) except twitter.TwitterException, e: print "Error! " + e except KeyboardInterrupt: print "Exiting. Please wait just a moment..." fetcher.stop() except Exception, e: print "Error! " + str(e) print "Exiting. Please wait just a moment..." fetcher.stop()