api.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, region_id, resource_type, amount, alt):
  33. """Main function"""
  34. return
  35. response = requests.get(
  36. '{}main/content'.format(BASE_URL),
  37. headers=HEADERS
  38. )
  39. soup = BeautifulSoup(response.text, 'html.parser')
  40. state_div = soup.find_all('div', {'class': 'index_case_50'})[1]
  41. action = state_div.findChild()['action']
  42. current_state_id = int(re.sub('.*/', '', action))
  43. params = {}
  44. LOGGER.info(
  45. 'Region belongs to state %s, current state %s',
  46. state_id, current_state_id
  47. )
  48. if current_state_id == state_id:
  49. params = {}
  50. if alt:
  51. params['alt'] = True
  52. json_data = {
  53. 'tmp_gov': '{}_{}'.format(resource_type, amount)
  54. }
  55. requests.post(
  56. '{}parliament/donew/34/{}_{}/{}'.format(
  57. BASE_URL, resource_type, amount, region_id
  58. ),
  59. headers=HEADERS,
  60. params=params,
  61. json=json_data
  62. )
  63. LOGGER.info(
  64. 'Created deep exploration law for %s in %s',
  65. RESOURCE_IDS[resource_type], region_id
  66. )
  67. response = requests.get(
  68. '{}parliament/index/{}'.format(BASE_URL, region_id),
  69. headers=HEADERS
  70. )
  71. soup = BeautifulSoup(response.text, 'html.parser')
  72. active_laws = soup.find('div', {'id': 'parliament_active_laws'})
  73. for exploration_law in active_laws.findAll(text='Deep exploration,'):
  74. action = exploration_law.parent.parent['action']
  75. action = action.replace('law', 'votelaw')
  76. result = requests.post(
  77. '{}{}/pro'.format(BASE_URL, action),
  78. params=params,
  79. headers=HEADERS
  80. )
  81. LOGGER.info('Response: %s', result.text)
  82. LOGGER.info(
  83. 'Accepted deep exploration law for %s in %s',
  84. RESOURCE_IDS[resource_type], region_id
  85. )