txlyre преди 2 години
родител
ревизия
3c24cc7275
променени са 2 файла, в които са добавени 35 реда и са изтрити 18 реда
  1. 8 18
      commands.py
  2. 27 0
      utils.py

+ 8 - 18
commands.py

@@ -1,7 +1,6 @@
 from sys import executable
 from asyncio import create_subprocess_shell
 from asyncio.subprocess import PIPE
-from datetime import datetime, timedelta
 
 from ujson import dumps
 from tortoise.exceptions import IntegrityError
@@ -21,7 +20,7 @@ from actions import (
   add_or_update_birthday,
   get_birthdays
 )
-from utils import WORDS_TABLE, make_temporary_filename, parse_kind, get_user_name, get_word_kind
+from utils import WORDS_TABLE, make_temporary_filename, parse_kind, get_user_name, get_word_for, calculate_age
 
 class Handler:
   def __init__(self, handler, is_restricted=False):
@@ -231,28 +230,19 @@ async def bday_handler(bot, event, command):
 
   birthdays = await get_birthdays(get_peer_id(event.peer_id))
   if not birthdays:
-    await event.reply('Пока пусто')
+    await event.reply('Пока пусто...')
 
     return
 
-  birthdays = sorted(birthdays, key=lambda birthday: birthday.date)
+  birthdays = map(lambda birthday: (birthday.user_id, calculate_age(birthday.date)), birthdays)
+  birthdays = sorted(birthdays, key=lambda birthday: birthday[1].days_until)
 
   birthdays_list = ''
-  for birthday in birthdays:   
-    now = datetime.now().date()
-    birthday_date = birthday.date.replace(year=now.year)
-
-    if now > birthday_date:
-      birthday_date = birthday.date.replace(year=now.year + 1)
-
-    delta = birthday_date - now
-    age = (birthday_date - birthday.date).days // 365
-    age_now = age - 1
-
-    birthdays_list += get_user_name(await bot.get_entity(birthday.user_id))
+  for user_id, age in birthdays:   
+    birthdays_list += get_user_name(await bot.get_entity(user_id))
     birthdays_list += ' — '
-    birthdays_list += birthday.date.strftime('%d.%m.%Y')
-    birthdays_list += f' (через {delta.days} {WORDS_TABLE["день"][get_word_kind(delta.days)]} исполнится {age} {WORDS_TABLE["год"][get_word_kind(age)]}; сейчас {age_now} {WORDS_TABLE["год"][get_word_kind(age_now)]})\n'
+    birthdays_list += age.date_string
+    birthdays_list += f' (через {get_word_for("день", age.days_until)} исполнится {get_word_for("год", age.age)}; сейчас {get_word_for("год", age.age_now)})\n'
 
   await event.reply(f'Дни рождения:\n\n{birthdays_list}')
 

+ 27 - 0
utils.py

@@ -1,5 +1,6 @@
 from uuid import uuid4
 from enum import IntEnum
+from datetime import datetime, timedelta
 from collections import namedtuple
 
 from telethon.utils import get_display_name
@@ -9,6 +10,11 @@ Command = namedtuple(
   'name argc args args_string'
 )
 
+Age = namedtuple(
+  'Age',
+  'days_until age age_now'
+)
+
 class Kind(IntEnum):
   CANNOT_APPLY_TO_SELF = 0
   CAN_APPLY_TO_SELF    = 1
@@ -103,3 +109,24 @@ def get_word_kind(number):
     return WordKind.SECOND
 
   return WordKind.THIRD
+
+def get_word_for(word, number):
+  return f'{number} {WORDS_TABLE[word][get_word_kind(number)]}'
+
+def calculate_age(date):
+  now = datetime.now().date()
+  birthday_date = date.replace(year=now.year)
+
+  if now > birthday_date:
+    birthday_date = date.replace(year=now.year + 1)
+
+  delta = birthday_date - now
+  age = (birthday_date - birthday.date).days // 365
+  age_now = age - 1
+
+  return Age(
+    days_until=delta.days,
+    age=age,
+    age_now=age_now,
+    date_string=date.strftime('%d.%m.%Y')
+  )