comparison pyikriam/buildings.py @ 176:3ba3edda6d1e

Extract more information for building townhall.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 11:32:59 +0800
parents 9f248c8460ce
children 6adad3bcca78
comparison
equal deleted inserted replaced
175:9f248c8460ce 176:3ba3edda6d1e
17 page = c(self._baseurl).get(self._params).get_content() 17 page = c(self._baseurl).get(self._params).get_content()
18 return page 18 return page
19 pass 19 pass
20 20
21 class townhall(position): 21 class townhall(position):
22 xpath_patterns = { 22 class_patterns = {
23 'level': 'div[@class=\'buildingLevel\']/text()' 23 'level': 'buildingLevel',
24 'occupied': 'value occupied',
25 'rooms': 'value total',
24 } 26 }
25 27 value_patterns = {
28 'growth': 'growth',
29 'happiness': 'happiness',
30 'interest_base': 'base',
31 'interest_research': 'research1',
32 'interest_capital': 'capital'
33 }
34 count_patterns = {
35 'pop_citizens': 'citizens',
36 'pop_woodworkers': 'woodworkers',
37 'pop_specialworkers': 'specialworkers',
38 'pop_scientists': 'scientists'
39 }
40 appear_patterns = {
41 'is_upgrading': 'isUpgrading'
42 }
43
26 def __init__(self, city_id, idx, baseurl): 44 def __init__(self, city_id, idx, baseurl):
27 super(townhall, self).__init__('townhall', city_id, idx, baseurl) 45 super(townhall, self).__init__('townhall', city_id, idx, baseurl)
28 pass 46 pass
29 47
30 def _sync(self, page): 48 def _sync(self, page):
31 parser = etree.HTMLParser(encoding='utf8') 49 parser = etree.HTMLParser(encoding='utf8')
32 page_dom = etree.parse(StringIO(page), parser) 50 page_dom = etree.parse(StringIO(page), parser)
33 xpath_building = '/html/body/descendant::' 51
34 for name, ptn in self.xpath_patterns.items(): 52 xpath_building = '/html/body/descendant::*[@class=\'%s\']/text()'
35 path = xpath_building + ptn 53 for name, clzname in self.class_patterns.items():
54 path = xpath_building % (clzname)
36 value = page_dom.xpath(path)[0] 55 value = page_dom.xpath(path)[0]
37 setattr(self, name, value) 56 setattr(self, name, value)
57 pass
58
59 xpath_value = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'value\']/text()'
60 for name, clzname in self.value_patterns.items():
61 path = xpath_value % (clzname)
62 value = page_dom.xpath(path)[0]
63 setattr(self, name, value)
64 pass
65
66 xpath_count = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'count\']/text()'
67 for name, clzname in self.count_patterns.items():
68 path = xpath_count % (clzname)
69 value = page_dom.xpath(path)[0]
70 setattr(self, name, value)
71 pass
72
73 xpath_appear = '/html/body/descendant::*[starts-with(@class,\'%s\')]'
74 for name, clzname in self.appear_patterns.items():
75 path = xpath_appear % (clzname)
76 cnt = len(page_dom.xpath(path))
77 if cnt != 0:
78 setattr(self, name, True)
79 else:
80 setattr(self, name, False)
81 pass
82 pass
83
84 xpath_upgrade = '/descendant::ul[@class=\'actions\']/li[@class=\'upgrade\']/a'
85 anodes = page_dom.xpath(xpath_upgrade)
86 if len(anodes) == 1:
87 anode = anodes[0]
88 self.upgrade_uri = anode.get('href')
89 else:
90 self.upgrade_uri = None
38 pass 91 pass
39 pass 92 pass
40 93
41 def sync(self): 94 def sync(self):
42 page = self.get_page() 95 page = self.get_page()
43 self._sync(page) 96 self._sync(page)
44 pass 97 pass
98
99 def upgrade(self):
100 url = self._baseurl + self.upgrade_uri
101 page = c(url).get().get_content()
102 pass
45 pass 103 pass