-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
58 lines (51 loc) · 2.39 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from persian_tools import digits
from telegram import InlineKeyboardButton
import const
class Util:
@staticmethod
def trim_search_results(results: list[tuple], offset: int) -> list:
message = list()
for i, result in enumerate(results):
a = (i + 1 + offset, result[0], digits.convert_to_fa(i + 1 + offset) + '. ' + result[1] + ' - ' + result[3] + '\n' + result[2])
if len(a[2]) + sum(map(lambda x: len(x[2]), message)) + 2 * len(message) <= 4096:
message.append(a)
else:
return message
return message
@staticmethod
def break_long_verses(poem_text: list[tuple]) -> list:
new_poem_text: list = list()
for verse in poem_text:
if len(verse[0]) > 3800:
separation_index = verse[0].rfind('.', 0, 3800)
new_poem_text.append(verse[0][:separation_index+1])
new_poem_text.append(verse[0][separation_index+1:])
else:
new_poem_text.append(verse[0])
return new_poem_text
@staticmethod
def format_poem(poem, url, title, poet):
return const.POEM.format(poem=poem, url=url, title=title, poet=poet)
@staticmethod
def break_long_poems(poem_text: list[str], poem_info) -> list:
messages = list()
poem_in_a_single_message = ''
for verse in poem_text:
message = Util.format_poem(poem_in_a_single_message + verse, poem_info[1], poem_info[0], poem_info[2])
if len(message) > 4096:
messages.append(Util.format_poem(poem_in_a_single_message, poem_info[1], poem_info[0], poem_info[2]))
poem_in_a_single_message = verse
else:
poem_in_a_single_message += verse + '\n'
messages.append(Util.format_poem(poem_in_a_single_message, poem_info[1], poem_info[0], poem_info[2]))
return messages
@staticmethod
def create_inline_keyboard(number_in_each_row: int, total_buttons: int, button_text_list: list, button_callback_data_list: list):
buttons = list()
for i in range(0, total_buttons, number_in_each_row):
row = list()
for j in range(i, i + number_in_each_row):
button = InlineKeyboardButton(button_text_list[j], callback_data=button_callback_data_list[j])
row.append(button)
buttons.append(row)
return buttons