Mercurial > eagle-eye
annotate pyikriam/__init__.py @ 161:1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 01 Nov 2008 22:21:25 +0800 |
parents | 7551342718b6 |
children | e49137521123 |
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 | |
160
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
7 from utils import dyna_prog, decorator |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
8 |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
9 class fake_moz(object): |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
10 __metaclass__ = decorator |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
11 |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
12 def __init__(self): |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
13 super(fake_moz, self).__init__() |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
14 cookie_jar = cookielib.LWPCookieJar() |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
15 cookie_proc = urllib2.HTTPCookieProcessor(cookie_jar) |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
16 opener = urllib2.build_opener(cookie_proc) |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
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')] |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
18 fake_moz.set_backend(self, opener) |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
19 self.cookie_jar = cookie_jar |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
20 pass |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
21 pass |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
22 |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
23 |
62 | 24 class Ikariam: |
25 | |
26 cities = {} | |
160
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
27 COOKIEFILE = '/tmp/ikariam.lwp' |
62 | 28 |
64
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
29 def __init__(self): |
160
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
30 browser = fake_moz() |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
31 self.browser = browser |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
32 self._cookie_jar = browser.cookie_jar |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
33 |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
34 if os.path.isfile(self.COOKIEFILE): |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
35 self._cookie_jar.load(self.COOKIEFILE) |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
36 pass |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
37 |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
38 urllib2.install_opener(browser) |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
39 |
64
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
40 self.confdata=LoadConfigfile().cd |
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
41 self.baseurl='http://'+self.confdata['server'] |
62 | 42 |
64
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
43 self.login() |
160
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
44 pass |
62 | 45 |
64
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
46 def login(self): |
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
47 print "login to %s...." % self.confdata['server'] |
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
48 params = {"universe":self.confdata['server'], \ |
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
49 "name":self.confdata['user'], \ |
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
50 "password":self.confdata['pass']} |
62 | 51 ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content() |
160
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
52 self._cookie_jar.save(self.COOKIEFILE) |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
53 pass |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
54 |
62 | 55 def logout(self): |
64
19d2a7b175b3
Load configure data from .eagleeye.pm
kevin@localhost.localdomain
parents:
62
diff
changeset
|
56 print "logut from %s...." % self.confdata['server'] |
62 | 57 c(self.baseurl+'/index.php?action=loginAvatar&function=logout') |
58 os.remove(self.COOKIEFILE) | |
160
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
59 pass |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
60 |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
61 ## |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
62 # \note We can cache data with decorator 'dynamic programming'. |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
63 # |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
64 @dyna_prog |
62 | 65 def city(self, id): |
160
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
66 return IkariamCity(id=id, core=self) |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
67 pass |
7551342718b6
Refactory pyikriam with patterns.
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
68 |
62 | 69 class IkariamCity: |
161
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
70 data_patterns = { |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
71 'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()', |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
72 'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()', |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
73 'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()', |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
74 'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()', |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
75 'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()', |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
76 'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()', |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
77 'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()' |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
78 } |
62 | 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): | |
161
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
85 from lxml import etree |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
86 from StringIO import StringIO |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
87 |
62 | 88 print "pull datas of the city %s" % self.id |
161
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
89 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']" |
62 | 90 |
161
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
91 page = c(self.core.baseurl).get(self.params).get_content() |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
92 parser = etree.HTMLParser(encoding='utf8') |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
93 page_dom = etree.parse(StringIO(page), parser) |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
94 for name, path in self.data_patterns.items(): |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
95 xpath = xpath_globalinfo + path |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
96 value = page_dom.xpath(xpath)[0] |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
97 setattr(self, name, value) |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
98 pass |
1507c2d16b35
Extract wood, marble, crystal, sulfure, and inhabitants information.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
99 pass |