comparison pyikriam/ikariam.py @ 168:8f699a9da6c0

Extract building level of townhalls. - townhall class is responding to extract information about townhall.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 02:26:28 +0800
parents f7fd2738b9b4
children 9f248c8460ce
comparison
equal deleted inserted replaced
167:6ab01f709650 168:8f699a9da6c0
3 import cookielib 3 import cookielib
4 import os 4 import os
5 import urllib2 5 import urllib2
6 import urllib 6 import urllib
7 from utils import dyna_prog, decorator 7 from utils import dyna_prog, decorator
8 from lxml import etree
9 from StringIO import StringIO
8 10
9 class fake_moz(object): 11 class fake_moz(object):
10 __metaclass__ = decorator 12 __metaclass__ = decorator
11 13
12 def __init__(self): 14 def __init__(self):
80 self.core = core 82 self.core = core
81 self.id = id 83 self.id = id
82 self.params = {'view':'city','id':id} 84 self.params = {'view':'city','id':id}
83 85
84 def sync(self): 86 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() 87 page = c(self.core.baseurl).get(self.params).get_content()
89 parser = etree.HTMLParser(encoding='utf8') 88 parser = etree.HTMLParser(encoding='utf8')
90 page_dom = etree.parse(StringIO(page), parser) 89 page_dom = etree.parse(StringIO(page), parser)
91 90
92 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']" 91 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']"
96 setattr(self, name, value) 95 setattr(self, name, value)
97 pass 96 pass
98 97
99 xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li' 98 xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li'
100 pos_doms = page_dom.xpath(xpath_mainview) 99 pos_doms = page_dom.xpath(xpath_mainview)
101 positions = [pos_dom.get('class').split()[-1] for pos_dom in pos_doms] 100 positions = [self._mk_position(pos_dom, idx)
101 for idx, pos_dom in enumerate(pos_doms)]
102 self.positions = positions 102 self.positions = positions
103 pass 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
104 pass 116 pass
117