comparison 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
comparison
equal deleted inserted replaced
60:610bbe1824ef 62:a4c364888197
1 from lazy.www import c
2 import cookielib
3 import os
4 import urllib2
5 import urllib
6 class Ikariam:
7
8 cities = {}
9
10 def __init__(self, server, username, password):
11 self.COOKIEFILE = '/tmp/ikariam.lwp'
12 self.server=server
13 self.baseurl='http://'+self.server
14
15 self.cj = cookielib.LWPCookieJar()
16 if os.path.isfile(self.COOKIEFILE):
17 self.cj.load(self.COOKIEFILE)
18
19 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
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')]
21 urllib2.install_opener(opener)
22
23 self.login(username, password)
24
25 def login(self,username,password):
26 print "login to %s...." % self.server
27 params = {"universe":self.server, "name":username, "password":password}
28 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
29 self.cj.save(self.COOKIEFILE)
30
31 def logout(self):
32 print "logut from %s...." % self.server
33 c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
34 os.remove(self.COOKIEFILE)
35
36 def city(self, id):
37 return self.cities.get(id, IkariamCity(id=id, core=self) )
38
39 class IkariamCity:
40
41 def __init__(self, id, core ):
42 self.core = core
43 self.id = id
44 self.params = {'view':'city','id':id}
45
46 def sync(self):
47 print "pull datas of the city %s" % self.id
48 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul"
49
50 xpath_gold = xpath_globalinfo + "/li[2]/a/span[@id='value_gold']/text()"
51 self.gold = c(self.core.baseurl).get(self.params).find(xpath_gold).get_content()[0]
52
53 if __name__ == '__main__':
54 i = Ikariam('hychen','pwdyouknow')
55 city = i.city(117261)
56 city.sync()
57 print city.gold