__main__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Main app"""
  2. import math
  3. import time
  4. from app import scheduler, LOGGER
  5. from app.api import get_professors, get_institutes, send_message
  6. from app.database import get_latest_professor, save_professors, get_yesterday_professors
  7. def job_update_department(state_id, department_type):
  8. """Update department professors"""
  9. LOGGER.info('"%s": Run update for "%s" department for state', state_id, department_type)
  10. latest_professor = get_latest_professor(state_id, department_type)
  11. date = None
  12. if latest_professor:
  13. date = latest_professor.date_time
  14. professors = get_professors(state_id, department_type, date)
  15. LOGGER.info(
  16. '"%s": Found "%s" new professors in "%s" department',
  17. state_id, len(professors), department_type
  18. )
  19. # print_professors(professors)
  20. save_professors(state_id, department_type, professors)
  21. LOGGER.info('"%s": saved professors', state_id)
  22. def print_professors(professors):
  23. """Print professors"""
  24. for professor in professors:
  25. print('{:30} {:2} {:>25}'.format(
  26. professor['name'],
  27. professor['points'],
  28. professor['date_time'].strftime('%d %B %Y %H:%M'),
  29. ))
  30. def add_update_department(state_id, department_type):
  31. """Add jobs"""
  32. scheduler.add_job(
  33. job_update_department,
  34. 'cron',
  35. args=[state_id, department_type],
  36. id='{}_{}'.format(state_id, department_type),
  37. replace_existing=True,
  38. hour='20'
  39. )
  40. def job_send_progress_message(state_id, department_type, language):
  41. """Update department professors"""
  42. LOGGER.info('"%s": Send progress message for "%s" department', state_id, department_type)
  43. yesterday_professors = get_yesterday_professors(state_id, department_type)
  44. if not yesterday_professors:
  45. LOGGER.warning('"%s": 0 professor yesterday in "%s" department', state_id, department_type)
  46. return
  47. yesterday_total = 0
  48. for professor in yesterday_professors:
  49. yesterday_total += professor.points
  50. institutes = get_institutes()
  51. uranium_institutes = []
  52. for institute in institutes:
  53. if institute['department_type'] == department_type:
  54. uranium_institutes.append(institute)
  55. if institute['state_id'] == state_id:
  56. state_institute = institute
  57. top_department = uranium_institutes[0]
  58. top_value = math.ceil(top_department['value'] / 10) * 10
  59. points_per_day = round(top_value / 14)
  60. message = ' '.join([
  61. "Huidige uranium bonus is {} % met {} punten.".format(
  62. state_institute['current_bonus'],
  63. state_institute['value']
  64. ),
  65. "Benodigde punten voor 10 % bonus: {} wat dagelijks {} punten zijn.".format(
  66. top_value,
  67. points_per_day
  68. ),
  69. "Aantal punten gisteren was: {}, dat is {} % van de benodigde punten.".format(
  70. yesterday_total,
  71. round(100 / points_per_day * yesterday_total)
  72. )
  73. ])
  74. print(message)
  75. send_message(language, message)
  76. def add_send_progress_message(state_id, department_type, language):
  77. """Add send_message"""
  78. scheduler.add_job(
  79. job_send_progress_message,
  80. 'cron',
  81. args=[state_id, department_type, language],
  82. id='send_message_{}_{}'.format(state_id, department_type),
  83. replace_existing=True,
  84. hour='20',
  85. minute='10'
  86. )
  87. if __name__ == '__main__':
  88. # jobs
  89. # job_update_department(2788, 6)
  90. # job_send_progress_message(2788, 6, 'nl')
  91. # VN
  92. # uranium
  93. add_update_department(2788, 6)
  94. # gold
  95. add_update_department(2788, 2)
  96. # construction
  97. add_update_department(2788, 1)
  98. # oil
  99. add_update_department(2788, 3)
  100. # Belgium
  101. # gold
  102. add_update_department(2604, 2)
  103. # uranium
  104. add_update_department(2604, 6)
  105. # De Provincien
  106. # gold
  107. add_update_department(2620, 2)
  108. # send message
  109. add_send_progress_message(2788, 6, 'nl')
  110. try:
  111. while True:
  112. time.sleep(100)
  113. except KeyboardInterrupt:
  114. print('Exiting application')
  115. scheduler.shutdown()
  116. exit()