-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeyboards.py
171 lines (150 loc) · 6.04 KB
/
keyboards.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import math
import os
import platform
import pyrogram
from pykeyboard import InlineKeyboard
import utils
def BuildPager(page: int, n_items: int, max_items_keyboard: int) -> list:
"""
Use this method to create the pager on the bottom of the keyboards.
page (``int``): Current page you are on.
n_items (``int``): Number of items to put in the keyboard.
max_items_keyboard (``int``): Maximum number of items to put in the keyboard per page.
SUCCESS Returns ``list`` of buttons to append to a keyboard that needs it.
"""
page = int(page)
n_items = int(n_items)
max_items_keyboard = int(max_items_keyboard)
pager = list()
if n_items > max_items_keyboard:
if page - 2 >= 0:
# goto first
pager.append(
pyrogram.types.InlineKeyboardButton(
pyrogram.emoji.LAST_TRACK_BUTTON, callback_data=f"FMpages{page}<<"
)
)
if page - 1 >= 0:
# previous page
pager.append(
pyrogram.types.InlineKeyboardButton(
pyrogram.emoji.REVERSE_BUTTON, callback_data=f"FMpages{page}-"
)
)
# select page button
pager.append(
pyrogram.types.InlineKeyboardButton(
f"{page + 1}/{math.ceil(n_items / max_items_keyboard)}",
callback_data="FMpages",
)
)
if page + 1 < math.ceil(n_items / max_items_keyboard):
# next page
pager.append(
pyrogram.types.InlineKeyboardButton(
pyrogram.emoji.PLAY_BUTTON, callback_data=f"FMpages{page}+"
)
)
if page + 2 < math.ceil(n_items / max_items_keyboard):
# goto last
pager.append(
pyrogram.types.InlineKeyboardButton(
pyrogram.emoji.NEXT_TRACK_BUTTON, callback_data=f"FMpages{page}>>"
)
)
return pager
# TODO PAGINATION
def BuildItemsKeyboard(
path: str, page: int = 0, max_columns: int = 2, max_rows: int = 8
) -> InlineKeyboard:
"""
Use this method to process items of the folder in order to create a keyboard.
path (``str``): Pass the path you want to build the keyboard on.
page (``int``): Pass the page you want to be on.
max_columns (``int``, *optional*, default = 2): Pass the maximum number of columns(buttons) to insert per keyboard(row) (1 to 8).
max_rows (``int``, *optional*, default = 8): Pass the maximum number of rows to insert per keyboard (6 to 100).
SUCCESS Returns ``InlineKeyboard`` of items and other useful buttons.
FAILURE Returns an error (``str``).
"""
path = str(path) if path else "/"
page = int(page)
max_rows = int(max_rows)
max_columns = int(max_columns)
# adjust the maximum number of columns(buttons) per keyboard(row) if out of bounds
if max_columns < 1 or max_columns > 8:
max_columns = 2
# adjust the maximum number of rows per keyboard if out of bounds
if max_rows < 6 or max_rows > 100:
max_rows = 8
max_items_keyboard = max_rows * max_columns
if platform.system() == "Windows" and path == "/":
# Windows root(s)
path = ""
items = utils.GetDrives()
else:
# Linux or Mac or other folder
try:
items = os.listdir(path)
except Exception:
items = list()
keyboard = InlineKeyboard()
if path:
if items:
keyboard.row(
pyrogram.types.InlineKeyboardButton(
text=f"{pyrogram.emoji.OPEN_FILE_FOLDER} .",
callback_data=("FMcd."),
),
pyrogram.types.InlineKeyboardButton(
text=f"{pyrogram.emoji.OPEN_FILE_FOLDER} ..",
callback_data=("FMcd.."),
),
pyrogram.types.InlineKeyboardButton(
text=f"{pyrogram.emoji.OPEN_FILE_FOLDER} {pyrogram.emoji.DOWN_ARROW}",
callback_data=("FMul."),
),
)
else:
keyboard.row(
pyrogram.types.InlineKeyboardButton(
text=f"{pyrogram.emoji.OPEN_FILE_FOLDER} .",
callback_data=("FMcd."),
),
pyrogram.types.InlineKeyboardButton(
text=f"{pyrogram.emoji.OPEN_FILE_FOLDER} ..",
callback_data=("FMcd.."),
),
)
tmp = list()
for i, item in enumerate(sorted(items)):
if i >= page * max_items_keyboard:
if len(keyboard[-1]) >= max_columns:
# max_columns buttons per line, then add another row
keyboard.row(*tmp)
tmp = list()
tmp_path = os.path.abspath(os.path.join(path, item))
if os.path.isfile(tmp_path):
tmp.append(
pyrogram.types.InlineKeyboardButton(
text=pyrogram.emoji.PAGE_FACING_UP + f" {item}",
callback_data=f"FMul{i}",
)
)
elif os.path.isdir(tmp_path):
tmp.append(
pyrogram.types.InlineKeyboardButton(
text=pyrogram.emoji.OPEN_FILE_FOLDER + f" {item}",
callback_data=f"FMcd{i}",
)
)
else:
tmp.append(
pyrogram.types.InlineKeyboardButton(
text=pyrogram.emoji.QUESTION_MARK + f" {item}",
callback_data=f"FM{i}" if path else f"FMcddrive{i}",
)
)
if sum(len(row) for row in keyboard.inline_keyboard) >= max_items_keyboard:
break
keyboard.row(BuildPager(page, len(items), max_items_keyboard))
return keyboard