annotate pyikriam/__init__.py @ 163:eff35293a058

fixed for research cultural exchange
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sat, 01 Nov 2008 15:43:07 +0800
parents 19d2a7b175b3
children 7551342718b6
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
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
7 class Ikariam:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
8
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
9 cities = {}
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
10
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
11 def __init__(self):
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
12 self.COOKIEFILE = '/tmp/ikariam.lwp'
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
13 self.confdata=LoadConfigfile().cd
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
14 self.baseurl='http://'+self.confdata['server']
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
15 self.cj = cookielib.LWPCookieJar()
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
16 if os.path.isfile(self.COOKIEFILE):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
17 self.cj.load(self.COOKIEFILE)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
18
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
19 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
20 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')]
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
21 urllib2.install_opener(opener)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
22
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
23 self.login()
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
24
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
25 def login(self):
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
26 print "login to %s...." % self.confdata['server']
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
27 params = {"universe":self.confdata['server'], \
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
28 "name":self.confdata['user'], \
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
29 "password":self.confdata['pass']}
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
30 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
31 self.cj.save(self.COOKIEFILE)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
32
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
33 def logout(self):
64
19d2a7b175b3 Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents: 62
diff changeset
34 print "logut from %s...." % self.confdata['server']
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
35 c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
36 os.remove(self.COOKIEFILE)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
37
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
38 def city(self, id):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
39 return self.cities.get(id, IkariamCity(id=id, core=self) )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
40
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
41 class IkariamCity:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
42
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
43 def __init__(self, id, core ):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
44 self.core = core
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
45 self.id = id
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
46 self.params = {'view':'city','id':id}
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
47
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
48 def sync(self):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
49 print "pull datas of the city %s" % self.id
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
50 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
51
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
52 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
53 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
54