api.py 2.8 KB

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