comparison 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
comparison
equal deleted inserted replaced
160:7551342718b6 161:1507c2d16b35
65 def city(self, id): 65 def city(self, id):
66 return IkariamCity(id=id, core=self) 66 return IkariamCity(id=id, core=self)
67 pass 67 pass
68 68
69 class IkariamCity: 69 class IkariamCity:
70 70 data_patterns = {
71 'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()',
72 'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()',
73 'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()',
74 'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()',
75 'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()',
76 'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()',
77 'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()'
78 }
71 def __init__(self, id, core ): 79 def __init__(self, id, core ):
72 self.core = core 80 self.core = core
73 self.id = id 81 self.id = id
74 self.params = {'view':'city','id':id} 82 self.params = {'view':'city','id':id}
75 83
76 def sync(self): 84 def sync(self):
85 from lxml import etree
86 from StringIO import StringIO
87
77 print "pull datas of the city %s" % self.id 88 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" 89 xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']"
79 90
80 xpath_gold = xpath_globalinfo + "/li[2]/a/span[@id='value_gold']/text()" 91 page = c(self.core.baseurl).get(self.params).get_content()
81 self.gold = c(self.core.baseurl).get(self.params).find(xpath_gold).get_content()[0] 92 parser = etree.HTMLParser(encoding='utf8')
82 93 page_dom = etree.parse(StringIO(page), parser)
94 for name, path in self.data_patterns.items():
95 xpath = xpath_globalinfo + path
96 value = page_dom.xpath(xpath)[0]
97 setattr(self, name, value)
98 pass
99 pass