~yasumoto7/five-a-day/icon_countdown

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import libxml2
import urllib2

from gettext import gettext as _

# deactivate error messages from the validation [libxml2.htmlParseDoc]
def noerr(ctx, str):
    pass

libxml2.registerErrorHandler(noerr, None)

def get_teams(user):
    try:
        participation = urllib2.urlopen("https://launchpad.net/~%s/+participation" %user)
    except urllib2.HTTPError:
        raise ValueError, _("User '%s' not found in launchpad") %user
    try:
        ctx = libxml2.htmlParseDoc(participation.read(), "UTF-8")
        x = ctx.xpathEval('//div[@class="portlet"]/h2[contains(.,"membership")]/following-sibling::table//tr')
        for i in x:
            team = i.xpathEval('td[2]/div/a')
            assert team
            team = team[0].prop("href").split("~", 1).pop()
            yield team
    finally:
        participation.close()