
import urllib.request
import lxml.html

from datetime import datetime


def main():
    bills = []
    for i in range(0, 200):
        url = 'http://www.tweedekamer.nl/kamerstukken/stemmingsuitslagen/detail?id=2015P' + "%05d" % (2266+i)
        print('request url: ' + url)
        response = urllib.request.urlopen(url)
        tree = lxml.html.fromstring( str(response.read()) )

        elements = tree.xpath('//div[@class="search-result-content"]/h3')

        bills_urls = get_bill_urls(elements)

        for url in bills_urls:
            bill = Bill()
            bill.from_url(url)
            bills.append(bill)
            print(bill)

    for bill in bills:
        print(bill)


class Bill():
    def __init__(self):
        self.title = 'undefined'
        self.original_title = 'undefined'
        self.type = 'undefined'  # [Amendement, Motie, Wetsvoorstel]
        self.date = datetime.now()
        self.votes = []
        self.url = ''

    def from_url(self, url):
        self.url = url
        response = urllib.request.urlopen('http://www.tweedekamer.nl' + url)
        tree = lxml.html.fromstring(str(response.read()))

        # get bill summary
        original_title = tree.xpath('//div[@class="paper-description"]/span')
        if original_title:
            self.original_title = original_title[0].text
        title = tree.xpath('//div[@class="paper-description"]/p')
        if title:
            self.title = title[0].text

        # get the bill type
        types = tree.xpath('//div[@class="paper-header"]/h1')
        if types:
            self.type = types[0].text
        else:
            types = tree.xpath('//div[@class="bill-info"]/h2')
            self.type = types[0].text

        # get the vote results
        vote_results_table = tree.xpath('//div[@class="vote-result"]/table/tbody/tr')
        for vote_html in vote_results_table:
            vote = Vote()
            self.votes.append(vote.from_html_row(vote_html))

        assert len(types) <= 1

    def __str__(self):
        summary = ''
        summary += 'Type: ' + self.type + '\n'
        summary += 'Title: ' + self.title + '\n'
        summary += 'Original title: ' + self.original_title + '\n'
        for vote in self.votes:
            summary += str(vote) + '\n'
        return summary


class Party():
    def __init__(self):
        self.name = ''
        self.seats = 0


class Vote():
    def __init__(self):
        self.party = Party()
        self.decision = ''
        self.details = ''

    def from_html_row(self, row):
        ncol = 0
        for column in row.iter():
            if column.tag == 'td':
                ncol += 1

            if ncol == 1 and column.tag == 'a':
                self.party.name = column.text
            elif ncol == 2:
                self.party.seats = int(column.text)
            elif ncol == 3 and column.tag == 'img':
                self.decision = 'VOOR'
            elif ncol == 4 and column.tag == 'img':
                self.decision = 'TEGEN'
            elif ncol == 5 and column.tag == 'h4':
                self.details = column.text
        return self

    def __str__(self):
        summary = self.party.name + ' (' + str(self.party.seats) + ')' + ': ' + self.decision
        if self.details:
            summary += ' (' + self.details + ')'
        return summary


def get_bill_urls(elements):
    vote_urls = []
    for element in elements:
        for child in element.iter():
            if 'href' in child.attrib:
                print(child.attrib['href'])
                vote_urls.append(child.attrib['href'])
    return vote_urls


if __name__ == '__main__':
    main()
@@ -686,16 +686,119 @@
 bill)%0A%0A%0A
+class Member():%0A    def __init__(self):%0A        self.name = 'undefined'%0A        self.party = Party()%0A%0A%0A
 class Bi
@@ -799,24 +799,24 @@
 ass Bill():%0A
-
     def __in
@@ -1047,24 +1047,407 @@
 elf.url = ''
+%0A        self.author = Member()%0A%0A    def get_vote_results(self):%0A        options = dict()%0A        for vote in self.votes:%0A            if vote.decision in options:%0A                options%5Bvote.decision%5D += vote.party.seats%0A            else:%0A                options%5Bvote.decision%5D = vote.party.seats%0A        for option in options:%0A            print(option + ' ' + str(options%5Boption%5D))
 %0A%0A    def fr
@@ -2085,34 +2085,77 @@
 .text%0A        el
-se
+if tree.xpath('//div%5B@class=%22bill-info%22%5D/h2')
 :%0A            ty
@@ -2144,37 +2144,41 @@
 '):%0A            
+self.
 type
-s
  = tree.xpath('/
@@ -2210,41 +2210,257 @@
 h2')
-%0A            self.type = types%5B0%5D
+%5B0%5D.text%0A%0A        # get the bill author%0A        authors = tree.xpath('//div%5B@class=%22paper-header%22%5D/dl/dd/a')%0A        for author in authors:%0A            print(author.text)%0A            print(author.attrib%5B'href'%5D)%0A            self.author.name = author
 .tex
@@ -2711,17 +2711,16 @@
 _html))%0A
-%0A
         
@@ -2942,32 +2942,64 @@
 al_title + '%5Cn'%0A
+        self.get_vote_results()%0A
         for vote
@@ -3137,24 +3137,24 @@
 f.name = ''%0A
-
         self
@@ -3164,16 +3164,47 @@
 ats = 0%0A
+        self.url = 'undefined'%0A
 %0A%0Aclass 

Add bill author html parsing