forked from AbirHasan2005/GdriveSearcherBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
201 lines (175 loc) · 6.15 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
from pyrogram.types import CallbackQuery as cbq
from pyrogram.errors.exceptions.bad_request_400 import MessageEmpty, MessageNotModified
from config import BOT_TOKEN, RESULTS_COUNT, SUDO_CHATS_ID, API_ID, API_HASH
from drive import drive
app = Client(":memory:", bot_token=BOT_TOKEN, api_id=API_ID,
api_hash=API_HASH)
i = 0
ii = 0
m = None
keyboard = None
data = None
user_id = None
@app.on_message(filters.command("start") & ~filters.edited & filters.chat(SUDO_CHATS_ID))
async def start_command(_, message):
await message.reply_text("What did you expect to happen? Try /help")
@app.on_message(filters.command("help") & ~filters.edited)
async def help_command(_, message):
await message.reply_text("/search [Query]")
@app.on_message(filters.command("search") & ~filters.edited & filters.chat(SUDO_CHATS_ID))
async def search(_, message: Message):
global i, m, data, user_id
if len(message.command) < 2:
await message.reply_text('/seach Filename')
return
query = message.text.split(' ',maxsplit=1)[1]
m = await message.reply_text("**Searching....**")
data = drive.drive_list(query)
# Anon Admin or That User!
user_id = 1087968824
if message.from_user:
user_id = message.from_user.id
results = len(data)
i = 0
i = i + RESULTS_COUNT
if results == 0:
await m.edit(text="Found Literally Nothing.")
return
text = f"**Total Results:** __{results}__\n"
for count in range(min(i, results)):
if data[count]['type'] == "file":
text += f"""
📄 [{data[count]['name']}
**Size:** __{data[count]['size']}__
**[Drive Link]({data[count]['drive_url']})** | **[Index Link]({data[count]['url']})**\n"""
else:
text += f"""
📂 __{data[count]['name']}__
**[Drive Link]({data[count]['drive_url']})** | **[Index Link]({data[count]['url']})**\n"""
if len(data) > RESULTS_COUNT:
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="<< Previous",
callback_data="previous"
),
InlineKeyboardButton(
text="Next >>",
callback_data="next"
)
],
[InlineKeyboardButton("Close ⬇️", callback_data="closeMeh")]
]
)
try:
await m.edit(text=text, disable_web_page_preview=True, reply_markup=keyboard)
except (MessageEmpty, MessageNotModified):
pass
return
try:
await m.edit(text=text, disable_web_page_preview=True)
except (MessageEmpty, MessageNotModified):
pass
@app.on_callback_query(filters.regex("previous"))
async def previous_callbacc(_, CallbackQuery: cbq):
global i, ii, m, data, user_id
if user_id != CallbackQuery.from_user.id:
await CallbackQuery.answer("Ser, You are not allowed to access other's Search Results!", show_alert=True)
return
if i < RESULTS_COUNT:
await CallbackQuery.answer(
"Already at 1st page, Can't go back.",
show_alert=True
)
return
ii -= RESULTS_COUNT
i -= RESULTS_COUNT
text = ""
for count in range(ii, i):
try:
if data[count]['type'] == "file":
text += f"""
📄 [{data[count]['name']}
**Size:** __{data[count]['size']}__
**[Drive Link]({data[count]['drive_url']})** | **[Index Link]({data[count]['url']})**\n"""
else:
text += f"""
📂 __{data[count]['name']}__
**[Drive Link]({data[count]['drive_url']})** | **[Index Link]({data[count]['url']})**\n"""
except IndexError:
continue
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="<< Previous",
callback_data="previous"
),
InlineKeyboardButton(
text="Next >>",
callback_data="next"
)
],
[
InlineKeyboardButton("Close ⬇️", callback_data="closeMeh")
]
]
)
try:
await m.edit(text=text, disable_web_page_preview=True, reply_markup=keyboard)
except (MessageEmpty, MessageNotModified):
pass
@app.on_callback_query(filters.regex("next"))
async def next_callbacc(_, cb: cbq):
global i, ii, m, data, user_id
if user_id != cb.from_user.id:
await cb.answer("Ser, You are not allowed to access other's Search Results!", show_alert=True)
return
ii = i
i += RESULTS_COUNT
text = ""
for count in range(ii, i):
try:
if data[count]['type'] == "file":
text += f"""
📄 [{data[count]['name']}
**Size:** __{data[count]['size']}__
**[Drive Link]({data[count]['drive_url']})** | **[Index Link]({data[count]['url']})**\n"""
else:
text += f"""
📂 __{data[count]['name']}__
**[Drive Link]({data[count]['drive_url']})** | **[Index Link]({data[count]['url']})**\n"""
except IndexError:
continue
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="<< Previous",
callback_data="previous"
),
InlineKeyboardButton(
text="Next >>",
callback_data="next"
)
],
[
InlineKeyboardButton("Close ⬇️", callback_data="closeMeh")
]
]
)
try:
await m.edit(text=text, disable_web_page_preview=True, reply_markup=keyboard)
except (MessageEmpty, MessageNotModified):
pass
@app.on_callback_query(filters.regex("closeMeh"))
async def close_cb(_, cb: cbq):
global user_id
if user_id != cb.from_user.id:
await cb.answer("Ser, You are not allowed to access other's Search Results!", show_alert=True)
return
await cb.message.delete(True)
app.run()