annotate pyikriam/__init__.py @ 160:7551342718b6

Refactory pyikriam with patterns. - Use dyna_prog, a dynamic programming decorator, to cache city objects. - fake_moz to emulate a mozilla browser.
author Thinker K.F. Li <thinker@branda.to>
date Sat, 01 Nov 2008 21:29:51 +0800
parents 19d2a7b175b3
children 1507c2d16b35
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:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
70
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
71 def __init__(self, id, core ):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
72 self.core = core
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
73 self.id = id
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
74 self.params = {'view':'city','id':id}
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
75
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
76 def sync(self):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
77 print "pull datas of the city %s" % self.id
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
78 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul"
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
79
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
80 xpath_gold = xpath_globalinfo + "/li[2]/a/span[@id='value_gold']/text()"
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
81 self.gold = c(self.core.baseurl).get(self.params).find(xpath_gold).get_content()[0]
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
82