#!/usr/bin/env python """Yet another simple identi.ca module. If you just run it, it will download your messages. If you supply a message, it will be posted For more advanced options, run with -h. Needs feedparser to be on your system Identi.ca plan API breakage but that 'The documented write API below will remain available until at least September 30, 2008.' Author: Zeth """ ################################################ # Override in lieu of a config file # # If you set these, then you won't be asked # # for them at the command line. # # # USERNAME = "" PASSWORD = "" # # ################################################ import urllib, urllib2 class IdentiCA(object): """Class for connecting to identi.ca.""" def __init__(self, username, password): """Initial Setup""" self.username = username self.password = password self.connection = urllib2.build_opener(urllib2.HTTPCookieProcessor()) def login(self): """Authenticate to identica.""" # Based on a snippet by Ryan Paul. self.connection.open(urllib2.Request("http://identi.ca/main/login", urllib.urlencode({"nickname": self.username, "password": self.password}))).read() def put_message(self, message): """Send a message to identi.ca.""" # Based on a snippet by Ryan Paul. self.connection.open(urllib2.Request("http://identi.ca/notice/new", urllib.urlencode({"status_textarea": message}))).read() def get_messages(self, limit = 0, format = "html"): """Download the latest messages. Format is html or text.""" import feedparser feed = 'http://identi.ca/' + \ self.username + '/all/rss?limit=' + str(limit) pfeed = feedparser.parse(feed) content = '' if format == 'html': for i in pfeed.entries: content += '
  • ' + fulldate(i.date_parsed) + '

    \n' content += '

    ' + \ i.title + '

    \n' content += i.description + '
  • \n' elif format == 'text': lastdate = None for i in pfeed.entries: date = fulldate(i.date_parsed) if lastdate != date: lastdate = date content += "\n" + date + '\n' content += "-" * len(date) + '\n\n' content += '* ' + i.title + '\n' return content def fulldate(timetuple): """Take the date in a tuple and converts it to English.""" import time ordinalsuffixes = {1: 'st', 2: 'nd', 3: 'rd'} format = "%A, " + str(timetuple[2]).lstrip('0') \ + ordinalsuffixes.get(timetuple[2] % 10, 'th') + " of %B, %Y." datestring = time.strftime(format, timetuple) return datestring def main(username = USERNAME, password = PASSWORD, limit = 0, format = 'text'): """When called directly.""" from optparse import OptionParser usage = "usage: %prog [options] [message]" parser = OptionParser(usage = usage) parser.add_option("-n", "--lines", action="store", type="int", dest="lines", default=False, help="output the N most recent messages.") parser.add_option("-m", "--html", action="store_true", dest="html", default=False, help="markup output as an HTML list.") (options, args) = parser.parse_args() # If there is no username or password then get it while not username: username = raw_input('Username: ') # Only care about the password if we are pushing if args: while not password: import getpass password = getpass.getpass() connection = IdentiCA(username, password) # If there is a message, push it if args: message = " ".join(args) if len(message) <= 140: connection.login() connection.put_message(message) else: print "Message is too long, aborting." return # If there is a number of messages, set it as the limit if options.lines: limit = options.lines if options.html: format = 'html' # Print out the messages. print connection.get_messages(limit = limit, format = format) # Get the ball rolling if __name__ == "__main__": main()