comparison pyikriam/buildings.py @ 168:8f699a9da6c0

Extract building level of townhalls. - townhall class is responding to extract information about townhall.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 02:26:28 +0800
parents
children 9f248c8460ce
comparison
equal deleted inserted replaced
167:6ab01f709650 168:8f699a9da6c0
1 from lazy.www import c
2 from lxml import etree
3 from StringIO import StringIO
4
5 class position(object):
6 def __init__(self, build_type, city_id, idx, baseurl):
7 self._baseurl = baseurl + '/index.php'
8 self.build_type = build_type
9 self.city_id = city_id
10 self.idx = idx
11 self._params = {'view': 'buildingGround',
12 'id': city_id,
13 'position': idx}
14 pass
15
16 def get_page(self):
17 page = c(self._baseurl).get(self._params).get_content()
18 return page
19 pass
20
21 class townhall(position):
22 xpath_patterns = {
23 'level': '/div/div/div[@class=\'buildingLevel\']/text()'
24 }
25
26 def __init__(self, city_id, idx, baseurl):
27 super(townhall, self).__init__('townhall', city_id, idx, baseurl)
28 pass
29
30 def _sync(self, page):
31 parser = etree.HTMLParser(encoding='utf8')
32 page_dom = etree.parse(StringIO(page), parser)
33 xpath_building = '/html/body/div/div'
34 for name, ptn in self.xpath_patterns.items():
35 path = xpath_building + ptn
36 value = page_dom.xpath(path)[0]
37 setattr(self, name, value)
38 pass
39 pass
40
41 def sync(self):
42 page = self.get_page()
43 self._sync(page)
44 pass
45 pass