app.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. """Main application"""
  2. import re
  3. import requests
  4. from bs4 import BeautifulSoup
  5. from hvs import BASE_URL, HEADERS, Session
  6. from hvs.models import ResourceTrack, ResourceStat
  7. RESOURCES = {
  8. 0: 'gold',
  9. 2: 'oil',
  10. 4: 'ore',
  11. 11: 'uranium',
  12. 15: 'diamond',
  13. }
  14. def download_resources(state_id, resource_id):
  15. """Download the resource list"""
  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. '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 print_resources(regions):
  42. """print resources"""
  43. print('region expl max D left c % t %')
  44. for region in regions.values():
  45. region['explored_percentage'] = 100 / region['maximum'] * region['explored']
  46. region['total_left'] = region['explored'] + region['limit_left']
  47. region['total_percentage'] = 100 / 2500 * region['total_left']
  48. print('{:25}: {:7.2f}{:4}{:4}{:5}{:7.2f}{:7.2f}'.format(
  49. region['name'],
  50. region['explored'],
  51. region['maximum'],
  52. region['deep_exploration'],
  53. region['limit_left'],
  54. region['explored_percentage'],
  55. region['total_percentage'],
  56. ))
  57. def need_refill(regions):
  58. """Check if refill is needed"""
  59. for region in regions.values():
  60. percentage = 100 / region['maximum'] * region['explored']
  61. if percentage < 25 and region['limit_left']:
  62. return True
  63. return False
  64. def refill(state_id, capital_id, resource_id):
  65. """Main function"""
  66. # Check location
  67. response = requests.get(
  68. '{}main/content'.format(BASE_URL),
  69. headers=HEADERS
  70. )
  71. soup = BeautifulSoup(response.text, 'html.parser')
  72. state_div = soup.find_all('div', {'class': 'index_case_50'})[1]
  73. action = state_div.findChild()['action']
  74. current_state_id = int(re.sub('.*/', '', action))
  75. print('Current state: {}'.format(current_state_id))
  76. data = {
  77. 'tmp_gov': resource_id
  78. }
  79. params = {}
  80. if current_state_id != state_id:
  81. params['alt'] = True
  82. requests.post(
  83. '{}parliament/donew/42/{}/0'.format(BASE_URL, resource_id),
  84. headers=HEADERS,
  85. params=params,
  86. data=data
  87. )
  88. response = requests.get(
  89. '{}parliament/index/{}'.format(BASE_URL, capital_id),
  90. headers=HEADERS
  91. )
  92. soup = BeautifulSoup(response.text, 'html.parser')
  93. active_laws = soup.find('div', {'id': 'parliament_active_laws'})
  94. resource_name = RESOURCES[resource_id]
  95. exploration_laws = active_laws.findAll(
  96. text='Resources exploration: state, {} resources'.format(resource_name)
  97. )
  98. print('Resources exploration: state, {} resources'.format(resource_name))
  99. print(exploration_laws)
  100. for exploration_law in exploration_laws:
  101. action = exploration_law.parent.parent['action']
  102. action = action.replace('law', 'votelaw')
  103. result = requests.post(
  104. '{}{}/pro'.format(BASE_URL, action),
  105. headers=HEADERS
  106. )
  107. print(result.text)
  108. def save_resources(state_id, regions):
  109. """Save resources to database"""
  110. session = Session()
  111. resource_track = ResourceTrack()
  112. resource_track.state_id = state_id
  113. session.add(resource_track)
  114. session.commit()
  115. for region_id, region in regions.items():
  116. resource_stat = ResourceStat()
  117. resource_stat.region_id = region_id
  118. resource_stat.explored = region['explored']
  119. resource_stat.deep_exploration = region['deep_exploration']
  120. resource_stat.limit_left = region['limit_left']
  121. session.add(resource_stat)
  122. session.commit()