api.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Main application"""
  2. import re
  3. import requests
  4. from bs4 import BeautifulSoup
  5. from dateutil import parser
  6. from app import BASE_URL, HEADERS, LOGGER, RESOURCE_IDS, RESOURCE_NAMES
  7. def download_deep_explorations(region_id):
  8. """Download the deep explorations list"""
  9. # return read_deep_explorations()
  10. response = requests.get(
  11. '{}listed/upgrades/{}'.format(BASE_URL, region_id),
  12. headers=HEADERS
  13. )
  14. return parse_deep_explorations(response.text)
  15. def read_deep_explorations():
  16. """Read deep_exploration file"""
  17. with open('deep_explorations.html') as file:
  18. return parse_deep_explorations(file)
  19. def parse_deep_explorations(html):
  20. """Read the deep_explorations left"""
  21. soup = BeautifulSoup(html, 'html.parser')
  22. deep_explorations_tree = soup.find_all(class_='list_link')
  23. deep_explorations = {}
  24. for deep_exploration_tree in deep_explorations_tree:
  25. deep_exploration_id = int(deep_exploration_tree['user'])
  26. columns = deep_exploration_tree.find_all('td')
  27. deep_explorations[deep_exploration_id] = {
  28. 'resource_type': RESOURCE_NAMES[columns[1].text.replace(' resources', '').lower()],
  29. 'until_date_time': parser.parse(columns[2].string),
  30. }
  31. return deep_explorations
  32. def deep_explorate(state_id, capital_id, resource_type, amount, alt):
  33. """Main function"""
  34. response = requests.get(
  35. '{}main/content'.format(BASE_URL),
  36. headers=HEADERS
  37. )
  38. soup = BeautifulSoup(response.text, 'html.parser')
  39. state_div = soup.find_all('div', {'class': 'index_case_50'})[1]
  40. action = state_div.findChild()['action']
  41. current_state_id = int(re.sub('.*/', '', action))
  42. LOGGER.info('Current state %s', current_state_id)
  43. params = {}
  44. if current_state_id != state_id:
  45. LOGGER.info(
  46. 'Not in the correct state, %s instead of %s',
  47. current_state_id, state_id
  48. )
  49. else:
  50. params = {}
  51. # if current_state_id != state_id:
  52. # params['alt'] = True
  53. if alt:
  54. params['alt'] = True
  55. json_data = {
  56. 'tmp_gov': '{}_{}'.format(resource_type, amount)
  57. }
  58. requests.post(
  59. '{}parliament/donew/34/{}_{}/{}'.format(
  60. BASE_URL, resource_type, amount, capital_id
  61. ),
  62. headers=HEADERS,
  63. params=params,
  64. json=json_data
  65. )
  66. response = requests.get(
  67. '{}parliament/index/{}'.format(BASE_URL, capital_id),
  68. headers=HEADERS
  69. )
  70. soup = BeautifulSoup(response.text, 'html.parser')
  71. active_laws = soup.find('div', {'id': 'parliament_active_laws'})
  72. deep_exploration_name = RESOURCE_IDS[resource_type]
  73. exploration_laws = active_laws.findAll(text='Deep exploration,')
  74. LOGGER.info('Resources exploration: state, %s deep_explorations', deep_exploration_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. params=params,
  81. headers=HEADERS
  82. )
  83. LOGGER.info('Response: %s', result.text)