app.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, limit):
  58. """Check if refill is needed"""
  59. for region in regions.values():
  60. percentage = 100 / region['maximum'] * region['explored']
  61. if percentage < limit and region['limit_left']:
  62. return True
  63. return False
  64. def max_refill_seconds(regions, limit, max_time):
  65. """Give random seconds for next refill"""
  66. lowest_percentage = limit
  67. for region in regions.values():
  68. percentage = 100 / region['maximum'] * region['explored']
  69. if percentage < lowest_percentage:
  70. lowest_percentage = percentage
  71. return max_time / limit * lowest_percentage
  72. def refill(state_id, capital_id, resource_id):
  73. """Main function"""
  74. # Check location
  75. response = requests.get(
  76. '{}main/content'.format(BASE_URL),
  77. headers=HEADERS
  78. )
  79. soup = BeautifulSoup(response.text, 'html.parser')
  80. state_div = soup.find_all('div', {'class': 'index_case_50'})[1]
  81. action = state_div.findChild()['action']
  82. current_state_id = int(re.sub('.*/', '', action))
  83. print('Current state: {}'.format(current_state_id))
  84. data = {
  85. 'tmp_gov': resource_id
  86. }
  87. params = {}
  88. if current_state_id != state_id:
  89. params['alt'] = True
  90. requests.post(
  91. '{}parliament/donew/42/{}/0'.format(BASE_URL, resource_id),
  92. headers=HEADERS,
  93. params=params,
  94. data=data
  95. )
  96. response = requests.get(
  97. '{}parliament/index/{}'.format(BASE_URL, capital_id),
  98. headers=HEADERS
  99. )
  100. soup = BeautifulSoup(response.text, 'html.parser')
  101. active_laws = soup.find('div', {'id': 'parliament_active_laws'})
  102. resource_name = RESOURCES[resource_id]
  103. exploration_laws = active_laws.findAll(
  104. text='Resources exploration: state, {} resources'.format(resource_name)
  105. )
  106. print('Resources exploration: state, {} resources'.format(resource_name))
  107. print(exploration_laws)
  108. for exploration_law in exploration_laws:
  109. action = exploration_law.parent.parent['action']
  110. action = action.replace('law', 'votelaw')
  111. result = requests.post(
  112. '{}{}/pro'.format(BASE_URL, action),
  113. headers=HEADERS
  114. )
  115. print(result.text)
  116. def save_resources(state_id, regions, resource_id):
  117. """Save resources to database"""
  118. session = Session()
  119. resource_track = ResourceTrack()
  120. resource_track.state_id = state_id
  121. resource_track.resource_id = resource_id
  122. session.add(resource_track)
  123. session.commit()
  124. for region_id, region in regions.items():
  125. resource_stat = ResourceStat()
  126. resource_stat.region_id = region_id
  127. resource_stat.explored = region['explored']
  128. resource_stat.deep_exploration = region['deep_exploration']
  129. resource_stat.limit_left = region['limit_left']
  130. session.add(resource_stat)
  131. session.commit()