annotate pyikriam/__init__.py @ 172:ba03aa8abdb2

reading Tavern's infomration
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 02 Nov 2008 00:30:02 +0800
parents e49137521123
children f7fd2738b9b4
rev   line source
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
1 from lazy.www import c
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
2 from lconf import LoadConfigfile
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
3 import cookielib
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
4 import os
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
5 import urllib2
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
6 import urllib
160
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
7 from utils import dyna_prog, decorator
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
8
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
9 class fake_moz(object):
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
10 __metaclass__ = decorator
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
11
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
12 def __init__(self):
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
13 super(fake_moz, self).__init__()
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
14 cookie_jar = cookielib.LWPCookieJar()
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
15 cookie_proc = urllib2.HTTPCookieProcessor(cookie_jar)
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
16 opener = urllib2.build_opener(cookie_proc)
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
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')]
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
18 fake_moz.set_backend(self, opener)
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
19 self.cookie_jar = cookie_jar
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
20 pass
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
21 pass
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
22
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
23
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
24 class Ikariam:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
25
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
26 cities = {}
160
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
27 COOKIEFILE = '/tmp/ikariam.lwp'
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
28
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
29 def __init__(self):
160
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
30 browser = fake_moz()
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
31 self.browser = browser
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
32 self._cookie_jar = browser.cookie_jar
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
33
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
34 if os.path.isfile(self.COOKIEFILE):
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
35 self._cookie_jar.load(self.COOKIEFILE)
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
36 pass
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
37
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
38 urllib2.install_opener(browser)
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
39
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
40 self.confdata=LoadConfigfile().cd
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
41 self.baseurl='http://'+self.confdata['server']
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
42
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
43 self.login()
160
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
44 pass
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
45
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
46 def login(self):
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
47 print "login to %s...." % self.confdata['server']
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
48 params = {"universe":self.confdata['server'], \
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
49 "name":self.confdata['user'], \
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
50 "password":self.confdata['pass']}
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
51 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
160
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
52 self._cookie_jar.save(self.COOKIEFILE)
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
53 pass
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
54
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
55 def logout(self):
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
56 print "logut from %s...." % self.confdata['server']
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
57 c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
58 os.remove(self.COOKIEFILE)
160
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
59 pass
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
60
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
61 ##
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
62 # \note We can cache data with decorator 'dynamic programming'.
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
63 #
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
64 @dyna_prog
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
65 def city(self, id):
160
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
66 return IkariamCity(id=id, core=self)
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
67 pass
7551342718b6 Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
68
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
69 class IkariamCity:
161
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
70 data_patterns = {
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
71 'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()',
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
72 'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()',
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
73 'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()',
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
74 'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()',
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
75 'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()',
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
76 'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()',
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
77 'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()'
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
78 }
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
79 def __init__(self, id, core ):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
80 self.core = core
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
81 self.id = id
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
82 self.params = {'view':'city','id':id}
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
83
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
84 def sync(self):
161
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
85 from lxml import etree
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
86 from StringIO import StringIO
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
87
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
88 page = c(self.core.baseurl).get(self.params).get_content()
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
89 parser = etree.HTMLParser(encoding='utf8')
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
90 page_dom = etree.parse(StringIO(page), parser)
162
e49137521123 pyikriam retrive information about land positions.
Thinker K.F. Li <thinker@branda.to>
parents: 161
diff changeset
91
e49137521123 pyikriam retrive information about land positions.
Thinker K.F. Li <thinker@branda.to>
parents: 161
diff changeset
92 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']"
161
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
93 for name, path in self.data_patterns.items():
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
94 xpath = xpath_globalinfo + path
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
95 value = page_dom.xpath(xpath)[0]
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
96 setattr(self, name, value)
162
e49137521123 pyikriam retrive information about land positions.
Thinker K.F. Li <thinker@branda.to>
parents: 161
diff changeset
97 pass
e49137521123 pyikriam retrive information about land positions.
Thinker K.F. Li <thinker@branda.to>
parents: 161
diff changeset
98
e49137521123 pyikriam retrive information about land positions.
Thinker K.F. Li <thinker@branda.to>
parents: 161
diff changeset
99 xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li'
e49137521123 pyikriam retrive information about land positions.
Thinker K.F. Li <thinker@branda.to>
parents: 161
diff changeset
100 pos_doms = page_dom.xpath(xpath_mainview)
e49137521123 pyikriam retrive information about land positions.
Thinker K.F. Li <thinker@branda.to>
parents: 161
diff changeset
101 positions = [pos_dom.get('class').split()[-1] for pos_dom in pos_doms]
e49137521123 pyikriam retrive information about land positions.
Thinker K.F. Li <thinker@branda.to>
parents: 161
diff changeset
102 self.positions = positions
161
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
103 pass
1507c2d16b35 Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
104 pass