annotate pyikriam/__init__.py @ 62:a4c364888197

add ikriam game binding lib python version
author hychen@mluna
date Wed, 22 Oct 2008 04:04:32 +0800
parents
children 19d2a7b175b3
rev   line source
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
1 from lazy.www import c
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
2 import cookielib
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
3 import os
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
4 import urllib2
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
5 import urllib
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
6 class Ikariam:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
7
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
8 cities = {}
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
9
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
10 def __init__(self, server, username, password):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
11 self.COOKIEFILE = '/tmp/ikariam.lwp'
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
12 self.server=server
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
13 self.baseurl='http://'+self.server
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
14
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
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
23 self.login(username, password)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
24
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
25 def login(self,username,password):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
26 print "login to %s...." % self.server
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
27 params = {"universe":self.server, "name":username, "password":password}
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
28 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
29 self.cj.save(self.COOKIEFILE)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
30
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
31 def logout(self):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
32 print "logut from %s...." % self.server
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
33 c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
34 os.remove(self.COOKIEFILE)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
35
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
36 def city(self, id):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
37 return self.cities.get(id, IkariamCity(id=id, core=self) )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
38
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
39 class IkariamCity:
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 def __init__(self, id, core ):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
42 self.core = core
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
43 self.id = id
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
44 self.params = {'view':'city','id':id}
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
45
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
46 def sync(self):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
47 print "pull datas of the city %s" % self.id
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
48 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
49
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
50 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
51 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
52
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
53 if __name__ == '__main__':
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
54 i = Ikariam('hychen','pwdyouknow')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
55 city = i.city(117261)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
56 city.sync()
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
57 print city.gold