api.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """API module"""
  2. import re
  3. from datetime import datetime, date, timedelta
  4. import requests
  5. from bs4 import BeautifulSoup
  6. from app import BASE_URL, HEADERS
  7. TYPES = {
  8. 'yellow': 1,
  9. 'oil': 2,
  10. 'ore': 5,
  11. 'uranium': 11,
  12. 'diamond': 15,
  13. }
  14. def get_factories(state_id):
  15. """Get factories from state"""
  16. # return read_factories()
  17. return download_factories(state_id)
  18. def read_factories():
  19. """Read factories file"""
  20. with open('factories.html') as file:
  21. factories, more = parse_factories(file)
  22. return factories
  23. def download_factories(state_id):
  24. """Download the factories"""
  25. factories = []
  26. more = True
  27. page = 0
  28. while more:
  29. response = requests.get(
  30. '{}factory/state/{}/0/0/{}'.format(BASE_URL, state_id, page*25),
  31. headers=HEADERS
  32. )
  33. tmp_factories, more = parse_factories(response.text)
  34. factories = factories + tmp_factories
  35. page += 1
  36. return factories
  37. def parse_factories(html):
  38. """Parse html return factories"""
  39. soup = BeautifulSoup(html, 'html.parser')
  40. factories_tree = soup.find_all(class_='list_link')
  41. factories = []
  42. for factory_tree in factories_tree:
  43. columns = factory_tree.find_all('td')
  44. if columns[1].contents[4].name == 'span':
  45. resource_type = TYPES[columns[1].contents[4]['class'][0]]
  46. else:
  47. resource_type = None
  48. factories.append({
  49. 'id': factory_tree['user'],
  50. 'name': columns[1].contents[0].strip(),
  51. 'resource_type': resource_type,
  52. 'region_name': columns[1].contents[2],
  53. 'level': columns[2].string,
  54. 'workers': re.sub(r'\/[0-9]*$', '', columns[3].string),
  55. 'wage': int(columns[4].string.replace('%', '')),
  56. 'experience': int(columns[5].string),
  57. })
  58. return factories, bool(len(factories_tree) >= 25)
  59. def parse_date(date_string):
  60. """Parse date to object"""
  61. if 'Today' in date_string:
  62. return date.today()
  63. if 'Yesterday' in date_string:
  64. return date.today() - timedelta(1)
  65. return datetime.strptime(date_string, '%d %B %Y').date()