annotate pyikriam/__init__.py @ 286:2ddd2bd21ef2

list spydata when check enemies
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Thu, 04 Dec 2008 21:54:00 +0800
parents 60c4b4b78a01
children
rev   line source
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
1 from lazy.www import c
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
2 from lconf import LoadConfigfile
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
3 import cookielib
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
4 import os
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
5 import urllib2
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
6 import urllib
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
7 class Ikariam:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
8
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
9 cities = {}
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
10
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
11 def __init__(self, DEBUG=False):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
12 self.COOKIEFILE = '/tmp/ikariam.lwp'
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
13 self.confdata=LoadConfigfile().cd
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
14 #self.baseurl='http://'+self.confdata['server']
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
15 if DEBUG:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
16 self.debug = True
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
17 self.baseurl = self.debug_url()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
18 else:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
19 self.baseurl = 'http://s4.ikariam.tw'
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
20 self.cj = cookielib.LWPCookieJar()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
21 if os.path.isfile(self.COOKIEFILE):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
22 self.cj.load(self.COOKIEFILE)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
23
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
24 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
25 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')]
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
26 urllib2.install_opener(opener)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
27
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
28 self.login()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
29
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
30 def debug_url(self):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
31 """
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
32 get the path of testing datas.
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
33 >>>
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
34
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
35 @return: file path with scheme 'file://'
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
36 """
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
37 return 'file://'+ os.path.abspath(os.path.curdir) + '/tests'
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
38
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
39 def login(self):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
40 print "login to %s...." % self.confdata['server']
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
41 params = {"universe":self.confdata['server'], \
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
42 "name":self.confdata['user'], \
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
43 "password":self.confdata['pass']}
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
44 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
45 self.cj.save(self.COOKIEFILE)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
46
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
47 def logout(self):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
48 print "logut from %s...." % self.confdata['server']
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
49 c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
50 os.remove(self.COOKIEFILE)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
51
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
52 def find_number_in(self, html, xpath):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
53 return parse_to_int(self.find_data_in(html, xpath))
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
54
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
55 def find_data_in(self, work, xpath):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
56 if work is None or xpath is None: return
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
57 return work.find(xpath)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
58
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
59 def city(self, id):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
60 return self.cities.get(id, IkariamCity(id=id, core=self) )
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
61
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
62 class IkariamCity:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
63
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
64 def __init__(self, id, core ):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
65 self.core = core
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
66 self.id = id
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
67 self.params = {'view':'city','id':id}
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
68 self.resources = {'gold':0,'wood':0,'wine':0,'marble':0,'crystal':0,'sulfur':0}
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
69
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
70 # define xpath queries
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
71 self.xpath = {
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
72 'gold':"/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul/li[2]/a/span[@id='value_gold']/text()",
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
73 'wood':"/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='cityResources']/ul/li[3]/span[@id='value_wood']/text()",
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
74 'wine':"/////div[@id='cityResources']/ul/li[4]/span[@id='value_wine']/text()",
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
75 'marble':"/////div[@id='cityResources']/ul/li[4]/span[@id='value_wine']/text()",
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
76 'crystal':"/////div[@id='cityResources']/ul/li[4]/span[@id='value_wine']/text()",
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
77 'sulfur':"/////div[@id='cityResources']/ul/li[4]/span[@id='value_wine']/text()"
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
78 }
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
79
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
80 def sync(self):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
81 print "pull datas of the city %s" % self.id
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
82
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
83 if self.core.debug:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
84 work = c(self.core.baseurl+'/viewcity.html').get()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
85 else:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
86 work = c(self.core.baseurl).get(self.params)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
87
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
88 #print work.find(self.xpath['gold']).get_content()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
89 print work.find(self.xpath['wood']).get_content()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
90 print work.find(self.xpath['wood']).get_content()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
91
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
92 def parse_to_int( str ): pass
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
93 # print str[0]
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 167
diff changeset
94 # if str: return int( str.strip().replace(',',''))