#!/usr/bin/env python import optparse import string import cgi import sys from httpencode import * http = HTTP() parser = optparse.OptionParser() parser.add_option('-u', '--username', help="Delicious username", dest="username") parser.add_option('-o', '--output', help="Output format (text or default html)", default='text', dest='output') delicious_url = string.Template( 'http://del.icio.us/feeds/json/$username?raw') item_html_template = string.Template( '
  • $title$tag_text$desc\n
  • ') item_text_template = string.Template( '* $title <$link>$tag_text$desc\n') def html_quote(v): return cgi.escape(unicode(v), 1) def br_quote(v): return v.replace('\n', '
    \n') def show_feed(username, output, writer): url = delicious_url.substitute(username=username) data = http.GET(url, output='python') if output == 'html': writer('\n') def html_writer(text): if isinstance(text, unicode): text = text.encode('ascii', 'xmlcharrefreplace') sys.stdout.write(text) def text_writer(text): if isinstance(text, unicode): text = text.encode('utf8') sys.stdout.write(text) if __name__ == '__main__': options, args = parser.parse_args() if not options.username: print 'You must give --username' parser.exit() if options.output not in ('text', 'html'): print '--output must be one of "text" or "html"' parser.exit() if options.output == 'html': writer = html_writer else: writer = text_writer show_feed(options.username, options.output, writer)