#!/usr/bin/python2.4 # # This examples generates an XML file from the submitted stories # for the specified digg user. # # For example: # http://server.com/cgi-bin/diggannos.py?user=kevinrose # # Produces: # # # # ... # # # CGI Parameters: # user - digg.com user to get submitted stories from # label (optional) - additional label to add to generated annotations. # # Copyright 2007 Google Inc. All Rights Reserved.""" __author__ = 'mwytock@google.com (Matt Wytock)' import cgi import urllib import xml.dom.minidom # Digg API application key. See http://apidoc.digg.com/ApplicationKeys for more # information. APPKEY = "http://www.google.com/cse" def GetSubmissions(user): url = "http://services.digg.com/user/%s/submissions?%s" % ( user, urllib.urlencode({ "appkey" : APPKEY, "count" : 100 })) fh = urllib.urlopen(url) # Sometimes xml.dom.minidom chokes on diggs XML, we just ignore # these problems for now try: doc = xml.dom.minidom.parse(fh) except: return stories = doc.documentElement for story in stories.childNodes: if story.nodeType == xml.dom.Node.ELEMENT_NODE: yield story.getAttribute("link") def main(): print "Content-Type: text/xml" print try: form = cgi.FieldStorage() doc = xml.dom.minidom.getDOMImplementation().createDocument( None, "Annotations", None) annotations = doc.documentElement user = form["user"].value for url in GetSubmissions(user): annotation = doc.createElement("Annotation") if "urlmode" in form and form["urlmode"].value == "host": import urlparse annotation.setAttribute("about", "%s/*" % urlparse.urlparse(url)[1]) else: annotation.setAttribute("about", url) label = doc.createElement("Label") label.setAttribute("name", "submitted") annotation.appendChild(label) label = doc.createElement("Label") label.setAttribute("name", user) annotation.appendChild(label) if "label" in form: label = doc.createElement("Label") label.setAttribute("name", form["label"].value) annotation.appendChild(label) annotations.appendChild(annotation) print doc.toxml() except (Exception), e: print "%s" % e if __name__ == "__main__": main()