comparison pyikriam/__init__.py @ 246:60c4b4b78a01

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