annotate pyikriam/ikariam.py @ 313:a5d85719155d

fixed the if/else logic
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Tue, 06 Jan 2009 23:51:53 +0800
parents 60c4b4b78a01
children
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
225
f3502833be7c add a gmail to get confirme url address.
kevin@tombear.tombears.com
parents: 220
diff changeset
14 def __init__(self,headers=None):
218
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
15 fakeheaders=[('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')]
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
16 if headers:
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
17 fakeheaders=fakeheaders+headers
166
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 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
19 cookie_jar = cookielib.LWPCookieJar()
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 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
21 opener = urllib2.build_opener(cookie_proc)
218
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
22 opener.addheaders = fakeheaders
166
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 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
24 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
25 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 pass
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 class Ikariam:
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 cities = {}
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 COOKIEFILE = '/tmp/ikariam.lwp'
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 def __init__(self):
218
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
34 self.browser = fake_moz()
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
35 self._cookie_jar = self.browser.cookie_jar
166
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 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
38 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
39 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40
218
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
41 urllib2.install_opener(self.browser)
166
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 = {
175
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
73 'gold': 'value_gold',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
74 'inhabitants': 'value_inhabitants',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
75 'wood': 'value_wood',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
76 'wine': 'value_wine',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
77 'marble': 'value_marble',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
78 'crystal': 'value_crystal',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
79 'sulfur': 'value_sulfur'
166
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
175
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
91 xpath_globalinfo = '/descendant::*[@id=\'%s\']/text()'
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
92 for name, tag_id in self.data_patterns.items():
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
93 xpath = xpath_globalinfo % (tag_id)
166
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
175
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
98 xpath_mainview = '/descendant::div[@id=\'mainview\']/ul/li'
166
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
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 225
diff changeset
118
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 225
diff changeset
119
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 225
diff changeset
120
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 225
diff changeset
121