Revision 23 (by alan, 2006/05/23 20:10:28) Revert

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
"""
    Del.icio.us Google Co-op Subscribed Links Creator

    @copyright: 2000-2006 by Alan Snelson <Alan@Wave2.co.uk>
    @license: BSD License
"""
__version__ = "0.1"
import urllib2, base64, getopt, sys
from xml.dom.minidom import *
from xml.sax import saxutils

def usage ():
    """ 
    Print deliciousgsubs usage information.
    """
    print "Usage: deliciousgsubs [-hotv] [-u username] [-p password]\r\n"
    print "Options: --help -h                Displays this usage."
    print "         --username -u USERNAME   Your Del.icio.us username." 
    print "         --password -p PASSWORD   Your Del.icio.us password."
    print "         --author -a " + '"AUTHOR"' + "     Your full name."
    print "         --description -d " + '"DESCR"' +  " Your Description (usually Company Name)."
    print "         --title -t " + '"TITLE"' + "       The subscribed link title" 
    print "                                  (links to http://del.icio.us/USERNAME)."
    sys.exit(0)

def main():
    """
    Entry point for Del.icio.us Subscribed Links Creator.
    Apologies for the lack of comments in this release!.
    """
    try:
        opts, args = getopt.getopt (sys.argv[1:], "a:d:ho:p:vu:", ["author=", "description=", "help", "output=", "password=", "title=", "username="])
    except getopt.GetoptError :
        # print help information and exit:
        usage()
        sys.exit(2)
    output = 'DeliciousSubscribed.xml'
    verbose = False
    username = ''
    password = ''
    author = ''
    description = ''
    title = ''
    for o, a in opts: 
        if o == "-v":
            verbose = True
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o in ("-o", "--output"): 
            output = a
        if o in ("-u", "--username"):
            username = a
        if o in ("-p", "--password"):
            password = a
        if o in ("-t", "--title"): 
            title = a
        if o in ("-d", "--description"):
            description = a
        if o in ("-a", "--author"):
            author = a
    if username == '': 
        # print help information and exit:
        usage()
        sys.exit(2)
    if password == '':
        # print help information and exit:
        usage()
        sys.exit(2)
    if author == '':
        author = username
    if description == '':
        description = username
    if title == '': 
        title = username + "'s Del.icio.us Tags"
    f=open(output, 'w')
    f.write('<?xml version="1.0" encoding="UTF-8"?>\r\n')
    f.write ('<Results>\r\n')
    f.write('<AuthorInfo description="' + description + '" author="' + author + '"/>\r\n')
    base64string = base64.encodestring('%s:%s' % (username, password)).strip()
    request = urllib2.Request('https://api.del.icio.us/v1/tags/get')
    request.add_header('User-Agent', 'webmaster@wave2.org; http://www.wave2.co.uk')
    request.add_header('Authorization', 'Basic %s' % base64string)
    doc = parseString(urllib2.urlopen(request).read())
    posts = doc.getElementsByTagName ('tag')
    for x in posts:
        if verbose == True:
            print "Adding tag: " + x.getAttribute('tag').encode('utf-8')
        request = urllib2.Request(' https://api.del.icio.us/v1/posts/recent?count=3&tag=' + x.getAttribute('tag').encode('utf-8'))
        request.add_header('User-Agent', 'webmaster@wave2.org; http://www.wave2.co.uk')
        request.add_header('Authorization', 'Basic %s' % base64string)
        doc = parseString(urllib2.urlopen(request).read())
        posts = doc.getElementsByTagName ('post')
        f.write('<ResultSpec id="' + x.getAttribute('tag').encode('utf-8') + '">\r\n')
        f.write('<Query>' + x.getAttribute('tag').encode('utf-8') + '</Query>\r\n')
        f.write('<Response>\r\n')
        f.write('<Output name="title">' + title + '</Output>\r\n')
        f.write('<Output name="more_url">http://del.icio.us/ ' + username + '</Output>\r\n')
        i = 1
        for y in posts:
            if i < 4:
                f.write('<Output name="link' + str(i) + '">' + saxutils.escape(y.getAttribute ('description')).encode('UTF-8') + '</Output>\r\n')
                f.write('<Output name="url' + str(i) + '">' + saxutils.escape(y.getAttribute('href')).encode('UTF-8') + '</Output>\r\n')
                i = i + 1
        f.write('</Response>\r\n')
        f.write('</ResultSpec>\r\n')
    f.write('</Results>\r\n')
    if verbose == True:
        print "Generation complete please copy output file: " + output + " to your webserver and update your Google Co-op profile."
    f.close()

if __name__ == "__main__":
    main()