# coding: utf-8
from __future__ import unicode_literals
from utils import CanadianScraper, CanadianPerson as Person

COUNCIL_PAGE = 'http://www.assnat.qc.ca/fr/deputes/index.html'


class QuebecPersonScraper(CanadianScraper):

    def scrape(self):
        page = self.lxmlize(COUNCIL_PAGE)
        for row in page.xpath('//*[@id="ListeDeputes"]/tbody/tr'):
            name_comma, division = [cell.text_content() for cell in row[:2]]
            name = ' '.join(reversed(name_comma.strip().split(',')))
            party = row[2].text_content()
            email = self.get_email(row[3], error=False)
            detail_url = row[0][0].attrib['href']
            detail_page = self.lxmlize(detail_url)
            photo_url = detail_page.xpath('//img[@class="photoDepute"]/@src')[0]
            division = division.replace('–', '—')  # n-dash, m-dash
            p = Person(primary_org='legislature', name=name, district=division, role='MNA',
                       party=party, image=photo_url)
            p.add_source(COUNCIL_PAGE)
            p.add_source(detail_url)
            if email:
                p.add_contact('email', email)
            yield p

@@ -1134,16 +1134,1008 @@
  email)%0A
+            contact_url = detail_url.replace('index.html', 'coordonnees.html')%0A            contact_page = self.lxmlize(contact_url)%0A            p.add_source(contact_url, note='For telephone number(s)')%0A            for div in contact_page.xpath('//div%5B@class=%22blockAdresseDepute%22%5D'):%0A                try:%0A                    phone = self.get_phone(div)%0A                    heading = div.find('h3').text%0A                except Exception:%0A                    pass # probably just no phone number present%0A                else:%0A                    try:%0A                        note = %7B%0A                            'Circonscription': 'constituency',%0A                            'Parlement': 'legislature',%0A                            'Minist%C3%A8re': 'legislature',%0A                        %7D%5Bheading%5D%0A                    except KeyError:%0A                        raise # scraper should be updated to handle new value%0A                    else:%0A                        p.add_contact('voice', phone, note)%0A
         

Include telephone numbers in Assemblée nationale du Québec.