api.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Main application"""
  2. import re
  3. from bs4 import BeautifulSoup
  4. from app import LOGGER, RR_WRAPPER, STATE_ID, CAPITAL_ID, RESOURCE_IDS
  5. def download_resources(resource_id):
  6. """Download the resource list"""
  7. # return read_resources()
  8. response = RR_WRAPPER.get(
  9. 'listed/stateresources/{}/{}'.format(STATE_ID, RESOURCE_IDS[resource_id])
  10. )
  11. return parse_resources(response)
  12. def read_resources():
  13. """Read resource file"""
  14. with open('resources.html') as file:
  15. return parse_resources(file)
  16. def parse_resources(html):
  17. """Read the resources left"""
  18. soup = BeautifulSoup(html, 'html.parser')
  19. regions_tree = soup.find_all(class_='list_link')
  20. regions = {}
  21. for region_tree in regions_tree:
  22. region_id = int(region_tree['user'])
  23. columns = region_tree.find_all('td')
  24. regions[region_id] = {
  25. 'region_name': re.sub('Factories: .*$', '', columns[1].text),
  26. 'explored': float(columns[2].string),
  27. 'maximum': int(float(columns[3].string)),
  28. 'deep_exploration': int(columns[4].string),
  29. 'limit_left': int(columns[5].string),
  30. }
  31. return regions
  32. def refill(resource_id):
  33. """Main function"""
  34. resource_name = RESOURCE_IDS[resource_id]
  35. LOGGER.info('Start refill for %s', resource_name)
  36. data = {
  37. 'tmp_gov': resource_id
  38. }
  39. RR_WRAPPER.post('parliament/donew/42/{}/0'.format(resource_id), data)
  40. LOGGER.info('created exploration law for %s', resource_name)
  41. response = RR_WRAPPER.get('parliament/index/{}'.format(CAPITAL_ID))
  42. soup = BeautifulSoup(response, 'html.parser')
  43. active_laws = soup.find('div', {'id': 'parliament_active_laws'})
  44. exploration_laws = active_laws.findAll(
  45. text='Resources exploration: state, {} resources'.format(resource_name)
  46. )
  47. LOGGER.info('Number of exploration laws: %s', len(exploration_laws))
  48. for exploration_law in exploration_laws:
  49. action = exploration_law.parent.parent['action']
  50. LOGGER.info('vote for law: %s', action)
  51. action = action.replace('law', 'votelaw')
  52. response = RR_WRAPPER.post('{}/pro'.format(action))
  53. LOGGER.info('Response: %s', response)