comparison pyikriam/__init__.py @ 64:19d2a7b175b3

Load configure data from .eagleeye.pm
author kevin@localhost.localdomain
date Wed, 22 Oct 2008 05:57:19 +0800
parents a4c364888197
children 7551342718b6
comparison
equal deleted inserted replaced
63:1c42ae140ad3 64:19d2a7b175b3
1 from lazy.www import c 1 from lazy.www import c
2 from lconf import LoadConfigfile
2 import cookielib 3 import cookielib
3 import os 4 import os
4 import urllib2 5 import urllib2
5 import urllib 6 import urllib
6 class Ikariam: 7 class Ikariam:
7 8
8 cities = {} 9 cities = {}
9 10
10 def __init__(self, server, username, password): 11 def __init__(self):
11 self.COOKIEFILE = '/tmp/ikariam.lwp' 12 self.COOKIEFILE = '/tmp/ikariam.lwp'
12 self.server=server 13 self.confdata=LoadConfigfile().cd
13 self.baseurl='http://'+self.server 14 self.baseurl='http://'+self.confdata['server']
14
15 self.cj = cookielib.LWPCookieJar() 15 self.cj = cookielib.LWPCookieJar()
16 if os.path.isfile(self.COOKIEFILE): 16 if os.path.isfile(self.COOKIEFILE):
17 self.cj.load(self.COOKIEFILE) 17 self.cj.load(self.COOKIEFILE)
18 18
19 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) 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')] 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) 21 urllib2.install_opener(opener)
22 22
23 self.login(username, password) 23 self.login()
24 24
25 def login(self,username,password): 25 def login(self):
26 print "login to %s...." % self.server 26 print "login to %s...." % self.confdata['server']
27 params = {"universe":self.server, "name":username, "password":password} 27 params = {"universe":self.confdata['server'], \
28 "name":self.confdata['user'], \
29 "password":self.confdata['pass']}
28 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content() 30 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
29 self.cj.save(self.COOKIEFILE) 31 self.cj.save(self.COOKIEFILE)
30 32
31 def logout(self): 33 def logout(self):
32 print "logut from %s...." % self.server 34 print "logut from %s...." % self.confdata['server']
33 c(self.baseurl+'/index.php?action=loginAvatar&function=logout') 35 c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
34 os.remove(self.COOKIEFILE) 36 os.remove(self.COOKIEFILE)
35 37
36 def city(self, id): 38 def city(self, id):
37 return self.cities.get(id, IkariamCity(id=id, core=self) ) 39 return self.cities.get(id, IkariamCity(id=id, core=self) )
48 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul" 50 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul"
49 51
50 xpath_gold = xpath_globalinfo + "/li[2]/a/span[@id='value_gold']/text()" 52 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] 53 self.gold = c(self.core.baseurl).get(self.params).find(xpath_gold).get_content()[0]
52 54
53 if __name__ == '__main__':
54 i = Ikariam('hychen','pwdyouknow')
55 city = i.city(117261)
56 city.sync()
57 print city.gold