api.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. def get_residents(region_id):
  8. """Get residents from region"""
  9. return download_players(region_id, 'residency')
  10. def get_citizens(region_id):
  11. """Get citizens from region"""
  12. return download_players(region_id, 'region')
  13. def get_work_permits(state_id):
  14. """Get work permits from state"""
  15. return download_players(state_id, 'permits_state')
  16. def download_players(region_id, player_type):
  17. """Download the players"""
  18. players = []
  19. more = True
  20. page = 0
  21. while more:
  22. if player_type == 'permits_state':
  23. url = '{}listed/{}/{}/{}'.format(BASE_URL, player_type, region_id, page*25)
  24. else:
  25. url = '{}listed/{}/{}/0/{}'.format(BASE_URL, player_type, region_id, page*25)
  26. response = requests.get(
  27. url,
  28. headers=HEADERS
  29. )
  30. if player_type == 'permits_state':
  31. tmp_players, more = parse_work_permits(response.text)
  32. else:
  33. tmp_players, more = parse_players(response.text)
  34. players = players + tmp_players
  35. page += 1
  36. return players
  37. def parse_players(html):
  38. """Parse html return players"""
  39. soup = BeautifulSoup(html, 'html.parser')
  40. players_tree = soup.find_all(class_='list_link')
  41. players = []
  42. for player_tree in players_tree:
  43. columns = player_tree.find_all('td')
  44. registration_date = parse_date(columns[2].string)
  45. players.append({
  46. 'id': int(re.sub(r'^.*\/', '', columns[1]['action'])),
  47. 'name': columns[1].string.strip(),
  48. 'nation': columns[5]['title'],
  49. 'registration_date': registration_date
  50. })
  51. return players, bool(len(players_tree) >= 25)
  52. def parse_work_permits(html):
  53. """Parse html return players"""
  54. soup = BeautifulSoup(html, 'html.parser')
  55. players_tree = soup.find_all(class_='list_link')
  56. players = []
  57. for player_tree in players_tree:
  58. columns = player_tree.find_all('td')
  59. registration_date = parse_date(columns[2].string)
  60. players.append({
  61. 'id': int(columns[1].find(class_='small')['user']),
  62. 'name': columns[1].contents[0].strip(),
  63. 'nation': columns[5]['title'],
  64. 'from': registration_date
  65. })
  66. return players, bool(len(players_tree) >= 25)
  67. def parse_date(date_string):
  68. """Parse date to object"""
  69. if 'Today' in date_string:
  70. return date.today()
  71. if 'Yesterday' in date_string:
  72. return date.today() - timedelta(1)
  73. return datetime.strptime(date_string, '%d %B %Y').date()