Mercurial > eagle-eye
annotate pyikriam/__init__.py @ 160:7551342718b6
Refactory pyikriam with patterns.
- Use dyna_prog, a dynamic programming decorator, to cache city objects.
- fake_moz to emulate a mozilla browser.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 01 Nov 2008 21:29:51 +0800 |
parents | 19d2a7b175b3 |
children | 1507c2d16b35 |
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: |
70 | |
71 def __init__(self, id, core ): | |
72 self.core = core | |
73 self.id = id | |
74 self.params = {'view':'city','id':id} | |
75 | |
76 def sync(self): | |
77 print "pull datas of the city %s" % self.id | |
78 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul" | |
79 | |
80 xpath_gold = xpath_globalinfo + "/li[2]/a/span[@id='value_gold']/text()" | |
81 self.gold = c(self.core.baseurl).get(self.params).find(xpath_gold).get_content()[0] | |
82 |