comparison pyikriam/__init__.py @ 68:4ba1e981716d

merged kevint and hychen's work.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 22 Oct 2008 06:24:39 +0800
parents 19d2a7b175b3
children 7551342718b6
comparison
equal deleted inserted replaced
67:6eccb3a95df5 68:4ba1e981716d
1 from lazy.www import c
2 from lconf import LoadConfigfile
3 import cookielib
4 import os
5 import urllib2
6 import urllib
7 class Ikariam:
8
9 cities = {}
10
11 def __init__(self):
12 self.COOKIEFILE = '/tmp/ikariam.lwp'
13 self.confdata=LoadConfigfile().cd
14 self.baseurl='http://'+self.confdata['server']
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()
24
25 def login(self):
26 print "login to %s...." % self.confdata['server']
27 params = {"universe":self.confdata['server'], \
28 "name":self.confdata['user'], \
29 "password":self.confdata['pass']}
30 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
31 self.cj.save(self.COOKIEFILE)
32
33 def logout(self):
34 print "logut from %s...." % self.confdata['server']
35 c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
36 os.remove(self.COOKIEFILE)
37
38 def city(self, id):
39 return self.cities.get(id, IkariamCity(id=id, core=self) )
40
41 class IkariamCity:
42
43 def __init__(self, id, core ):
44 self.core = core
45 self.id = id
46 self.params = {'view':'city','id':id}
47
48 def sync(self):
49 print "pull datas of the city %s" % self.id
50 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul"
51
52 xpath_gold = xpath_globalinfo + "/li[2]/a/span[@id='value_gold']/text()"
53 self.gold = c(self.core.baseurl).get(self.params).find(xpath_gold).get_content()[0]
54