diff 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
line wrap: on
line diff
--- a/pyikriam/ikariam.py	Sun Nov 02 04:12:23 2008 +0800
+++ b/pyikriam/ikariam.py	Sun Nov 02 09:59:51 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)]