Mercurial > eagle-eye
annotate pyikriam/__init__.py @ 80:7ab5fc8c847c
added some memo of TODO
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Fri, 24 Oct 2008 21:56:03 +0800 |
parents | 19d2a7b175b3 |
children | 7551342718b6 |
rev | line source |
---|---|
62 | 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 | 3 import cookielib |
4 import os | |
5 import urllib2 | |
6 import urllib | |
7 class Ikariam: | |
8 | |
9 cities = {} | |
10 | |
64
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
11 def __init__(self): |
62 | 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 | 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 | |
64
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
23 self.login() |
62 | 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 | 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): | |
64
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
34 print "logut from %s...." % self.confdata['server'] |
62 | 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 |