annotate pyikriam/buildings.py @ 376:6ca0677a361e

refiend output format and fixed timing issue.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Tue, 14 Apr 2009 17:02:17 +0800
parents 60c4b4b78a01
children
rev   line source
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
1 from lazy.www import c, Resource
168
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 from lxml import etree
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 from StringIO import StringIO
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
4 from sync_utils import sync_tagclass, sync_tagvalue
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
5 from sync_utils import sync_tagcount, sync_tagclass_start_appear
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
6 from sync_utils import ikariam_zh_timeval
168
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 class position(object):
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 def __init__(self, build_type, city_id, idx, baseurl):
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 self._baseurl = baseurl + '/index.php'
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 self.build_type = build_type
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 self.city_id = city_id
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 self.idx = idx
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 self._params = {'view': 'buildingGround',
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 'id': city_id,
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 'position': idx}
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 pass
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 def get_page(self):
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 page = c(self._baseurl).get(self._params).get_content()
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 return page
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
22
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
23 def sync(self):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
24 page = self.get_page()
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
25 parser = etree.HTMLParser(encoding='utf8')
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
26 page_dom = etree.parse(StringIO(page), parser)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
27
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
28 self._sync(page_dom)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
29 pass
168
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 pass
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
32 ## \brief Base class of all building class.
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
33 #
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
34 # This class extract information from page of building. That are
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
35 # information all buildings have.
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
36 #
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
37 class building(position):
176
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
38 class_patterns = {
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
39 'level': 'buildingLevel'
176
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
40 }
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
41 appear_patterns = {
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
42 'is_upgrading': 'isUpgrading'
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
43 }
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
44 upgrade_res_patterns = {
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
45 'wood': 'wood',
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
46 'marble': 'marble',
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
47 'crystal': 'glass'
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
48 }
176
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
49
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
50 def _sync(self, page_dom):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
51 sync_tagclass(self, building.class_patterns, page_dom)
176
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
52
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
53 sync_tagclass_start_appear(self, building.appear_patterns, page_dom)
176
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
54
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
55 xpath_upgrade = '/descendant::ul[@class=\'actions\']/li[@class=\'upgrade\']/a'
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
56 anodes = page_dom.xpath(xpath_upgrade)
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
57 if len(anodes) == 1:
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
58 anode = anodes[0]
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
59 self._upgrade_uri = anode.get('href')
176
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
60 else:
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
61 self._upgrade_uri = None
176
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
62 pass
168
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
64 self.upgrade_wood = 0
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
65 self.upgrade_marble = 0
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
66 self.upgrade_crystal = 0
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
67 self.upgrade_time = 0
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
68 self.upgrade_countdown = 0
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
69
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
70 if self.is_upgrading:
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
71 xpath_countdown = '/descendant::div[@id=\'upgradeCountDown\']/text()'
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
72 value = page_dom.xpath(xpath_countdown)[0]
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
73 self.upgrade_countdown = ikariam_zh_timeval(value)
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
74 else:
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
75 xpath_res = '/descendant::div[@class=\'content\']/ul[@class=\'resources\']/li[starts-with(@class, \'%s\')]/text()'
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
76
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
77 for resname, clzname in building.upgrade_res_patterns.items():
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
78 xpath = xpath_res % (clzname)
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
79 txts = page_dom.xpath(xpath)
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
80 if len(txts) == 1:
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
81 value = txts[0].strip()
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
82 setattr(self, 'upgrade_' + resname, int(value))
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
83 pass
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
84 pass
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
85
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
86 xpath_time = xpath_res % ('time')
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
87 txts = page_dom.xpath(xpath_time)
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
88 if len(txts) == 1:
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
89 value = txts[0].strip()
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
90 self.upgrade_time = ikariam_zh_timeval(value)
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
91 pass
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
92 pass
168
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 pass
176
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
94
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
95 def upgrade(self):
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
96 url = self._baseurl + self._upgrade_uri
176
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
97 page = c(url).get().get_content()
3ba3edda6d1e Extract more information for building townhall.
Thinker K.F. Li <thinker@branda.to>
parents: 175
diff changeset
98 pass
168
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 pass
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
100
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
101 class townhall(building):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
102 class_patterns = {
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
103 'occupied': 'value occupied',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
104 'rooms': 'value total',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
105 }
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
106 value_patterns = {
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
107 'growth': 'growth',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
108 'happiness': 'happiness',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
109 'interest_base': 'base',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
110 'interest_research': 'research1',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
111 'interest_capital': 'capital',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
112 'overpopulation': 'cat overpopulation'
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
113 }
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
114 count_patterns = {
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
115 'pop_citizens': 'citizens',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
116 'pop_woodworkers': 'woodworkers',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
117 'pop_specialworkers': 'specialworkers',
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
118 'pop_scientists': 'scientists'
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
119 }
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
120
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
121 def __init__(self, city_id, idx, baseurl):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
122 super(townhall, self).__init__('townhall', city_id, idx, baseurl)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
123 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
124
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
125 def _sync(self, page_dom):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
126 sync_tagclass(self, townhall.class_patterns, page_dom)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
127
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
128 sync_tagvalue(self, townhall.value_patterns, page_dom)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
129
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
130 sync_tagcount(self, townhall.count_patterns, page_dom)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
131
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
132 super(townhall, self)._sync(page_dom)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
133 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
134 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
135
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
136 class academy(building):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
137 def __init__(self, city_id, idx, baseurl):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
138 super(academy, self).__init__('academy', city_id, idx, baseurl)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
139 pass
178
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
140
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
141 def _sync(self, page_dom):
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
142 xpath_research_name = '/descendant::*[@class=\'researchName\']/a'
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
143 anodes = page_dom.xpath(xpath_research_name)
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
144 if len(anodes) == 1:
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
145 anode = anodes[0]
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
146 self.researching = anode.get('title')
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
147 xpath_countdown = '/descendant::div[@id=\'researchCountDown\']/text()'
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
148 txtnodes = page_dom.xpath(xpath_countdown)
188
bf4ddf5bffb9 Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents: 178
diff changeset
149 self.researching_countdown = ikariam_zh_timeval(txtnodes[0])
178
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
150 else:
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
151 self.researching = None
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
152 self.researching_countdown = None
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
153 pass
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
154
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
155 super(academy, self)._sync(page_dom)
bff16e6ee3ef Extract status of reearching for Academy.
Thinker K.F. Li <thinker@branda.to>
parents: 177
diff changeset
156 pass
177
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
157 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
158
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
159 class warehouse(building):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
160 def __init__(self, city_id, idx, baseurl):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
161 super(warehouse, self).__init__('warehouse', city_id, idx, baseurl)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
162 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
163 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
164
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
165 class barracks(building):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
166 def __init__(self, city_id, idx, baseurl):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
167 super(barracks, self).__init__('barracks', city_id, idx, baseurl)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
168 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
169 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
170
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
171 class branchoffice(building):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
172 def __init__(self, city_id, idx, baseurl):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
173 super(branchoffice, self).__init__('branchoffice', city_id, idx, baseurl)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
174 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
175 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
176
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
177 class port(building):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
178 def __init__(self, city_id, idx, baseurl):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
179 super(port, self).__init__('port', city_id, idx, baseurl)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
180 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
181 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
182
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
183 class wall(building):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
184 def __init__(self, city_id, idx, baseurl):
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
185 super(wall, self).__init__('wall', city_id, idx, baseurl)
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
186 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
187 pass
6adad3bcca78 Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents: 176
diff changeset
188
213
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
189 class shipyard(building):
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
190 def __init__(self, city_id, idx, baseurl):
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
191 super(shipyard, self).__init__('shipyard', city_id, idx, baseurl)
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
192 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
193 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
194
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
195 class safehouse(building):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
196 def __init__(self, city_id, idx, baseurl):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
197 super(shipyard, self).__init__('safehouse', city_id, idx, baseurl)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
198 self.data_patterns = {
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
199 'vacancy_spys_total':"/::input[@id='spyCount']/@value",
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
200 'trained_spys_total':"/descendant::div[@id='mainview']/div[4]/div[1]/p/span/span[2]/text()"}
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
201 pass
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
202
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
203 def __sync__(self, page_dom):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
204 workflow = create_workflow('find', page_dom)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
205 attrs = workflow.findall(self.data_patterns).get_content()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
206 for attr_name, attr_value in attrs:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
207 setattr(self, attr_name, attr_value)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
208 pass
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
209 pass
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
210
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
211 def _mk_spy(self):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
212 import spys
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 213
diff changeset
213
213
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
214 class empty_pos(position):
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
215 res_patterns = {
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
216 'wood': 'wood',
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
217 'marble': 'marble',
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
218 'crystal': 'glass'
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
219 }
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
220
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
221 def _sync(self, page_dom):
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
222 self.building_info = None
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
223 self.res_wood = 0
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
224 self.res_marble = 0
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
225 self.res_crystal = 0
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
226 self.building_time = 0
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
227 self._building_uri = None
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
228
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
229 xpath_building = '/descendant::div[@class=\'buildinginfo\']/h4/text()'
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
230 buildings = page_dom.xpath(xpath_building)
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
231 if len(buildings) == 1:
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
232 self.building_info = buildings[0]
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
233 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
234
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
235 xpath_costs = '/descendant::div[@class=\'costs\']/ul/li[@class=\'%s\']/text()'
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
236 for res, ptn in empty_pos.res_patterns.items():
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
237 xpath = xpath_costs % (ptn)
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
238 txts = page_dom.xpath(xpath)
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
239 if len(txts) == 1:
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
240 value = int(txts[0])
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
241 setattr(self, 'res_' + res, value)
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
242 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
243 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
244
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
245 xpath = xpath_costs % ('time')
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
246 txts = page_dom.xpath(xpath)
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
247 if len(txts) == 1:
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
248 value = ikariam_zh_timeval(txts[0])
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
249 self.building_time = value
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
250 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
251
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
252 xpath_button = '/descendant::a[@class=\'button build\']'
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
253 anodes = page_dom.xpath(xpath_button)
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
254 if len(anodes) == 1:
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
255 self._building_uri = anodes[0].get('href')
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
256 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
257 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
258
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
259 def build(self):
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
260 url = self._baseurl + self._building_uri
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
261 page = c(url).get().get_content()
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
262 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
263 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
264
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
265 class land(empty_pos):
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
266 def __init__(self, city_id, idx, baseurl):
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
267 super(land, self).__init__('land', city_id, idx, baseurl)
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
268 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
269 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
270
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
271 class shore(empty_pos):
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
272 def __init__(self, city_id, idx, baseurl):
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
273 super(shore, self).__init__('shore', city_id, idx, baseurl)
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
274 pass
550e20dd7573 Handle more building type
Thinker K.F. Li <thinker@branda.to>
parents: 188
diff changeset
275 pass