api.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """API module"""
  2. import re
  3. from datetime import datetime, timedelta
  4. import requests
  5. from bs4 import BeautifulSoup
  6. from app import BASE_URL, HEADERS
  7. def get_professors(state_id, department_type, start_date):
  8. """Download list of professors"""
  9. professors = []
  10. not_reached_date = True
  11. page = 0
  12. while not_reached_date:
  13. tmp_professors = download_department(state_id, department_type, page)
  14. if not tmp_professors:
  15. not_reached_date = False
  16. break
  17. # tmp_professors = read_department()
  18. for professor in tmp_professors:
  19. if start_date is not None and professor['date_time'] <= start_date:
  20. not_reached_date = False
  21. break
  22. professors.append(professor)
  23. page += 1
  24. professors.reverse()
  25. return professors
  26. def download_department(state_id, department_type, page):
  27. """Download the department"""
  28. response = requests.get(
  29. '{}listed/professors/{}/{}/{}'.format(BASE_URL, department_type, state_id, page*25),
  30. headers=HEADERS
  31. )
  32. return parse_department(response.text)
  33. def read_department():
  34. """Read from department file"""
  35. with open('department.html') as file:
  36. return parse_department(file)
  37. def parse_department(html):
  38. """Parse html return professors"""
  39. soup = BeautifulSoup(html, 'html.parser')
  40. professors_tree = soup.find_all(class_='list_link')
  41. professors = []
  42. today = datetime.strftime(datetime.now(), '%-d %B %Y')
  43. yesterday = datetime.strftime(datetime.now() - timedelta(1), '%-d %B %Y')
  44. for professor_tree in professors_tree:
  45. columns = professor_tree.find_all('td')
  46. date = columns[3].string
  47. date = date.replace('Today ', today)
  48. date = date.replace('Yesterday ', yesterday)
  49. professors.append({
  50. 'id': int(re.sub(r'^.*\/', '', columns[1]['action'])),
  51. 'name': re.sub(r'\s\(.*$', '', columns[1].string),
  52. 'points': int(re.sub(r'^.*\(\+|\)$', '', columns[1].string)),
  53. 'date_time': datetime.strptime(date, '%d %B %Y %H:%M'),
  54. })
  55. return professors
  56. def get_institutes():
  57. """Get all institutes"""
  58. # return read_institutes()
  59. return download_institutes()
  60. def download_institutes():
  61. """Download the department"""
  62. response = requests.get(
  63. '{}listed/institutes'.format(BASE_URL),
  64. headers=HEADERS
  65. )
  66. return parse_institutes(response.text)
  67. def read_institutes():
  68. """Read from department file"""
  69. with open('institutes.html') as file:
  70. return parse_institutes(file)
  71. def parse_institutes(html):
  72. """Parse html to institute list"""
  73. soup = BeautifulSoup(html, 'html.parser')
  74. institutes_tree = soup.find_all(class_='list_link')
  75. institutes = []
  76. for institute_tree in institutes_tree:
  77. department_type = \
  78. int(re.sub(r'^.*\/', '', institute_tree.select('.results_date')[1]['action']))
  79. value = int(institute_tree.select('.list_level')[0].string)
  80. current_bonus = float(re.sub(r'\s%$', '', institute_tree.select('.list_level')[2].string))
  81. if current_bonus >= 2:
  82. institutes.append({
  83. 'state_id': int(institute_tree['user']),
  84. 'department_type': department_type,
  85. 'current_bonus': current_bonus,
  86. 'value': value,
  87. })
  88. return institutes
  89. def send_message(language, message):
  90. """Send chat message"""
  91. requests.post(
  92. '{}send_chat/{}'.format(BASE_URL, language),
  93. headers=HEADERS,
  94. data={'message': message}
  95. )