1 #!/usr/bin/env python 2 3 """ 4 Fetch the profile of a user from a Confluence site, printing their profile 5 details in a tab-separated sequence using the following format: 6 7 USERNAME FULLNAME EMAIL IMAGE-URL 8 """ 9 10 from os.path import split 11 from urllib import basejoin 12 import libxml2dom, sys, codecs 13 14 def get_profile(url, username): 15 try: 16 d = libxml2dom.parseURI("%s/display/~%s" % (url.rstrip("/"), username), html=True, htmlencoding="utf-8") 17 18 fullname = d.xpath("//span[@id='fullName']") 19 fullname = fullname and fullname[0].textContent or "" 20 email = d.xpath("//span[@id='email']") 21 email = email and email[0].textContent.replace(" at ", "@").replace(" dot ", ".") or "" 22 image = d.xpath("//img[contains(@class, 'userLogo')]/@src") 23 image = image and image[0].textContent or "" 24 25 return [username, fullname, email, image and basejoin(url, image) or ""] 26 27 except libxml2dom.LSException: 28 return None 29 30 def main(): 31 progname = split(sys.argv[0])[-1] 32 33 try: 34 url = sys.argv[1] 35 username = sys.argv[2] 36 except IndexError: 37 print >>sys.stderr, "%s <wiki URL> <username>" % progname 38 print >>sys.stderr 39 print >>sys.stderr, "Example: %s http://wiki.list.org/ <username>" % progname 40 sys.exit(1) 41 42 stdout = codecs.getwriter("utf-8")(sys.stdout) 43 details = get_profile(url, username) 44 if details: 45 print >>stdout, "\t".join(details) 46 47 if __name__ == "__main__": 48 main() 49 50 # vim: tabstop=4 expandtab shiftwidth=4