changeset 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 7150e7b3dbec
files pyikriam/buildings.py pyikriam/example.py pyikriam/sync_utils.py
diffstat 3 files changed, 69 insertions(+), 8 deletions(-) [+]
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
--- a/pyikriam/example.py	Sun Nov 02 16:33:01 2008 +0800
+++ b/pyikriam/example.py	Sun Nov 02 22:31:06 2008 +0800
@@ -22,7 +22,6 @@
 print 'marble is ' + city.marble
 print 'crystal is ' + city.crystal
 print 'sulfur is ' + city.sulfur
-print 'positions ' + repr(city.positions)
 
 for idx, pos in enumerate(city.positions):
     if not isinstance(pos, buildings.position):
--- a/pyikriam/sync_utils.py	Sun Nov 02 16:33:01 2008 +0800
+++ b/pyikriam/sync_utils.py	Sun Nov 02 22:31:06 2008 +0800
@@ -2,6 +2,8 @@
 # \brief Sync information of objects with DOM trees of respective pages.
 #
 
+import re as _re
+
 def sync_tagclass(obj, patterns, page_dom):
     xpath_building = '/html/body/descendant::*[@class=\'%s\']/text()'
     for name, clzname in patterns.items():
@@ -42,3 +44,29 @@
         pass
     pass
 
+_reo_tv = _re.compile(u'(([0-9]+)\u6642)? ?(([0-9]+)\u5206)? ?(([0-9]+)\u79d2)?')
+## \brief Translate timeval in Chinese text format to integer seconds.
+#
+def ikariam_zh_timeval(tv_str):
+    tmo = _reo_tv.match(tv_str)
+    if not tmo:
+        raise SyntaxError, \
+            '%s is an invalid time interval string' % (repr(tv_str))
+    tv = 0
+
+    value = tmo.group(2)        # hour
+    if value:
+        tv = tv + int(value) * 360
+        pass
+    
+    value = tmo.group(4)        # minute
+    if value:
+        tv = tv + int(value) * 60
+        pass
+    
+    value = tmo.group(6)        # second
+    if value:
+        tv = tv + int(value)
+        pass
+
+    return tv