Mercurial > eagle-eye
annotate pyikriam/ikariam.py @ 179:8afd380c20ac
do not fight with friends.
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Sun, 02 Nov 2008 21:15:51 +0800 |
parents | 8f699a9da6c0 |
children | 9f248c8460ce |
rev | line source |
---|---|
166
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
1 from lazy.www import c |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
2 from lconf import LoadConfigfile |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
3 import cookielib |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
4 import os |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
5 import urllib2 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
6 import urllib |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
7 from utils import dyna_prog, decorator |
168
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
8 from lxml import etree |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
9 from StringIO import StringIO |
166
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
10 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
11 class fake_moz(object): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
12 __metaclass__ = decorator |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
14 def __init__(self): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
15 super(fake_moz, self).__init__() |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
16 cookie_jar = cookielib.LWPCookieJar() |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
17 cookie_proc = urllib2.HTTPCookieProcessor(cookie_jar) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
18 opener = urllib2.build_opener(cookie_proc) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
19 opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.1.12pre) Gecko/20071220 BonEcho/2.0.0.12pre')] |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
20 fake_moz.set_backend(self, opener) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
21 self.cookie_jar = cookie_jar |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
22 pass |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
23 pass |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
24 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
25 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
26 class Ikariam: |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
27 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
28 cities = {} |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
29 COOKIEFILE = '/tmp/ikariam.lwp' |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
30 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
31 def __init__(self): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
32 browser = fake_moz() |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
33 self.browser = browser |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
34 self._cookie_jar = browser.cookie_jar |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
35 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
36 if os.path.isfile(self.COOKIEFILE): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
37 self._cookie_jar.load(self.COOKIEFILE) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
38 pass |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
39 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
40 urllib2.install_opener(browser) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
41 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
42 self.confdata=LoadConfigfile().cd |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
43 self.baseurl='http://'+self.confdata['server'] |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
44 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
45 self.login() |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
46 pass |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
47 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
48 def login(self): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
49 print "login to %s...." % self.confdata['server'] |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
50 params = {"universe":self.confdata['server'], \ |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
51 "name":self.confdata['user'], \ |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
52 "password":self.confdata['pass']} |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
53 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content() |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
54 self._cookie_jar.save(self.COOKIEFILE) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
55 pass |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
56 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
57 def logout(self): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
58 print "logut from %s...." % self.confdata['server'] |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
59 c(self.baseurl+'/index.php?action=loginAvatar&function=logout') |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
60 os.remove(self.COOKIEFILE) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
61 pass |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
62 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
63 ## |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
64 # \note We can cache data with decorator 'dynamic programming'. |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
65 # |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
66 @dyna_prog |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
67 def city(self, id): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
68 return IkariamCity(id=id, core=self) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
69 pass |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
70 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
71 class IkariamCity: |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
72 data_patterns = { |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
73 'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()', |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
74 'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()', |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
75 'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()', |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
76 'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()', |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
77 'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()', |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
78 'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()', |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
79 'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()' |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
80 } |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
81 def __init__(self, id, core ): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
82 self.core = core |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
83 self.id = id |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
84 self.params = {'view':'city','id':id} |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
85 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
86 def sync(self): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
87 page = c(self.core.baseurl).get(self.params).get_content() |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
88 parser = etree.HTMLParser(encoding='utf8') |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
89 page_dom = etree.parse(StringIO(page), parser) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
90 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
91 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']" |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
92 for name, path in self.data_patterns.items(): |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
93 xpath = xpath_globalinfo + path |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
94 value = page_dom.xpath(xpath)[0] |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
95 setattr(self, name, value) |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
96 pass |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
97 |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
98 xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li' |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
99 pos_doms = page_dom.xpath(xpath_mainview) |
168
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
100 positions = [self._mk_position(pos_dom, idx) |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
101 for idx, pos_dom in enumerate(pos_doms)] |
166
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
102 self.positions = positions |
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
103 pass |
168
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
104 |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
105 def _mk_position(self, pos_dom, idx): |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
106 import buildings |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
107 |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
108 build_type = pos_dom.get('class').split()[-1].lower() |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
109 if hasattr(buildings, build_type): |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
110 clz = getattr(buildings, build_type) |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
111 if issubclass(clz, buildings.position): |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
112 building = clz(self.id, idx, self.core.baseurl) |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
113 return building |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
114 pass |
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
115 return build_type |
166
f7fd2738b9b4
Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
116 pass |
168
8f699a9da6c0
Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents:
166
diff
changeset
|
117 |