| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 | """Main application"""import reimport requestsfrom bs4 import BeautifulSoupfrom hvs import BASE_URL, HEADERS, Sessionfrom hvs.models import ResourceTrack, ResourceStatRESOURCES = {    0: 'gold',    2: 'oil',    4: 'ore',    11: 'uranium',    15: 'diamond',}def download_resources(state_id, resource_id):    """Download the resource list"""    response = requests.get(        '{}listed/stateresources/{}/{}'.format(BASE_URL, state_id, RESOURCES[resource_id]),        headers=HEADERS    )    return parse_resources(response.text)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] = {            '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 regionsdef print_resources(regions):    """print resources"""    print('region                        expl max   D left    c %    t %')    for region in regions.values():        region['explored_percentage'] = 100 / region['maximum'] * region['explored']        region['total_left'] = region['explored'] + region['limit_left']        region['total_percentage'] = 100 / 2500 * region['total_left']        print('{:25}: {:7.2f}{:4}{:4}{:5}{:7.2f}{:7.2f}'.format(            region['name'],            region['explored'],            region['maximum'],            region['deep_exploration'],            region['limit_left'],            region['explored_percentage'],            region['total_percentage'],        ))def need_refill(regions):    """Check if refill is needed"""    for region in regions.values():        percentage = 100 / region['maximum'] * region['explored']        if percentage < 25 and region['limit_left']:            return True    return Falsedef refill(capital_id, resource_id):    """Main function"""    data = {        'tmp_gov': resource_id    }    requests.post(        '{}parliament/donew/42/{}/0'.format(BASE_URL, resource_id),        headers=HEADERS,        data=data    )    response = requests.get(        '{}parliament/index/{}'.format(BASE_URL, capital_id),        headers=HEADERS    )    soup = BeautifulSoup(response.text, 'html.parser')    active_laws = soup.find('div', {'id': 'parliament_active_laws'})    resource_name = RESOURCES[resource_id]    exploration_laws = active_laws.findAll(        text='Resources exploration: state, {} resources'.format(resource_name)    )    print('Resources exploration: state, {} resources'.format(resource_name))    print(exploration_laws)    for exploration_law in exploration_laws:        action = exploration_law.parent.parent['action']        action = action.replace('law', 'votelaw')        result = requests.post(            '{}{}/pro'.format(BASE_URL, action),            headers=HEADERS        )        print(result.text)def save_resources(state_id, regions):    """Save resources to database"""    session = Session()    resource_track = ResourceTrack()    resource_track.state_id = state_id    session.add(resource_track)    session.commit()    for region_id, region in regions.items():        resource_stat = ResourceStat()        resource_stat.region_id = region_id        resource_stat.explored = region['explored']        resource_stat.deep_exploration = region['deep_exploration']        resource_stat.limit_left = region['limit_left']        session.add(resource_stat)    session.commit()
 |