# HG changeset patch # User "Rex Tsai " # Date 1225622188 -28800 # Node ID 270e608bb0b6ec7d38effb6e21809bb70ffca7b3 # Parent dba0543daf3608e5e6c256e3427baa79bbd2357b# Parent bff16e6ee3efda608dda7f7db1f9bad4cbbe05fe branch merged. diff -r dba0543daf36 -r 270e608bb0b6 pyikriam/buildings.py --- a/pyikriam/buildings.py Sun Nov 02 16:52:40 2008 +0800 +++ b/pyikriam/buildings.py Sun Nov 02 18:36:28 2008 +0800 @@ -1,6 +1,7 @@ from lazy.www import c from lxml import etree from StringIO import StringIO +from sync_utils import * class position(object): def __init__(self, build_type, city_id, idx, baseurl): @@ -16,30 +17,138 @@ def get_page(self): page = c(self._baseurl).get(self._params).get_content() return page + + def sync(self): + page = self.get_page() + parser = etree.HTMLParser(encoding='utf8') + page_dom = etree.parse(StringIO(page), parser) + + self._sync(page_dom) + pass pass -class townhall(position): - xpath_patterns = { - 'level': '/div/div/div[@class=\'buildingLevel\']/text()' +class building(position): + class_patterns = { + 'level': 'buildingLevel' + } + appear_patterns = { + 'is_upgrading': 'isUpgrading' } + def _sync(self, page_dom): + sync_tagclass(self, building.class_patterns, page_dom) + + sync_tagclass_start_appear(self, building.appear_patterns, page_dom) + + 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 + + if self.is_upgrading: + xpath_countdown = '/descendant::div[@id=\'upgradeCountDown\']/text()' + value = page_dom.xpath(xpath_countdown)[0] + self.upgrade_countdown = value + else: + self.upgrade_countdown = None + pass + pass + + def upgrade(self): + url = self._baseurl + self.upgrade_uri + page = c(url).get().get_content() + pass + pass + +class townhall(building): + class_patterns = { + 'occupied': 'value occupied', + 'rooms': 'value total', + } + value_patterns = { + 'growth': 'growth', + 'happiness': 'happiness', + 'interest_base': 'base', + 'interest_research': 'research1', + 'interest_capital': 'capital', + 'overpopulation': 'cat overpopulation' + } + count_patterns = { + 'pop_citizens': 'citizens', + 'pop_woodworkers': 'woodworkers', + 'pop_specialworkers': 'specialworkers', + 'pop_scientists': 'scientists' + } + def __init__(self, city_id, idx, baseurl): super(townhall, self).__init__('townhall', city_id, idx, baseurl) pass - def _sync(self, page): - parser = etree.HTMLParser(encoding='utf8') - page_dom = etree.parse(StringIO(page), parser) - xpath_building = '/html/body/div/div' - for name, ptn in self.xpath_patterns.items(): - path = xpath_building + ptn - value = page_dom.xpath(path)[0] - setattr(self, name, value) - pass + def _sync(self, page_dom): + sync_tagclass(self, townhall.class_patterns, page_dom) + + sync_tagvalue(self, townhall.value_patterns, page_dom) + + sync_tagcount(self, townhall.count_patterns, page_dom) + + super(townhall, self)._sync(page_dom) + pass + pass + +class academy(building): + def __init__(self, city_id, idx, baseurl): + super(academy, self).__init__('academy', city_id, idx, baseurl) pass - def sync(self): - page = self.get_page() - self._sync(page) + def _sync(self, page_dom): + xpath_research_name = '/descendant::*[@class=\'researchName\']/a' + anodes = page_dom.xpath(xpath_research_name) + if len(anodes) == 1: + anode = anodes[0] + self.researching = anode.get('title') + xpath_countdown = '/descendant::div[@id=\'researchCountDown\']/text()' + txtnodes = page_dom.xpath(xpath_countdown) + self.researching_countdown = txtnodes[0] + else: + self.researching = None + self.researching_countdown = None + pass + + super(academy, self)._sync(page_dom) + pass + pass + +class warehouse(building): + def __init__(self, city_id, idx, baseurl): + super(warehouse, self).__init__('warehouse', city_id, idx, baseurl) pass pass + +class barracks(building): + def __init__(self, city_id, idx, baseurl): + super(barracks, self).__init__('barracks', city_id, idx, baseurl) + pass + pass + +class branchoffice(building): + def __init__(self, city_id, idx, baseurl): + super(branchoffice, self).__init__('branchoffice', city_id, idx, baseurl) + pass + pass + +class port(building): + def __init__(self, city_id, idx, baseurl): + super(port, self).__init__('port', city_id, idx, baseurl) + pass + pass + +class wall(building): + def __init__(self, city_id, idx, baseurl): + super(wall, self).__init__('wall', city_id, idx, baseurl) + pass + pass + diff -r dba0543daf36 -r 270e608bb0b6 pyikriam/example.py --- a/pyikriam/example.py Sun Nov 02 16:52:40 2008 +0800 +++ b/pyikriam/example.py Sun Nov 02 18:36:28 2008 +0800 @@ -1,4 +1,5 @@ import sys +import buildings from ikariam import Ikariam if len(sys.argv) != 2: @@ -23,5 +24,15 @@ print 'sulfur is ' + city.sulfur print 'positions ' + repr(city.positions) -city.positions[0].sync() -print 'positions[0] level is ' + str(city.positions[0].level) +for idx, pos in enumerate(city.positions): + if not isinstance(pos, buildings.position): + continue + pos.sync() + building_attrs = filter(lambda attr: not attr[0].startswith('_'), + pos.__dict__.items()) + building_attrs.sort(key=lambda x: x[0]) + print + print 'positions[%d]' % (idx) + for building_attr, value in building_attrs: + print '\t%s: %s' % (building_attr, repr(value)) + pass diff -r dba0543daf36 -r 270e608bb0b6 pyikriam/ikariam.py --- a/pyikriam/ikariam.py Sun Nov 02 16:52:40 2008 +0800 +++ b/pyikriam/ikariam.py Sun Nov 02 18:36:28 2008 +0800 @@ -70,13 +70,13 @@ class IkariamCity: data_patterns = { - 'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()', - 'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()', - 'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()', - 'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()', - 'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()', - 'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()', - 'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()' + 'gold': 'value_gold', + 'inhabitants': 'value_inhabitants', + 'wood': 'value_wood', + 'wine': 'value_wine', + 'marble': 'value_marble', + 'crystal': 'value_crystal', + 'sulfur': 'value_sulfur' } def __init__(self, id, core ): self.core = core @@ -88,14 +88,14 @@ parser = etree.HTMLParser(encoding='utf8') page_dom = etree.parse(StringIO(page), parser) - xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']" - for name, path in self.data_patterns.items(): - xpath = xpath_globalinfo + path + xpath_globalinfo = '/descendant::*[@id=\'%s\']/text()' + for name, tag_id in self.data_patterns.items(): + xpath = xpath_globalinfo % (tag_id) value = page_dom.xpath(xpath)[0] setattr(self, name, value) pass - xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li' + xpath_mainview = '/descendant::div[@id=\'mainview\']/ul/li' pos_doms = page_dom.xpath(xpath_mainview) positions = [self._mk_position(pos_dom, idx) for idx, pos_dom in enumerate(pos_doms)] diff -r dba0543daf36 -r 270e608bb0b6 pyikriam/sync_utils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyikriam/sync_utils.py Sun Nov 02 18:36:28 2008 +0800 @@ -0,0 +1,44 @@ +## \file +# \brief Sync information of objects with DOM trees of respective pages. +# + +def sync_tagclass(obj, patterns, page_dom): + xpath_building = '/html/body/descendant::*[@class=\'%s\']/text()' + for name, clzname in patterns.items(): + path = xpath_building % (clzname) + value = float(page_dom.xpath(path)[0]) + setattr(obj, name, value) + pass + pass + +def sync_tagvalue(obj, patterns, page_dom): + xpath_value = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'value\']/text()' + for name, clzname in patterns.items(): + path = xpath_value % (clzname) + value = float(page_dom.xpath(path)[0]) + setattr(obj, name, value) + pass + pass + +def sync_tagcount(obj, patterns, page_dom): + xpath_count = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'count\']/text()' + for name, clzname in patterns.items(): + path = xpath_count % (clzname) + value = int(page_dom.xpath(path)[0]) + setattr(obj, name, value) + pass + pass + +def sync_tagclass_start_appear(obj, patterns, page_dom): + xpath_appear = '/html/body/descendant::*[starts-with(@class,\'%s\')]' + for name, clzname in patterns.items(): + path = xpath_appear % (clzname) + cnt = len(page_dom.xpath(path)) + if cnt != 0: + setattr(obj, name, True) + else: + setattr(obj, name, False) + pass + pass + pass +