# Path: montanha/models.py
# class PoliticalParty(models.Model):
# 
#     class Meta:
#         verbose_name = _("Political Party")
#         verbose_name_plural = _("Political Parties")
# 
#     siglum = models.CharField(max_length=15,
#                               verbose_name=_("Siglum"),
#                               unique=True)
# 
#     name = models.CharField(max_length=2048,
#                             verbose_name=_("Full name"),
#                             blank=True,
#                             null=True)
# 
#     logo = ThumbnailerImageField(
#         verbose_name=_('Logo'),
#         upload_to='political_party',
#         blank=True,
#         null=True)
# 
#     wikipedia = models.URLField(
#         verbose_name=_('Wikipedia'),
#         blank=True,
#         null=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.siglum)


import urllib2
from BeautifulSoup import BeautifulSoup
from montanha.models import PoliticalParty
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
##




SITE_URL = "http://pt.wikipedia.org"
PAGE_URL = "%s/wiki/%s" % (SITE_URL,
                           "Anexo:Lista_de_partidos_pol%C3%ADticos_no_Brasil")


req = urllib2.Request(PAGE_URL, headers={"User-Agent": "Mozilla/5.0"})
html = urllib2.urlopen(req)
doc = BeautifulSoup(html)

table = doc.find("span", {"id": "Partidos_ativos"}).findNext("table")

trs = table.findAll("tr")
for line in trs:
    siglum = line.findNext("td").findNext("td").string
    name = line.findNext("td").findNext("a").string
    wikipedia = SITE_URL + line.findNext("td").find("a")["href"]
    try:
        pp = PoliticalParty.objects.get(siglum=siglum)
        pp.name = name
        pp.wikipedia = wikipedia
        pp.save()

# Path: montanha/models.py
# class PoliticalParty(models.Model):
# 
#     class Meta:
#         verbose_name = _("Political Party")
#         verbose_name_plural = _("Political Parties")
# 
#     siglum = models.CharField(max_length=15,
#                               verbose_name=_("Siglum"),
#                               unique=True)
# 
#     name = models.CharField(max_length=2048,
#                             verbose_name=_("Full name"),
#                             blank=True,
#                             null=True)
# 
#     logo = ThumbnailerImageField(
#         verbose_name=_('Logo'),
#         upload_to='political_party',
#         blank=True,
#         null=True)
# 
#     wikipedia = models.URLField(
#         verbose_name=_('Wikipedia'),
#         blank=True,
#         null=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.siglum)


# Path: scripts/get_wikipedia_data.py
import urllib2
from BeautifulSoup import BeautifulSoup
from montanha.models import PoliticalParty

# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
##




SITE_URL = "http://pt.wikipedia.org"
PAGE_URL = "%s/wiki/%s" % (SITE_URL,
                           "Anexo:Lista_de_partidos_pol%C3%ADticos_no_Brasil")


req = urllib2.Request(PAGE_URL, headers={"User-Agent": "Mozilla/5.0"})
html = urllib2.urlopen(req)
doc = BeautifulSoup(html)

table = doc.find("span", {"id": "Partidos_ativos"}).findNext("table")

trs = table.findAll("tr")
for line in trs:
    siglum = line.findNext("td").findNext("td").string
    name = line.findNext("td").findNext("a").string
    wikipedia = SITE_URL + line.findNext("td").find("a")["href"]
    try:
        pp = PoliticalParty.objects.get(siglum=siglum)
        pp.name = name
        pp.wikipedia = wikipedia
        pp.save()

    except PoliticalParty.DoesNotExist: