view pyikriam/buildings.py @ 183:2362c8c8909e

implemented military builder.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 02 Nov 2008 21:14:55 +0800
parents 8f699a9da6c0
children 9f248c8460ce
line wrap: on
line source

from lazy.www import c
from lxml import etree
from StringIO import StringIO

class position(object):
    def __init__(self, build_type, city_id, idx, baseurl):
        self._baseurl = baseurl + '/index.php'
        self.build_type = build_type
        self.city_id = city_id
        self.idx = idx
        self._params = {'view': 'buildingGround',
                        'id': city_id,
                        'position': idx}
        pass

    def get_page(self):
        page = c(self._baseurl).get(self._params).get_content()
        return page
    pass

class townhall(position):
    xpath_patterns = {
        'level': '/div/div/div[@class=\'buildingLevel\']/text()'
        }

    def __init__(self, city_id, idx, baseurl):
        super(townhall, self).__init__('townhall', city_id, idx, baseurl)
        pass

    def _sync(self, page):
        parser = etree.HTMLParser(encoding='utf8')
        page_dom = etree.parse(StringIO(page), parser)
        xpath_building = '/html/body/div/div'
        for name, ptn in self.xpath_patterns.items():
            path = xpath_building + ptn
            value = page_dom.xpath(path)[0]
            setattr(self, name, value)
            pass
        pass

    def sync(self):
        page = self.get_page()
        self._sync(page)
        pass
    pass