Mercurial > eagle-eye
comparison pyikriam/ikariam.py @ 174:0cfc7a19a4d2
branch merged
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Sun, 02 Nov 2008 04:12:23 +0800 |
parents | 8f699a9da6c0 |
children | 9f248c8460ce |
comparison
equal
deleted
inserted
replaced
173:e8a244ce5a1d | 174:0cfc7a19a4d2 |
---|---|
1 from lazy.www import c | |
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 from lxml import etree | |
9 from StringIO import StringIO | |
10 | |
11 class fake_moz(object): | |
12 __metaclass__ = decorator | |
13 | |
14 def __init__(self): | |
15 super(fake_moz, self).__init__() | |
16 cookie_jar = cookielib.LWPCookieJar() | |
17 cookie_proc = urllib2.HTTPCookieProcessor(cookie_jar) | |
18 opener = urllib2.build_opener(cookie_proc) | |
19 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')] | |
20 fake_moz.set_backend(self, opener) | |
21 self.cookie_jar = cookie_jar | |
22 pass | |
23 pass | |
24 | |
25 | |
26 class Ikariam: | |
27 | |
28 cities = {} | |
29 COOKIEFILE = '/tmp/ikariam.lwp' | |
30 | |
31 def __init__(self): | |
32 browser = fake_moz() | |
33 self.browser = browser | |
34 self._cookie_jar = browser.cookie_jar | |
35 | |
36 if os.path.isfile(self.COOKIEFILE): | |
37 self._cookie_jar.load(self.COOKIEFILE) | |
38 pass | |
39 | |
40 urllib2.install_opener(browser) | |
41 | |
42 self.confdata=LoadConfigfile().cd | |
43 self.baseurl='http://'+self.confdata['server'] | |
44 | |
45 self.login() | |
46 pass | |
47 | |
48 def login(self): | |
49 print "login to %s...." % self.confdata['server'] | |
50 params = {"universe":self.confdata['server'], \ | |
51 "name":self.confdata['user'], \ | |
52 "password":self.confdata['pass']} | |
53 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content() | |
54 self._cookie_jar.save(self.COOKIEFILE) | |
55 pass | |
56 | |
57 def logout(self): | |
58 print "logut from %s...." % self.confdata['server'] | |
59 c(self.baseurl+'/index.php?action=loginAvatar&function=logout') | |
60 os.remove(self.COOKIEFILE) | |
61 pass | |
62 | |
63 ## | |
64 # \note We can cache data with decorator 'dynamic programming'. | |
65 # | |
66 @dyna_prog | |
67 def city(self, id): | |
68 return IkariamCity(id=id, core=self) | |
69 pass | |
70 | |
71 class IkariamCity: | |
72 data_patterns = { | |
73 'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()', | |
74 'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()', | |
75 'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()', | |
76 'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()', | |
77 'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()', | |
78 'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()', | |
79 'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()' | |
80 } | |
81 def __init__(self, id, core ): | |
82 self.core = core | |
83 self.id = id | |
84 self.params = {'view':'city','id':id} | |
85 | |
86 def sync(self): | |
87 page = c(self.core.baseurl).get(self.params).get_content() | |
88 parser = etree.HTMLParser(encoding='utf8') | |
89 page_dom = etree.parse(StringIO(page), parser) | |
90 | |
91 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']" | |
92 for name, path in self.data_patterns.items(): | |
93 xpath = xpath_globalinfo + path | |
94 value = page_dom.xpath(xpath)[0] | |
95 setattr(self, name, value) | |
96 pass | |
97 | |
98 xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li' | |
99 pos_doms = page_dom.xpath(xpath_mainview) | |
100 positions = [self._mk_position(pos_dom, idx) | |
101 for idx, pos_dom in enumerate(pos_doms)] | |
102 self.positions = positions | |
103 pass | |
104 | |
105 def _mk_position(self, pos_dom, idx): | |
106 import buildings | |
107 | |
108 build_type = pos_dom.get('class').split()[-1].lower() | |
109 if hasattr(buildings, build_type): | |
110 clz = getattr(buildings, build_type) | |
111 if issubclass(clz, buildings.position): | |
112 building = clz(self.id, idx, self.core.baseurl) | |
113 return building | |
114 pass | |
115 return build_type | |
116 pass | |
117 |