api.py 2.8 KB

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