changeset 168:8f699a9da6c0

Extract building level of townhalls. - townhall class is responding to extract information about townhall.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 02:26:28 +0800
parents 6ab01f709650
children 7df753a99926
files pyikriam/buildings.py pyikriam/example.py pyikriam/ikariam.py
diffstat 3 files changed, 64 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/buildings.py	Sun Nov 02 02:26:28 2008 +0800
@@ -0,0 +1,45 @@
+from lazy.www import c
+from lxml import etree
+from StringIO import StringIO
+
+class position(object):
+    def __init__(self, build_type, city_id, idx, baseurl):
+        self._baseurl = baseurl + '/index.php'
+        self.build_type = build_type
+        self.city_id = city_id
+        self.idx = idx
+        self._params = {'view': 'buildingGround',
+                        'id': city_id,
+                        'position': idx}
+        pass
+
+    def get_page(self):
+        page = c(self._baseurl).get(self._params).get_content()
+        return page
+    pass
+
+class townhall(position):
+    xpath_patterns = {
+        'level': '/div/div/div[@class=\'buildingLevel\']/text()'
+        }
+
+    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
+        pass
+
+    def sync(self):
+        page = self.get_page()
+        self._sync(page)
+        pass
+    pass
--- a/pyikriam/example.py	Sun Nov 02 01:00:35 2008 +0800
+++ b/pyikriam/example.py	Sun Nov 02 02:26:28 2008 +0800
@@ -23,3 +23,5 @@
 print 'sulfur is ' + city.sulfur
 print 'positions ' + repr(city.positions)
 
+city.positions[0].sync()
+print 'positions[0] level is ' + str(city.positions[0].level)
--- a/pyikriam/ikariam.py	Sun Nov 02 01:00:35 2008 +0800
+++ b/pyikriam/ikariam.py	Sun Nov 02 02:26:28 2008 +0800
@@ -5,6 +5,8 @@
 import urllib2
 import urllib
 from utils import dyna_prog, decorator
+from lxml import etree
+from StringIO import StringIO
 
 class fake_moz(object):
     __metaclass__ = decorator
@@ -82,9 +84,6 @@
         self.params = {'view':'city','id':id}
         
     def sync(self):
-        from lxml import etree
-        from StringIO import StringIO
-
         page = c(self.core.baseurl).get(self.params).get_content()
         parser = etree.HTMLParser(encoding='utf8')
         page_dom = etree.parse(StringIO(page), parser)
@@ -98,7 +97,21 @@
         
         xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li'
         pos_doms = page_dom.xpath(xpath_mainview)
-        positions = [pos_dom.get('class').split()[-1] for pos_dom in pos_doms]
+        positions = [self._mk_position(pos_dom, idx)
+                     for idx, pos_dom in enumerate(pos_doms)]
         self.positions = positions
         pass
+
+    def _mk_position(self, pos_dom, idx):
+        import buildings
+
+        build_type = pos_dom.get('class').split()[-1].lower()
+        if hasattr(buildings, build_type):
+            clz = getattr(buildings, build_type)
+            if issubclass(clz, buildings.position):
+                building = clz(self.id, idx, self.core.baseurl)
+                return building
+            pass
+        return build_type
     pass
+