__main__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. msg_current = "Huidige uranium bonus is {} % met {} punten.".format(
  61. state_institute['current_bonus'],
  62. state_institute['value']
  63. )
  64. if state_institute['current_bonus'] == 10:
  65. msg_required = "Dagelijks zijn er {} punten nodig.".format(
  66. points_per_day
  67. )
  68. else:
  69. msg_required = "Benodigde punten voor 10 % bonus: {} wat dagelijks {} punten zijn.".format(
  70. top_value,
  71. points_per_day
  72. )
  73. msg_yesterday = "Aantal punten gisteren: {}, wat {} % van de benodigde aantal punten is.".format(
  74. yesterday_total,
  75. round(100 / points_per_day * yesterday_total)
  76. )
  77. message = ' '.join([
  78. msg_current,
  79. msg_required,
  80. msg_yesterday
  81. ])
  82. print(message)
  83. send_message(language, message)
  84. def add_send_progress_message(state_id, department_type, language):
  85. """Add send_message"""
  86. scheduler.add_job(
  87. job_send_progress_message,
  88. 'cron',
  89. args=[state_id, department_type, language],
  90. id='send_message_{}_{}'.format(state_id, department_type),
  91. replace_existing=True,
  92. hour='20',
  93. minute='10'
  94. )
  95. if __name__ == '__main__':
  96. # jobs
  97. # job_update_department(2788, 6)
  98. # job_send_progress_message(2788, 6, 'nl')
  99. # VN
  100. # uranium
  101. add_update_department(2788, 6)
  102. # gold
  103. add_update_department(2788, 2)
  104. # construction
  105. add_update_department(2788, 1)
  106. # oil
  107. add_update_department(2788, 3)
  108. # Belgium
  109. # gold
  110. add_update_department(2604, 2)
  111. # uranium
  112. add_update_department(2604, 6)
  113. # De Provincien
  114. # gold
  115. add_update_department(2620, 2)
  116. # send message
  117. add_send_progress_message(2788, 6, 'nl')
  118. try:
  119. while True:
  120. time.sleep(100)
  121. except KeyboardInterrupt:
  122. print('Exiting application')
  123. scheduler.shutdown()
  124. exit()