# HG changeset patch # User Thinker K.F. Li # Date 1225596779 -28800 # Node ID 3ba3edda6d1ea939c5b54c278695581c7b90f775 # Parent 9f248c8460cea5c9f76a182008ad0e192b520090 Extract more information for building townhall. diff -r 9f248c8460ce -r 3ba3edda6d1e pyikriam/buildings.py --- a/pyikriam/buildings.py Sun Nov 02 09:59:51 2008 +0800 +++ b/pyikriam/buildings.py Sun Nov 02 11:32:59 2008 +0800 @@ -19,10 +19,28 @@ pass class townhall(position): - xpath_patterns = { - 'level': 'div[@class=\'buildingLevel\']/text()' + class_patterns = { + 'level': 'buildingLevel', + 'occupied': 'value occupied', + 'rooms': 'value total', } - + value_patterns = { + 'growth': 'growth', + 'happiness': 'happiness', + 'interest_base': 'base', + 'interest_research': 'research1', + 'interest_capital': 'capital' + } + count_patterns = { + 'pop_citizens': 'citizens', + 'pop_woodworkers': 'woodworkers', + 'pop_specialworkers': 'specialworkers', + 'pop_scientists': 'scientists' + } + appear_patterns = { + 'is_upgrading': 'isUpgrading' + } + def __init__(self, city_id, idx, baseurl): super(townhall, self).__init__('townhall', city_id, idx, baseurl) pass @@ -30,16 +48,56 @@ def _sync(self, page): parser = etree.HTMLParser(encoding='utf8') page_dom = etree.parse(StringIO(page), parser) - xpath_building = '/html/body/descendant::' - for name, ptn in self.xpath_patterns.items(): - path = xpath_building + ptn + + xpath_building = '/html/body/descendant::*[@class=\'%s\']/text()' + for name, clzname in self.class_patterns.items(): + path = xpath_building % (clzname) + value = page_dom.xpath(path)[0] + setattr(self, name, value) + pass + + xpath_value = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'value\']/text()' + for name, clzname in self.value_patterns.items(): + path = xpath_value % (clzname) + value = page_dom.xpath(path)[0] + setattr(self, name, value) + pass + + xpath_count = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'count\']/text()' + for name, clzname in self.count_patterns.items(): + path = xpath_count % (clzname) value = page_dom.xpath(path)[0] setattr(self, name, value) pass + + xpath_appear = '/html/body/descendant::*[starts-with(@class,\'%s\')]' + for name, clzname in self.appear_patterns.items(): + path = xpath_appear % (clzname) + cnt = len(page_dom.xpath(path)) + if cnt != 0: + setattr(self, name, True) + else: + setattr(self, name, False) + pass + pass + + xpath_upgrade = '/descendant::ul[@class=\'actions\']/li[@class=\'upgrade\']/a' + anodes = page_dom.xpath(xpath_upgrade) + if len(anodes) == 1: + anode = anodes[0] + self.upgrade_uri = anode.get('href') + else: + self.upgrade_uri = None + pass pass def sync(self): page = self.get_page() self._sync(page) pass + + def upgrade(self): + url = self._baseurl + self.upgrade_uri + page = c(url).get().get_content() + pass pass diff -r 9f248c8460ce -r 3ba3edda6d1e pyikriam/example.py --- a/pyikriam/example.py Sun Nov 02 09:59:51 2008 +0800 +++ b/pyikriam/example.py Sun Nov 02 11:32:59 2008 +0800 @@ -24,4 +24,11 @@ print 'positions ' + repr(city.positions) city.positions[0].sync() -print 'positions[0] level is ' + str(city.positions[0].level) +city_attrs = ('level', 'occupied', 'rooms', 'growth', 'happiness', + 'interest_base', 'interest_research', 'interest_capital', + 'pop_citizens', 'pop_woodworkers', 'pop_specialworkers', + 'pop_scientists', 'is_upgrading', 'upgrade_uri') +for city_attr in city_attrs: + value = getattr(city.positions[0], city_attr) + print 'positions[0].%s is %s' % (city_attr, str(value)) + pass