annotate pyikriam/ikariam.py @ 166:f7fd2738b9b4

Move Ikariam out of __init__.py to ikariam.py.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 00:59:00 +0800
parents
children 8f699a9da6c0
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
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 class fake_moz(object):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 __metaclass__ = decorator
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 def __init__(self):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 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
14 cookie_jar = cookielib.LWPCookieJar()
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 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
16 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
17 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
18 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
19 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
20 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 class Ikariam:
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 cities = {}
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 COOKIEFILE = '/tmp/ikariam.lwp'
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 def __init__(self):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 browser = fake_moz()
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 self.browser = browser
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 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
33
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 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
35 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
36 pass
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 urllib2.install_opener(browser)
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 self.confdata=LoadConfigfile().cd
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 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
42
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 self.login()
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 pass
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 def login(self):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 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
48 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
49 "name":self.confdata['user'], \
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 "password":self.confdata['pass']}
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 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
52 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
53 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 def logout(self):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 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
57 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
58 os.remove(self.COOKIEFILE)
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 ##
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 # \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
63 #
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 @dyna_prog
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 def city(self, id):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 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
67 pass
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 class IkariamCity:
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 data_patterns = {
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 '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
72 '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
73 '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
74 '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
75 '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
76 '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
77 '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
78 }
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 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
80 self.core = core
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 self.id = id
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 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
83
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 def sync(self):
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 from lxml import etree
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 from StringIO import StringIO
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87
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
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 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
93 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
94 xpath = xpath_globalinfo + path
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
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 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
100 pos_doms = page_dom.xpath(xpath_mainview)
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 positions = [pos_dom.get('class').split()[-1] for pos_dom in pos_doms]
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
f7fd2738b9b4 Move Ikariam out of __init__.py to ikariam.py.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 pass