api.py 3.1 KB

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