comparison pyikriam/__init__.py @ 166:f7fd2738b9b4

Move Ikariam out of __init__.py to ikariam.py.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 00:59:00 +0800
parents e49137521123
children 6ab01f709650
comparison
equal deleted inserted replaced
165:4f06f72365fa 166:f7fd2738b9b4
1 from lazy.www import c 1 from ikariam import Ikariam
2 from lconf import LoadConfigfile
3 import cookielib
4 import os
5 import urllib2
6 import urllib
7 from utils import dyna_prog, decorator
8 2
9 class fake_moz(object): 3 __all__ = ('Ikariam')
10 __metaclass__ = decorator
11
12 def __init__(self):
13 super(fake_moz, self).__init__()
14 cookie_jar = cookielib.LWPCookieJar()
15 cookie_proc = urllib2.HTTPCookieProcessor(cookie_jar)
16 opener = urllib2.build_opener(cookie_proc)
17 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')]
18 fake_moz.set_backend(self, opener)
19 self.cookie_jar = cookie_jar
20 pass
21 pass
22
23
24 class Ikariam:
25
26 cities = {}
27 COOKIEFILE = '/tmp/ikariam.lwp'
28
29 def __init__(self):
30 browser = fake_moz()
31 self.browser = browser
32 self._cookie_jar = browser.cookie_jar
33
34 if os.path.isfile(self.COOKIEFILE):
35 self._cookie_jar.load(self.COOKIEFILE)
36 pass
37
38 urllib2.install_opener(browser)
39
40 self.confdata=LoadConfigfile().cd
41 self.baseurl='http://'+self.confdata['server']
42
43 self.login()
44 pass
45
46 def login(self):
47 print "login to %s...." % self.confdata['server']
48 params = {"universe":self.confdata['server'], \
49 "name":self.confdata['user'], \
50 "password":self.confdata['pass']}
51 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
52 self._cookie_jar.save(self.COOKIEFILE)
53 pass
54
55 def logout(self):
56 print "logut from %s...." % self.confdata['server']
57 c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
58 os.remove(self.COOKIEFILE)
59 pass
60
61 ##
62 # \note We can cache data with decorator 'dynamic programming'.
63 #
64 @dyna_prog
65 def city(self, id):
66 return IkariamCity(id=id, core=self)
67 pass
68
69 class IkariamCity:
70 data_patterns = {
71 'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()',
72 'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()',
73 'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()',
74 'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()',
75 'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()',
76 'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()',
77 'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()'
78 }
79 def __init__(self, id, core ):
80 self.core = core
81 self.id = id
82 self.params = {'view':'city','id':id}
83
84 def sync(self):
85 from lxml import etree
86 from StringIO import StringIO
87
88 page = c(self.core.baseurl).get(self.params).get_content()
89 parser = etree.HTMLParser(encoding='utf8')
90 page_dom = etree.parse(StringIO(page), parser)
91
92 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']"
93 for name, path in self.data_patterns.items():
94 xpath = xpath_globalinfo + path
95 value = page_dom.xpath(xpath)[0]
96 setattr(self, name, value)
97 pass
98
99 xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li'
100 pos_doms = page_dom.xpath(xpath_mainview)
101 positions = [pos_dom.get('class').split()[-1] for pos_dom in pos_doms]
102 self.positions = positions
103 pass
104 pass