# coding: utf-8
from pupa.scrape import Scraper

from utils import lxmlize, CanadianLegislator as Legislator

import re
from urlparse import ulrjoin

COUNCIL_PAGE = 'http://www.calgary.ca/General/Pages/Calgary-City-Council.aspx'


class CalgaryPersonScraper(Scraper):

  def get_people(self):
    page = lxmlize(COUNCIL_PAGE)
    councillor_urls = page.xpath('//div[@class="cocis-image"]/a/@href')
    for url in councillor_urls:
      yield councillor_data(urljoin(COUNCIL_PAGE, url))

def councillor_data(url):
  page = lxmlize(url)
  name_and_ward = page.xpath('string(//h1[@id="cocis-content-page-title"])')
  ward, name = re.match(r'(Ward [0-9]+) Councillor (.+)').groups()
  photo_url_rel = page.xpath('div[@id="contactInfo"]//img[1]/@src')
  photo_url = urljoin(url, photo_url_rel)
  # no email, there's a contact form!
  phone = page.xpath('//strong[contains(., "Phone")]/next-sibling::text()')

  p = Legislator(name=name, post_id=ward, role='Councillor')
  p.add_source(COUNCIL_PAGE)
  p.add_contact('voice', phone, 'legislature')
  p.image = photo

  return p


@@ -139,10 +139,10 @@
 rt u
-l
 r
+l
 join
@@ -222,16 +222,68 @@
 l.aspx'%0A
+MAYOR_PAGE = 'http://calgarymayor.ca/forms_all.php'%0A
 %0A%0Aclass 
@@ -375,30 +375,20 @@
 GE)%0A    
-councillor_url
+node
 s = page
@@ -405,319 +405,1042 @@
 div%5B
+contains(
 @class
-=
+,
 %22cocis-
-image%22%5D/a/@
+has-caption%22)%5D')%5B1:%5D%0A    for node in nodes:%0A      url = urljoin(COUNCIL_PAGE, node.xpath('string(.//a%5B1%5D/
 href
-'
+)')
 )%0A    
-for url in councillor_urls:%0A      yield councillor_data(urljoin(COUNCIL_PAGE, url))%0A%0Adef councillor_data(url):%0A  page = lxmlize(url)%0A  name_and_ward = page.xpath('string(//h1%5B@id=%22cocis-content-page-title%22%5D)')%0A  ward, name = re.match(r'(Ward %5B0-9%5D+) Councillor (.+)').groups(
+  name = node.xpath('string(.//a%5B2%5D)')%0A      ward = ' '.join(node.xpath('string(.//strong)').split()%5B:-1%5D)%0A      yield councillor_data(url, name, ward)%0A%0A    mayor_node = page.xpath('//div%5Bcontains(@class, %22cocis-image-panel%22)%5D')%5B0%5D%0A    photo_url = urljoin(COUNCIL_PAGE, mayor_node.xpath('string(.//img/@src)'))%0A    name = mayor_node.xpath('string(.//a%5B2%5D)')%0A    mayor_page = lxmlize(MAYOR_PAGE)%0A    email = mayor_page.xpath('string(//a%5Bcontains(., %22@%22)%5D)')%0A    phone = mayor_page.xpath('string(//strong%5Bcontains(., %22Phone%22)%5D/'%0A                             'following-sibling::text())')%0A    m = Legislator(name=name, post_id='Calgary', role='Mayor')%0A    m.add_source(COUNCIL_PAGE)%0A    m.add_source(MAYOR_PAGE)%0A    m.add_contact('email', email, None)%0A    m.add_contact('voice', phone, 'legislature')%0A    m.image = photo_url%0A    yield m%0A%0Adef councillor_data(url, name, ward):%0A  page = lxmlize(url
 )%0A  
@@ -1642,12 +1642,17 @@
 %22)%5D/
-next
+following
 -sib
@@ -1821,16 +1821,20 @@
  = photo
+_url
 %0A%0A  retu

fix calgary