diff pyikriam/__init__.py @ 161:1507c2d16b35

Extract wood, marble, crystal, sulfure, and inhabitants information.
author Thinker K.F. Li <thinker@branda.to>
date Sat, 01 Nov 2008 22:21:25 +0800
parents 7551342718b6
children e49137521123
line wrap: on
line diff
--- a/pyikriam/__init__.py	Sat Nov 01 21:29:51 2008 +0800
+++ b/pyikriam/__init__.py	Sat Nov 01 22:21:25 2008 +0800
@@ -67,16 +67,33 @@
     pass
 
 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()'
+        }
     def __init__(self, id, core ):
         self.core = core
         self.id = id
         self.params = {'view':'city','id':id}
         
     def sync(self):
+        from lxml import etree
+        from StringIO import StringIO
+
 	print "pull datas of the city %s" % self.id
-        xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul"
+        xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']"
 
-        xpath_gold = xpath_globalinfo + "/li[2]/a/span[@id='value_gold']/text()"
-        self.gold = c(self.core.baseurl).get(self.params).find(xpath_gold).get_content()[0]
-        
+        page = c(self.core.baseurl).get(self.params).get_content()
+        parser = etree.HTMLParser(encoding='utf8')
+        page_dom = etree.parse(StringIO(page), parser)
+        for name, path in self.data_patterns.items():
+            xpath = xpath_globalinfo + path
+            value = page_dom.xpath(xpath)[0]
+            setattr(self, name, value)
+        pass
+    pass