utils.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from uuid import uuid4
  2. from enum import IntEnum
  3. from datetime import datetime
  4. from collections import namedtuple
  5. from telethon.utils import get_display_name
  6. Command = namedtuple(
  7. 'Command',
  8. 'name argc args args_string'
  9. )
  10. Age = namedtuple(
  11. 'Age',
  12. 'days_until age age_now date_string'
  13. )
  14. class Kind(IntEnum):
  15. CANNOT_APPLY_TO_SELF = 0
  16. CAN_APPLY_TO_SELF = 1
  17. NO_TARGET = 2
  18. class WordKind(IntEnum):
  19. FIRST = 0 # «год» / «день».
  20. SECOND = 1 # «лет» / «дней».
  21. THIRD = 2 # «года» / «дня».
  22. WORDS_TABLE = {
  23. 'год': ('год', 'лет', 'года'),
  24. 'день': ('день', 'дней', 'дня')
  25. }
  26. def parse_command(text):
  27. text = text.strip()
  28. if not text.startswith('/'):
  29. raise ValueError
  30. text = text.split(' ')
  31. if len(text) < 1:
  32. raise ValueError
  33. command = text[0][1:].lower()
  34. if '@' in command:
  35. command = command.split('@')
  36. command = command[0]
  37. args = text[1:]
  38. argc = len(args)
  39. args_string = ' '.join(args)
  40. return Command(
  41. name=command,
  42. argc=argc,
  43. args=args,
  44. args_string=args_string
  45. )
  46. def get_user_name(user):
  47. full_name = get_display_name(user)
  48. if not full_name:
  49. full_name = user.username
  50. if not full_name:
  51. full_name = '?'
  52. return full_name
  53. def get_link_to_user(user):
  54. full_name = get_user_name(user)
  55. if user.username:
  56. return f'[{full_name}](@{user.username})'
  57. return f'[{full_name}](tg://user?id={user.id})'
  58. def is_valid_name(name):
  59. return name.isidentifier()
  60. def make_temporary_filename(ext):
  61. uid = uuid4().hex
  62. return f'tmp_{uid}.{ext}'
  63. def parse_kind(kind):
  64. kind = int(kind)
  65. if kind < 0 or kind > 2:
  66. raise ValueError
  67. return kind
  68. # Bruh?
  69. def get_word_kind(number):
  70. if number == 0 or 5 <= number <= 20:
  71. return WordKind.SECOND
  72. last_digit = number % 10
  73. if last_digit == 0:
  74. return WordKind.SECOND
  75. if last_digit == 1:
  76. return WordKind.FIRST
  77. if 5 <= last_digit <= 9:
  78. return WordKind.SECOND
  79. return WordKind.THIRD
  80. def get_word_for(word, number):
  81. return f'{number} {WORDS_TABLE[word][get_word_kind(number)]}'
  82. def calculate_age(date):
  83. now = datetime.now().date()
  84. birthday_date = date.replace(year=now.year)
  85. if now > birthday_date:
  86. birthday_date = date.replace(year=now.year + 1)
  87. delta = birthday_date - now
  88. age = (birthday_date - date).days // 365
  89. age_now = age - 1
  90. return Age(
  91. days_until=delta.days,
  92. age=age,
  93. age_now=age_now,
  94. date_string=date.strftime('%d.%m.%Y')
  95. )