__main__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. return
  46. yesterday_total = 0
  47. for professor in yesterday_professors:
  48. yesterday_total += professor.points
  49. institutes = get_institutes()
  50. uranium_institutes = []
  51. for institute in institutes:
  52. if institute['department_type'] == department_type:
  53. uranium_institutes.append(institute)
  54. if institute['state_id'] == state_id:
  55. state_institute = institute
  56. top_department = uranium_institutes[0]
  57. top_value = math.ceil(top_department['value'] / 10) * 10
  58. points_per_day = round(top_value / 14)
  59. message = ' '.join([
  60. "Huidige uranium bonus is {} % met {} punten.".format(
  61. state_institute['current_bonus'],
  62. state_institute['value']
  63. ),
  64. "Benodigde punten voor 10 % bonus: {} wat dagelijks {} punten zijn.".format(
  65. top_value,
  66. points_per_day
  67. ),
  68. "Aantal punten gisteren was: {}, dat is {} % van de benodigde punten.".format(
  69. yesterday_total,
  70. round(100 / points_per_day * yesterday_total)
  71. )
  72. ])
  73. print(message)
  74. send_message(language, message)
  75. def add_send_progress_message(state_id, department_type, language):
  76. """Add send_message"""
  77. scheduler.add_job(
  78. job_send_progress_message,
  79. 'cron',
  80. args=[state_id, department_type, language],
  81. id='send_message_{}_{}'.format(state_id, department_type),
  82. replace_existing=True,
  83. hour='20',
  84. minute='10'
  85. )
  86. if __name__ == '__main__':
  87. # jobs
  88. # job_update_department(2788, 6)
  89. # job_send_progress_message(2788, 6)
  90. # VN
  91. # uranium
  92. add_update_department(2788, 6)
  93. # gold
  94. add_update_department(2788, 2)
  95. # construction
  96. add_update_department(2788, 1)
  97. # oil
  98. add_update_department(2788, 3)
  99. # Belgium
  100. # gold
  101. add_update_department(2604, 2)
  102. # De Provincien
  103. # gold
  104. add_update_department(2620, 2)
  105. # send message
  106. add_send_progress_message(2788, 6, 'nl')
  107. try:
  108. while True:
  109. time.sleep(100)
  110. except KeyboardInterrupt:
  111. print('Exiting application')
  112. exit()