Exemplo n.º 1
0
def party_from_wiki(url, name=None):

    # The party wiki page is fetched to get the full name and full logo
    page = urllib.request.urlopen(url)
    party_soup = BeautifulSoup(page, "html.parser")
    infobox = party_soup.find("table", {"class": "infobox vcard"})
    logo = "http:" + infobox.find("td", {"class": "logo"})
    logo = logo.find("img").attrs["src"]

    # If both the English and Spanish name are present the Spanish one
    # has class "nickname", otherway it has "fn org"
    full_name = infobox.find("span", {"class": "nickname"})
    if full_name:
        full_name = full_name.text
    else:
        full_name = infobox.find("span", {"class": "fn org"}).text

    if not name:
        name = full_name

    short_name = None
    abbreviation = infobox.find("td", {"class": "nickname"})
    if abbreviation:
        short_name = abbreviation.text

    # With the data obtained from the wiki a new party is created and added
    # to the party set
    party = mapache.Party(
        name,
        logo_url=logo,
        short_name=short_name,
        # The abbreviation/short name will be created automatically
        # if the name provided is too long and short_name=None
        full_name=full_name)
    return party
Exemplo n.º 2
0
 def test_repeated_names(self):
     party = mapache.Party(name='name',
                           logo_url=img_url,
                           short_name='short_name',
                           full_name='full_name',
                           extra_names=['extra_name1', 'full_name'])
     self.assertEqual(len(party.get_all_names()), 4)
Exemplo n.º 3
0
 def test_short_name_from_name_initials(self):
     party = mapache.Party(name='Name Name', logo_url=img_url)
     self.assertEqual(party.short_name, 'NAM')
Exemplo n.º 4
0
 def test_short_name_from_name(self):
     party = mapache.Party(name='Name', logo_url=img_url)
     self.assertEqual(party.short_name, party.name)
Exemplo n.º 5
0
 def test_short_name_too_long(self):
     party = mapache.Party(name='name',
                           logo_url=img_url,
                           short_name='1234567891011')
     self.assertLessEqual(len(party.short_name), 7)
Exemplo n.º 6
0
 def test_getnames(self):
     party = mapache.Party(name='name',
                           logo_url=img_url,
                           short_name='short_name',
                           full_name='full_name')
     self.assertEqual(len(party.get_all_names()), 3)