app.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """General functions module"""
  2. import math
  3. from app import LOGGER, database, api
  4. def update_department(state_id, department_type):
  5. """Update department professors"""
  6. LOGGER.info('"%s": Run update for "%s" department for state', state_id, department_type)
  7. latest_professor = database.get_latest_professor(state_id, department_type)
  8. date = None
  9. if latest_professor:
  10. date = latest_professor.date_time
  11. professors = api.get_professors(state_id, department_type, date)
  12. LOGGER.info(
  13. '"%s": Found "%s" new professors in "%s" department',
  14. state_id, len(professors), department_type
  15. )
  16. # print_professors(professors)
  17. database.save_professors(state_id, department_type, professors)
  18. LOGGER.info('"%s": saved professors', state_id)
  19. def send_progress_message(state_id, department_type, language):
  20. """Update department professors"""
  21. LOGGER.info('"%s": Send progress message for "%s" department', state_id, department_type)
  22. yesterday_professors = database.get_yesterday_professors(state_id, department_type)
  23. if not yesterday_professors:
  24. LOGGER.warning('"%s": 0 professor yesterday in "%s" department', state_id, department_type)
  25. return
  26. yesterday_total = 0
  27. for professor in yesterday_professors:
  28. yesterday_total += professor.points
  29. institutes = api.get_institutes()
  30. uranium_institutes = []
  31. for institute in institutes:
  32. if institute['department_type'] == department_type:
  33. uranium_institutes.append(institute)
  34. if institute['state_id'] == state_id:
  35. state_institute = institute
  36. top_department = uranium_institutes[0]
  37. top_value = math.ceil(top_department['value'] / 10) * 10
  38. points_per_day = round(top_value / 14)
  39. msg_current = "Huidige uranium bonus is {} % met {} punten.".format(
  40. state_institute['current_bonus'],
  41. state_institute['value']
  42. )
  43. if state_institute['current_bonus'] == 10:
  44. msg_required = "Dagelijks zijn er {} punten nodig.".format(
  45. points_per_day
  46. )
  47. else:
  48. msg_required = \
  49. "Benodigde punten voor 10 % bonus: {} wat dagelijks {} punten zijn.".format(
  50. top_value,
  51. points_per_day
  52. )
  53. msg_yesterday = \
  54. "Aantal punten gisteren: {}, wat {} % van de benodigde aantal punten is.".format(
  55. yesterday_total,
  56. round(100 / points_per_day * yesterday_total)
  57. )
  58. message = ' '.join([
  59. msg_current,
  60. msg_required,
  61. msg_yesterday
  62. ])
  63. print(message)
  64. api.send_message(language, message)