#!/usr/bin/python2.4
#
# This example generates a XML file with a
# definition and tags for the specified users
# friend network.
#
# For example:
# http://server.com/cgi-bin/diggcse.py?user=kevinrose
#
# Produces:
#
#
# digg.com Network - kevinrose
#
#
#
#
#
#
#
#
#
#
#
#
# ...
#
#
# CGI Parameters:
# user - digg.com user to build CSE for
#
# 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"
# URL where diggannos.py runs
DIGGANNOS = "http://mwytock.komarix.org/pub/bin/diggannos.py"
def GetFriends(user):
offset = 0
total = 1 # default value > offset
while offset < total:
url = "http://services.digg.com/user/%s/friends?%s" % (
user,
urllib.urlencode({
"appkey" : APPKEY,
"count" : 100,
"offset" : offset
}))
fh = urllib.urlopen(url)
doc = xml.dom.minidom.parse(fh)
users = doc.documentElement
total = int(users.getAttribute("total"))
offset += int(users.getAttribute("count"))
for node in users.childNodes:
if (node.nodeType == xml.dom.Node.ELEMENT_NODE and
node.nodeName == "user"):
yield node.getAttribute("name")
def main():
print "Content-Type: text/xml"
print
form = cgi.FieldStorage()
try:
user = form["user"].value
doc = xml.dom.minidom.getDOMImplementation().createDocument(
None, "GoogleCustomizations", None)
gc = doc.documentElement
cse = doc.createElement("CustomSearchEngine")
title = doc.createElement("Title")
title.appendChild(doc.createTextNode("digg.com Network - %s" % user))
cse.appendChild(title)
context = doc.createElement("Context")
bg_labels = doc.createElement("BackgroundLabels")
label = doc.createElement("Label")
label.setAttribute("name", "submitted")
label.setAttribute("mode", "FILTER")
bg_labels.appendChild(label)
context.appendChild(bg_labels)
cse.appendChild(context)
gc.appendChild(cse)
include = doc.createElement("Include")
include.setAttribute(
"href",
"%s?%s" % (
DIGGANNOS,
urllib.urlencode({
"user" : user
})))
include.setAttribute("type", "Annotations")
gc.appendChild(include)
for friend in GetFriends(user):
include = doc.createElement("Include")
include.setAttribute(
"href",
"http://mwytock.komarix.org/bin/diggannos.py?%s" % urllib.urlencode({
"user" : friend
}))
include.setAttribute("type", "Annotations")
gc.appendChild(include)
print doc.toxml()
except (Exception), e:
print "%s" % e
if __name__ == "__main__":
main()