Mercurial > eagle-eye
diff pyikriam/buildings.py @ 188:bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 02 Nov 2008 22:31:06 +0800 |
parents | bff16e6ee3ef |
children | 550e20dd7573 |
line wrap: on
line diff
--- a/pyikriam/buildings.py Sun Nov 02 16:33:01 2008 +0800 +++ b/pyikriam/buildings.py Sun Nov 02 22:31:06 2008 +0800 @@ -1,7 +1,9 @@ from lazy.www import c from lxml import etree from StringIO import StringIO -from sync_utils import * +from sync_utils import sync_tagclass, sync_tagvalue +from sync_utils import sync_tagcount, sync_tagclass_start_appear +from sync_utils import ikariam_zh_timeval class position(object): def __init__(self, build_type, city_id, idx, baseurl): @@ -27,6 +29,11 @@ pass pass +## \brief Base class of all building class. +# +# This class extract information from page of building. That are +# information all buildings have. +# class building(position): class_patterns = { 'level': 'buildingLevel' @@ -34,6 +41,11 @@ appear_patterns = { 'is_upgrading': 'isUpgrading' } + upgrade_res_patterns = { + 'wood': 'wood', + 'marble': 'marble', + 'crystal': 'glass' + } def _sync(self, page_dom): sync_tagclass(self, building.class_patterns, page_dom) @@ -44,22 +56,44 @@ anodes = page_dom.xpath(xpath_upgrade) if len(anodes) == 1: anode = anodes[0] - self.upgrade_uri = anode.get('href') + self._upgrade_uri = anode.get('href') else: - self.upgrade_uri = None + self._upgrade_uri = None pass + self.upgrade_wood = 0 + self.upgrade_marble = 0 + self.upgrade_crystal = 0 + self.upgrade_time = 0 + self.upgrade_countdown = 0 + if self.is_upgrading: xpath_countdown = '/descendant::div[@id=\'upgradeCountDown\']/text()' value = page_dom.xpath(xpath_countdown)[0] - self.upgrade_countdown = value + self.upgrade_countdown = ikariam_zh_timeval(value) else: - self.upgrade_countdown = None + xpath_res = '/descendant::div[@class=\'content\']/ul[@class=\'resources\']/li[starts-with(@class, \'%s\')]/text()' + + for resname, clzname in building.upgrade_res_patterns.items(): + xpath = xpath_res % (clzname) + txts = page_dom.xpath(xpath) + if len(txts) == 1: + value = txts[0].strip() + setattr(self, 'upgrade_' + resname, int(value)) + pass + pass + + xpath_time = xpath_res % ('time') + txts = page_dom.xpath(xpath_time) + if len(txts) == 1: + value = txts[0].strip() + self.upgrade_time = ikariam_zh_timeval(value) + pass pass pass def upgrade(self): - url = self._baseurl + self.upgrade_uri + url = self._baseurl + self._upgrade_uri page = c(url).get().get_content() pass pass @@ -112,7 +146,7 @@ self.researching = anode.get('title') xpath_countdown = '/descendant::div[@id=\'researchCountDown\']/text()' txtnodes = page_dom.xpath(xpath_countdown) - self.researching_countdown = txtnodes[0] + self.researching_countdown = ikariam_zh_timeval(txtnodes[0]) else: self.researching = None self.researching_countdown = None