Mercurial > eagle-eye
comparison pyikriam/buildings.py @ 177:6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
- We add more building types.
- All of them need to sync attributes with content of Ikariam's online page.
- These functions are refactoried to functions.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 02 Nov 2008 15:14:51 +0800 |
parents | 3ba3edda6d1e |
children | bff16e6ee3ef |
comparison
equal
deleted
inserted
replaced
176:3ba3edda6d1e | 177:6adad3bcca78 |
---|---|
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 | 5 |
5 class position(object): | 6 class position(object): |
6 def __init__(self, build_type, city_id, idx, baseurl): | 7 def __init__(self, build_type, city_id, idx, baseurl): |
7 self._baseurl = baseurl + '/index.php' | 8 self._baseurl = baseurl + '/index.php' |
8 self.build_type = build_type | 9 self.build_type = build_type |
14 pass | 15 pass |
15 | 16 |
16 def get_page(self): | 17 def get_page(self): |
17 page = c(self._baseurl).get(self._params).get_content() | 18 page = c(self._baseurl).get(self._params).get_content() |
18 return page | 19 return page |
20 | |
21 def sync(self): | |
22 page = self.get_page() | |
23 parser = etree.HTMLParser(encoding='utf8') | |
24 page_dom = etree.parse(StringIO(page), parser) | |
25 | |
26 self._sync(page_dom) | |
27 pass | |
19 pass | 28 pass |
20 | 29 |
21 class townhall(position): | 30 class building(position): |
22 class_patterns = { | 31 class_patterns = { |
23 'level': 'buildingLevel', | 32 'level': 'buildingLevel' |
24 'occupied': 'value occupied', | |
25 'rooms': 'value total', | |
26 } | |
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 } | 33 } |
40 appear_patterns = { | 34 appear_patterns = { |
41 'is_upgrading': 'isUpgrading' | 35 'is_upgrading': 'isUpgrading' |
42 } | 36 } |
43 | |
44 def __init__(self, city_id, idx, baseurl): | |
45 super(townhall, self).__init__('townhall', city_id, idx, baseurl) | |
46 pass | |
47 | 37 |
48 def _sync(self, page): | 38 def _sync(self, page_dom): |
49 parser = etree.HTMLParser(encoding='utf8') | 39 sync_tagclass(self, building.class_patterns, page_dom) |
50 page_dom = etree.parse(StringIO(page), parser) | |
51 | 40 |
52 xpath_building = '/html/body/descendant::*[@class=\'%s\']/text()' | 41 sync_tagclass_start_appear(self, building.appear_patterns, page_dom) |
53 for name, clzname in self.class_patterns.items(): | |
54 path = xpath_building % (clzname) | |
55 value = page_dom.xpath(path)[0] | |
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 | 42 |
84 xpath_upgrade = '/descendant::ul[@class=\'actions\']/li[@class=\'upgrade\']/a' | 43 xpath_upgrade = '/descendant::ul[@class=\'actions\']/li[@class=\'upgrade\']/a' |
85 anodes = page_dom.xpath(xpath_upgrade) | 44 anodes = page_dom.xpath(xpath_upgrade) |
86 if len(anodes) == 1: | 45 if len(anodes) == 1: |
87 anode = anodes[0] | 46 anode = anodes[0] |
88 self.upgrade_uri = anode.get('href') | 47 self.upgrade_uri = anode.get('href') |
89 else: | 48 else: |
90 self.upgrade_uri = None | 49 self.upgrade_uri = None |
91 pass | 50 pass |
92 pass | |
93 | 51 |
94 def sync(self): | 52 if self.is_upgrading: |
95 page = self.get_page() | 53 xpath_countdown = '/descendant::div[@id=\'upgradeCountDown\']/text()' |
96 self._sync(page) | 54 value = page_dom.xpath(xpath_countdown)[0] |
55 self.upgrade_countdown = value | |
56 else: | |
57 self.upgrade_countdown = None | |
58 pass | |
97 pass | 59 pass |
98 | 60 |
99 def upgrade(self): | 61 def upgrade(self): |
100 url = self._baseurl + self.upgrade_uri | 62 url = self._baseurl + self.upgrade_uri |
101 page = c(url).get().get_content() | 63 page = c(url).get().get_content() |
102 pass | 64 pass |
103 pass | 65 pass |
66 | |
67 class townhall(building): | |
68 class_patterns = { | |
69 'occupied': 'value occupied', | |
70 'rooms': 'value total', | |
71 } | |
72 value_patterns = { | |
73 'growth': 'growth', | |
74 'happiness': 'happiness', | |
75 'interest_base': 'base', | |
76 'interest_research': 'research1', | |
77 'interest_capital': 'capital', | |
78 'overpopulation': 'cat overpopulation' | |
79 } | |
80 count_patterns = { | |
81 'pop_citizens': 'citizens', | |
82 'pop_woodworkers': 'woodworkers', | |
83 'pop_specialworkers': 'specialworkers', | |
84 'pop_scientists': 'scientists' | |
85 } | |
86 | |
87 def __init__(self, city_id, idx, baseurl): | |
88 super(townhall, self).__init__('townhall', city_id, idx, baseurl) | |
89 pass | |
90 | |
91 def _sync(self, page_dom): | |
92 sync_tagclass(self, townhall.class_patterns, page_dom) | |
93 | |
94 sync_tagvalue(self, townhall.value_patterns, page_dom) | |
95 | |
96 sync_tagcount(self, townhall.count_patterns, page_dom) | |
97 | |
98 super(townhall, self)._sync(page_dom) | |
99 pass | |
100 pass | |
101 | |
102 class academy(building): | |
103 def __init__(self, city_id, idx, baseurl): | |
104 super(academy, self).__init__('academy', city_id, idx, baseurl) | |
105 pass | |
106 pass | |
107 | |
108 class warehouse(building): | |
109 def __init__(self, city_id, idx, baseurl): | |
110 super(warehouse, self).__init__('warehouse', city_id, idx, baseurl) | |
111 pass | |
112 pass | |
113 | |
114 class barracks(building): | |
115 def __init__(self, city_id, idx, baseurl): | |
116 super(barracks, self).__init__('barracks', city_id, idx, baseurl) | |
117 pass | |
118 pass | |
119 | |
120 class branchoffice(building): | |
121 def __init__(self, city_id, idx, baseurl): | |
122 super(branchoffice, self).__init__('branchoffice', city_id, idx, baseurl) | |
123 pass | |
124 pass | |
125 | |
126 class port(building): | |
127 def __init__(self, city_id, idx, baseurl): | |
128 super(port, self).__init__('port', city_id, idx, baseurl) | |
129 pass | |
130 pass | |
131 | |
132 class wall(building): | |
133 def __init__(self, city_id, idx, baseurl): | |
134 super(wall, self).__init__('wall', city_id, idx, baseurl) | |
135 pass | |
136 pass | |
137 |