comparison pyikriam/ikariam.py @ 175:9f248c8460ce

Simplize xpath patterns. - Use 'descendant' aix name simplize patterns.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 09:59:51 +0800
parents 8f699a9da6c0
children 785c89e5db32
comparison
equal deleted inserted replaced
174:0cfc7a19a4d2 175:9f248c8460ce
68 return IkariamCity(id=id, core=self) 68 return IkariamCity(id=id, core=self)
69 pass 69 pass
70 70
71 class IkariamCity: 71 class IkariamCity:
72 data_patterns = { 72 data_patterns = {
73 'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()', 73 'gold': 'value_gold',
74 'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()', 74 'inhabitants': 'value_inhabitants',
75 'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()', 75 'wood': 'value_wood',
76 'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()', 76 'wine': 'value_wine',
77 'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()', 77 'marble': 'value_marble',
78 'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()', 78 'crystal': 'value_crystal',
79 'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()' 79 'sulfur': 'value_sulfur'
80 } 80 }
81 def __init__(self, id, core ): 81 def __init__(self, id, core ):
82 self.core = core 82 self.core = core
83 self.id = id 83 self.id = id
84 self.params = {'view':'city','id':id} 84 self.params = {'view':'city','id':id}
86 def sync(self): 86 def sync(self):
87 page = c(self.core.baseurl).get(self.params).get_content() 87 page = c(self.core.baseurl).get(self.params).get_content()
88 parser = etree.HTMLParser(encoding='utf8') 88 parser = etree.HTMLParser(encoding='utf8')
89 page_dom = etree.parse(StringIO(page), parser) 89 page_dom = etree.parse(StringIO(page), parser)
90 90
91 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']" 91 xpath_globalinfo = '/descendant::*[@id=\'%s\']/text()'
92 for name, path in self.data_patterns.items(): 92 for name, tag_id in self.data_patterns.items():
93 xpath = xpath_globalinfo + path 93 xpath = xpath_globalinfo % (tag_id)
94 value = page_dom.xpath(xpath)[0] 94 value = page_dom.xpath(xpath)[0]
95 setattr(self, name, value) 95 setattr(self, name, value)
96 pass 96 pass
97 97
98 xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li' 98 xpath_mainview = '/descendant::div[@id=\'mainview\']/ul/li'
99 pos_doms = page_dom.xpath(xpath_mainview) 99 pos_doms = page_dom.xpath(xpath_mainview)
100 positions = [self._mk_position(pos_dom, idx) 100 positions = [self._mk_position(pos_dom, idx)
101 for idx, pos_dom in enumerate(pos_doms)] 101 for idx, pos_dom in enumerate(pos_doms)]
102 self.positions = positions 102 self.positions = positions
103 pass 103 pass