import xml.dom.minidom as minidom import urllib def get_person(url): foaf_url = url foaf = "http://xmlns.com/foaf/0.1/" # FOAF Namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" # RDF Namespace u = urllib.urlopen(foaf_url) # Load file doc = minidom.parseString(u.read()) # XML Parse u.close() # Close file # Search through all foaf:PersonalProfileDocuments, looking for one # which is about this doc (rdf:about="") and which has a maker element ppds = doc.getElementsByTagNameNS(foaf, "PersonalProfileDocument") ppd = None for i in ppds: if (i.getAttributeNodeNS(rdf, "about").value==u''): ppd = i break target = None if ppd: maker = ppd.getElementsByTagNameNS(foaf,"maker")[0] if maker: type, value = maker.attributes.items()[0] people = doc.getElementsByTagNameNS(foaf, "Person") for i in people: try: if ("nodeID" in type): # rdf:nodeID="nodename" if (i.getAttributeNode(type).value == value): target = i break if ("resource" in type): # rdf:resource="#nodename", rdf:ID="nodename" if (value[1:] == i.getAttributeNodeNS(rdf,"ID").value): target = i break if (value == i.getAttributeNodeNS(rdf,"resource").value): target = i break except Exception, E: pass if not target: people = doc.getElementsByTagNameNS(foaf, "Person") target = people[0] person = {} # Create a dict of common variables needed. for i in ["nick","name","aimChatID","yahooChatID", "msnChatID","jabberID"]: elements = target.getElementsByTagNameNS(foaf,i) if (len(elements) > 0 and elements[0].parentNode == target): person[i] = elements[0].firstChild.nodeValue for i in ["homepage", "weblog"]: elements = target.getElementsByTagNameNS(foaf,i) if (len(elements) > 0 and elements[0].parentNode == target): person[i] = elements[0].getAttributeNodeNS(rdf,"resource").value return person if __name__=="__main__": import sys print get_person(sys.argv[1])