def request_village(self, village_id, doc_login = None): ''' If village_id is None, doc_login will be used and a list of villages will be stored. ''' doc_resources = doc_login or self.request_GET("/dorf1.php?newdid=%d" % village_id) pages = { 'resources': doc_resources, 'village': self.request_GET("/dorf2.php") } active_village = reader.read_villages(doc_resources, True)[0] if village_id is None: all_villages = reader.read_villages(doc_resources) self.villages.clear() self.villages.update( { vill['village_id']: Village(self, **vill) for vill in all_villages } ) if active_village['village_id'] not in self.villages: self.villages[active_village['village_id']] = Village(self, **active_village) self.current_village = self.villages[active_village['village_id']] village = self.villages[active_village['village_id']] if active_village['name'] != village.name: logger.log_info("village renamed", "Village %s renamed to %s" % (village.name, active_village['name'])) village.name = active_village['name'] village.read_content(pages) village.read_events(pages) village.new_refresh_time() return village
def read_content(self, pages:dict): ''' Reads the content of the given pages and writes them into the village. possible keys: - resources (dorf1.php) resource_fields resources production - village (dorf2.php) - troops (rally_place) ''' # dorf1.php: if 'resources' in pages: doc = pages['resources'] self.resource_fields = reader.read_resource_fields(doc) self.resources = Resources(reader.read_resources(doc)) self.production = Resources(reader.read_production(doc)) if not self.resources or not self.production: logger.log_error("page error", [ "%s: %s" % (t, p.find("html").html().strip()) for t,p in pages.items() ], 'Could not fetch village data') try: self.storage_capacity = Resources(reader.read_storage_capacity(doc)) self.name = reader.read_villages(doc, True)[0]['name'] except Exception as e: traceback.print_exc() self.storage_capacity = [] self.name = "" # dorf2.php if 'village' in pages: doc = pages['village'] self.buildings = reader.read_buildings(doc)