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