api.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """Main application"""
  2. import re
  3. import requests
  4. from bs4 import BeautifulSoup
  5. from app import BASE_URL, HEADERS, LOGGER, RESOURCE_IDS
  6. def download_resources(state_id, resource_id):
  7. """Download the resource list"""
  8. # return read_resources()
  9. response = requests.get(
  10. '{}listed/stateresources/{}/{}'.format(BASE_URL, state_id, RESOURCES[resource_id]),
  11. headers=HEADERS
  12. )
  13. return parse_resources(response.text)
  14. def read_resources():
  15. """Read resource file"""
  16. with open('resources.html') as file:
  17. return parse_resources(file)
  18. def parse_resources(html):
  19. """Read the resources left"""
  20. soup = BeautifulSoup(html, 'html.parser')
  21. regions_tree = soup.find_all(class_='list_link')
  22. regions = {}
  23. for region_tree in regions_tree:
  24. region_id = int(region_tree['user'])
  25. columns = region_tree.find_all('td')
  26. regions[region_id] = {
  27. 'region_name': re.sub('Factories: .*$', '', columns[1].text),
  28. 'explored': float(columns[2].string),
  29. 'maximum': int(float(columns[3].string)),
  30. 'deep_exploration': int(columns[4].string),
  31. 'limit_left': int(columns[5].string),
  32. }
  33. return regions
  34. def refill(state_id, capital_id, resource_id):
  35. """Main function"""
  36. # Check location
  37. response = requests.get(
  38. '{}main/content'.format(BASE_URL),
  39. headers=HEADERS
  40. )
  41. soup = BeautifulSoup(response.text, 'html.parser')
  42. state_div = soup.find_all('div', {'class': 'index_case_50'})[1]
  43. action = state_div.findChild()['action']
  44. current_state_id = int(re.sub('.*/', '', action))
  45. LOGGER.info('Current state %s', current_state_id)
  46. json_data = {
  47. 'tmp_gov': resource_id
  48. }
  49. params = {}
  50. if current_state_id != state_id:
  51. params['alt'] = True
  52. requests.post(
  53. '{}parliament/donew/42/{}/0'.format(BASE_URL, resource_id),
  54. headers=HEADERS,
  55. params=params,
  56. json=json_data
  57. )
  58. response = requests.get(
  59. '{}parliament/index/{}'.format(BASE_URL, capital_id),
  60. headers=HEADERS
  61. )
  62. soup = BeautifulSoup(response.text, 'html.parser')
  63. active_laws = soup.find('div', {'id': 'parliament_active_laws'})
  64. resource_name = RESOURCE_IDS[resource_id]
  65. exploration_laws = active_laws.findAll(
  66. text='Resources exploration: state, {} resources'.format(resource_name)
  67. )
  68. LOGGER.info('Resources exploration: state, %s resources', resource_name)
  69. for exploration_law in exploration_laws:
  70. action = exploration_law.parent.parent['action']
  71. action = action.replace('law', 'votelaw')
  72. result = requests.post(
  73. '{}{}/pro'.format(BASE_URL, action),
  74. headers=HEADERS
  75. )
  76. LOGGER.info('Response: %s', result.text)