"""Main application""" import re from bs4 import BeautifulSoup from app import LOGGER, RR_WRAPPER, STATE_ID, CAPITAL_ID, RESOURCE_IDS def download_resources(resource_id): """Download the resource list""" # return read_resources() response = RR_WRAPPER.get( 'listed/stateresources/{}/{}'.format(STATE_ID, RESOURCE_IDS[resource_id]) ) return parse_resources(response) def read_resources(): """Read resource file""" with open('resources.html') as file: return parse_resources(file) def parse_resources(html): """Read the resources left""" soup = BeautifulSoup(html, 'html.parser') regions_tree = soup.find_all(class_='list_link') regions = {} for region_tree in regions_tree: region_id = int(region_tree['user']) columns = region_tree.find_all('td') regions[region_id] = { 'region_name': re.sub('Factories: .*$', '', columns[1].text), 'explored': float(columns[2].string), 'maximum': int(float(columns[3].string)), 'deep_exploration': int(columns[4].string), 'limit_left': int(columns[5].string), } return regions def refill(resource_id): """Main function""" resource_name = RESOURCE_IDS[resource_id] LOGGER.info('Start refill for %s', resource_name) data = { 'tmp_gov': resource_id } RR_WRAPPER.post('parliament/donew/42/{}/0'.format(resource_id), data) LOGGER.info('created exploration law for %s', resource_name) response = RR_WRAPPER.get('parliament/index/{}'.format(CAPITAL_ID)) soup = BeautifulSoup(response, 'html.parser') active_laws = soup.find('div', {'id': 'parliament_active_laws'}) exploration_laws = active_laws.findAll( text='Resources exploration: state, {} resources'.format(resource_name) ) LOGGER.info('Number of exploration laws: %s', len(exploration_laws)) for exploration_law in exploration_laws: action = exploration_law.parent.parent['action'] LOGGER.info('vote for law: %s', action) action = action.replace('law', 'votelaw') response = RR_WRAPPER.post('{}/pro'.format(action)) LOGGER.info('Response: %s', response)