annotate pyikriam/ikariam.py @ 218:785c89e5db32

Remove those file, and change the addheader from fake moz
author kevin@localhost.localdomain
date Tue, 04 Nov 2008 16:01:25 +0800
parents 9f248c8460ce
children 7107c26ba197
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
218
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
14 def __init__(self,headers=0):
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
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 class Ikariam:
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 cities = {}
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 COOKIEFILE = '/tmp/ikariam.lwp'
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 def __init__(self):
218
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
35 self.browser = fake_moz()
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
36 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
37
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 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
39 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
40 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41
218
785c89e5db32 Remove those file, and change the addheader from fake moz
kevin@localhost.localdomain
parents: 175
diff changeset
42 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
43 self.confdata=LoadConfigfile().cd
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 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
45
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 self.login()
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 def login(self):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 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
51 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
52 "name":self.confdata['user'], \
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 "password":self.confdata['pass']}
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 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
55 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
56 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 def logout(self):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 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
60 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
61 os.remove(self.COOKIEFILE)
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 pass
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 ##
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 # \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
66 #
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 @dyna_prog
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 def city(self, id):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 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
70 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 class IkariamCity:
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 data_patterns = {
175
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
74 'gold': 'value_gold',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
75 'inhabitants': 'value_inhabitants',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
76 'wood': 'value_wood',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
77 'wine': 'value_wine',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
78 'marble': 'value_marble',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
79 'crystal': 'value_crystal',
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
80 'sulfur': 'value_sulfur'
166
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 }
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 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
83 self.core = core
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 self.id = id
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 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
86
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 def sync(self):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 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
89 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
90 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
91
175
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
92 xpath_globalinfo = '/descendant::*[@id=\'%s\']/text()'
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
93 for name, tag_id in self.data_patterns.items():
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
94 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
95 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
96 setattr(self, name, value)
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98
175
9f248c8460ce Simplize xpath patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 168
diff changeset
99 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
100 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
101 positions = [self._mk_position(pos_dom, idx)
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
102 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
103 self.positions = positions
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 pass
168
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
105
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
106 def _mk_position(self, pos_dom, idx):
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
107 import buildings
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
108
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
109 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
110 if hasattr(buildings, build_type):
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
111 clz = getattr(buildings, build_type)
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
112 if issubclass(clz, buildings.position):
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
113 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
114 return building
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
115 pass
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
116 return build_type
166
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 pass
168
8f699a9da6c0 Extract building level of townhalls.
Thinker K.F. Li <thinker@branda.to>
parents: 166
diff changeset
118