@@ -0,0 +1,2882 @@
+import sys%0Aimport os%0Aimport re%0A%0Aos.environ%5B'DJANGO_SETTINGS_MODULE'%5D = 'mzalendo.settings'%0A%0A# Horrible boilerplate - there must be a better way :)%0Asys.path.append(%0A    os.path.abspath(%0A        os.path.dirname(__file__) + '../..'%0A    )%0A)%0A%0Asys.path.append(%0A    os.path.abspath(%0A        os.path.dirname(__file__) + '../../..'%0A    )%0A)%0A%0Aimport itertools%0Afrom BeautifulSoup import BeautifulSoup%0A%0Afrom django.template.defaultfilters import slugify%0A%0Afrom mzalendo.core import models%0A%0A%0A# Can't download this with urllib2 as wikipedia won't let us,%0A# So there is a wgeted version sitting in this directory.%0A# source_url = 'http://en.wikipedia.org/wiki/List_of_constituencies_of_Kenya'%0A%0Aoutput_dict = %7B%7D%0A%0Asource_html = open('List_of_constituencies_of_Kenya')%0Asoup = BeautifulSoup(source_html)%0A%0Adistrict_constituency_lists = soup.findAll('ul')%0A%0Aassert len(district_constituency_lists) == 47, len(district_constituency_lists)%0A%0Afor constituency_list in district_constituency_lists:%0A    try:%0A        province_name = constituency_list.findPrevious('h3').find('span', %7B'class': 'mw-headline'%7D).a.string%0A    except AttributeError:%0A        # On wikipedia, there is no separate province listing for Nairobi.%0A        province_name = 'Nairobi'%0A%0A    province_slug = slugify(province_name)# + '-province'%0A%0A    try:%0A        models.Place.objects.get(slug=province_slug, kind__slug='province')%0A    except models.Place.DoesNotExist:%0A        try: %0A            models.Place.objects.get(slug=province_slug + '-province', kind__slug='province')%0A        except models.Place.DoesNotExist:%0A            print %22Can't find province: %25s%22 %25 province_slug%0A            continue%0A%0A    try:%0A        district_name = constituency_list.findPrevious('p').a.string%0A    except AttributeError:%0A        # District is also done differently for Nairobi.%0A        district_name = 'Nairobi County'%0A%0A    district_name = re.sub(r' District$', ' County', district_name)%0A    district_slug = slugify(district_name)%0A%0A    try:%0A        models.Place.objects.get(slug=district_slug , kind__slug='county')%0A    except models.Place.DoesNotExist:%0A        print %22Can't find district: %25s%22 %25 district_slug%0A        continue%0A%0A    province_dict = output_dict.setdefault(province_slug, %7B%7D)%0A    district_list = province_dict.setdefault(district_slug, %5B%5D)%0A%0A    constituencies = constituency_list.findAll('li')%0A%0A    for constituency in constituencies:%0A        constituency_name = constituency.a.string%0A        constituency_name = re.sub(r' Constituency$', r'', constituency_name) %0A        constituency_slug = slugify(constituency_name)%0A            %0A    try:%0A        models.Place.objects.get(slug=constituency_slug, kind__slug='constituency')%0A    except models.Place.DoesNotExist:%0A        print %22Can't find constituency: %25s%22 %25 constituency_slug%0A        continue%0A%0A        district_list.append(constituency_slug)%0A%0A    district_list.sort()%0A%0Aimport pdb;pdb.set_trace()%0A

Update constitiency to county script to match Paul's new file.