forked from TheHamkerCat/GdriveSearcherBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
181 lines (155 loc) · 5.24 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
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from pyrogram.errors.exceptions.bad_request_400 import MessageEmpty, MessageNotModified
from config import BOT_TOKEN, RESULTS_COUNT, SUDO_CHATS_ID
from drive import drive
from requests import get as g
import logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
L = logging.getLogger("googleapiclient.discovery")
app = Client(":memory:", bot_token=BOT_TOKEN, api_id=6,
api_hash="eb06d4abfb49dc3eeb1aeb98ae0f581e")
i = 0
ii = 0
m = None
keyboard = None
data = 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):
global i, m, data
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)
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"
)
]
]
)
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):
global i, ii, m, data
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"
)
]
]
)
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(_, CallbackQuery):
global i, ii, m, data
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"
)
]
]
)
try:
await m.edit(text=text, disable_web_page_preview=True, reply_markup=keyboard)
except (MessageEmpty, MessageNotModified):
pass
app.run()