comparison pyikriam/buildings.py @ 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 550e20dd7573
comparison
equal deleted inserted replaced
178:bff16e6ee3ef 188:bf4ddf5bffb9
1 from lazy.www import c 1 from lazy.www import c
2 from lxml import etree 2 from lxml import etree
3 from StringIO import StringIO 3 from StringIO import StringIO
4 from sync_utils import * 4 from sync_utils import sync_tagclass, sync_tagvalue
5 from sync_utils import sync_tagcount, sync_tagclass_start_appear
6 from sync_utils import ikariam_zh_timeval
5 7
6 class position(object): 8 class position(object):
7 def __init__(self, build_type, city_id, idx, baseurl): 9 def __init__(self, build_type, city_id, idx, baseurl):
8 self._baseurl = baseurl + '/index.php' 10 self._baseurl = baseurl + '/index.php'
9 self.build_type = build_type 11 self.build_type = build_type
25 27
26 self._sync(page_dom) 28 self._sync(page_dom)
27 pass 29 pass
28 pass 30 pass
29 31
32 ## \brief Base class of all building class.
33 #
34 # This class extract information from page of building. That are
35 # information all buildings have.
36 #
30 class building(position): 37 class building(position):
31 class_patterns = { 38 class_patterns = {
32 'level': 'buildingLevel' 39 'level': 'buildingLevel'
33 } 40 }
34 appear_patterns = { 41 appear_patterns = {
35 'is_upgrading': 'isUpgrading' 42 'is_upgrading': 'isUpgrading'
43 }
44 upgrade_res_patterns = {
45 'wood': 'wood',
46 'marble': 'marble',
47 'crystal': 'glass'
36 } 48 }
37 49
38 def _sync(self, page_dom): 50 def _sync(self, page_dom):
39 sync_tagclass(self, building.class_patterns, page_dom) 51 sync_tagclass(self, building.class_patterns, page_dom)
40 52
42 54
43 xpath_upgrade = '/descendant::ul[@class=\'actions\']/li[@class=\'upgrade\']/a' 55 xpath_upgrade = '/descendant::ul[@class=\'actions\']/li[@class=\'upgrade\']/a'
44 anodes = page_dom.xpath(xpath_upgrade) 56 anodes = page_dom.xpath(xpath_upgrade)
45 if len(anodes) == 1: 57 if len(anodes) == 1:
46 anode = anodes[0] 58 anode = anodes[0]
47 self.upgrade_uri = anode.get('href') 59 self._upgrade_uri = anode.get('href')
48 else: 60 else:
49 self.upgrade_uri = None 61 self._upgrade_uri = None
50 pass 62 pass
63
64 self.upgrade_wood = 0
65 self.upgrade_marble = 0
66 self.upgrade_crystal = 0
67 self.upgrade_time = 0
68 self.upgrade_countdown = 0
51 69
52 if self.is_upgrading: 70 if self.is_upgrading:
53 xpath_countdown = '/descendant::div[@id=\'upgradeCountDown\']/text()' 71 xpath_countdown = '/descendant::div[@id=\'upgradeCountDown\']/text()'
54 value = page_dom.xpath(xpath_countdown)[0] 72 value = page_dom.xpath(xpath_countdown)[0]
55 self.upgrade_countdown = value 73 self.upgrade_countdown = ikariam_zh_timeval(value)
56 else: 74 else:
57 self.upgrade_countdown = None 75 xpath_res = '/descendant::div[@class=\'content\']/ul[@class=\'resources\']/li[starts-with(@class, \'%s\')]/text()'
76
77 for resname, clzname in building.upgrade_res_patterns.items():
78 xpath = xpath_res % (clzname)
79 txts = page_dom.xpath(xpath)
80 if len(txts) == 1:
81 value = txts[0].strip()
82 setattr(self, 'upgrade_' + resname, int(value))
83 pass
84 pass
85
86 xpath_time = xpath_res % ('time')
87 txts = page_dom.xpath(xpath_time)
88 if len(txts) == 1:
89 value = txts[0].strip()
90 self.upgrade_time = ikariam_zh_timeval(value)
91 pass
58 pass 92 pass
59 pass 93 pass
60 94
61 def upgrade(self): 95 def upgrade(self):
62 url = self._baseurl + self.upgrade_uri 96 url = self._baseurl + self._upgrade_uri
63 page = c(url).get().get_content() 97 page = c(url).get().get_content()
64 pass 98 pass
65 pass 99 pass
66 100
67 class townhall(building): 101 class townhall(building):
110 if len(anodes) == 1: 144 if len(anodes) == 1:
111 anode = anodes[0] 145 anode = anodes[0]
112 self.researching = anode.get('title') 146 self.researching = anode.get('title')
113 xpath_countdown = '/descendant::div[@id=\'researchCountDown\']/text()' 147 xpath_countdown = '/descendant::div[@id=\'researchCountDown\']/text()'
114 txtnodes = page_dom.xpath(xpath_countdown) 148 txtnodes = page_dom.xpath(xpath_countdown)
115 self.researching_countdown = txtnodes[0] 149 self.researching_countdown = ikariam_zh_timeval(txtnodes[0])
116 else: 150 else:
117 self.researching = None 151 self.researching = None
118 self.researching_countdown = None 152 self.researching_countdown = None
119 pass 153 pass
120 154