Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/bot/modules/gd_search.py
1630 views
1
from .. import LOGGER, user_data
2
from ..helper.ext_utils.bot_utils import (
3
sync_to_async,
4
get_telegraph_list,
5
new_task,
6
)
7
from ..helper.mirror_leech_utils.gdrive_utils.search import GoogleDriveSearch
8
from ..helper.telegram_helper.button_build import ButtonMaker
9
from ..helper.telegram_helper.message_utils import send_message, edit_message
10
11
12
async def list_buttons(user_id, is_recursive=True, user_token=False):
13
buttons = ButtonMaker()
14
buttons.data_button(
15
"Folders", f"list_types {user_id} folders {is_recursive} {user_token}"
16
)
17
buttons.data_button(
18
"Files", f"list_types {user_id} files {is_recursive} {user_token}"
19
)
20
buttons.data_button(
21
"Both", f"list_types {user_id} both {is_recursive} {user_token}"
22
)
23
buttons.data_button(
24
f"Recursive: {is_recursive}",
25
f"list_types {user_id} rec {is_recursive} {user_token}",
26
)
27
buttons.data_button(
28
f"User Token: {user_token}",
29
f"list_types {user_id} ut {is_recursive} {user_token}",
30
)
31
buttons.data_button("Cancel", f"list_types {user_id} cancel")
32
return buttons.build_menu(2)
33
34
35
async def _list_drive(key, message, item_type, is_recursive, user_token, user_id):
36
LOGGER.info(f"listing: {key}")
37
if user_token:
38
user_dict = user_data.get(user_id, {})
39
target_id = user_dict.get("gdrive_id", "") or ""
40
LOGGER.info(target_id)
41
else:
42
target_id = ""
43
telegraph_content, contents_no = await sync_to_async(
44
GoogleDriveSearch(is_recursive=is_recursive, item_type=item_type).drive_list,
45
key,
46
target_id,
47
user_id,
48
)
49
if telegraph_content:
50
try:
51
button = await get_telegraph_list(telegraph_content)
52
except Exception as e:
53
await edit_message(message, e)
54
return
55
msg = f"<b>Found {contents_no} result for <i>{key}</i></b>"
56
await edit_message(message, msg, button)
57
else:
58
await edit_message(message, f"No result found for <i>{key}</i>")
59
60
61
@new_task
62
async def select_type(_, query):
63
user_id = query.from_user.id
64
message = query.message
65
key = message.reply_to_message.text.split(maxsplit=1)[1].strip()
66
data = query.data.split()
67
if user_id != int(data[1]):
68
return await query.answer(text="Not Yours!", show_alert=True)
69
elif data[2] == "rec":
70
await query.answer()
71
is_recursive = not bool(eval(data[3]))
72
buttons = await list_buttons(user_id, is_recursive, eval(data[4]))
73
return await edit_message(message, "Choose list options:", buttons)
74
elif data[2] == "ut":
75
await query.answer()
76
user_token = not bool(eval(data[4]))
77
buttons = await list_buttons(user_id, eval(data[3]), user_token)
78
return await edit_message(message, "Choose list options:", buttons)
79
elif data[2] == "cancel":
80
await query.answer()
81
return await edit_message(message, "list has been canceled!")
82
await query.answer()
83
item_type = data[2]
84
is_recursive = eval(data[3])
85
user_token = eval(data[4])
86
await edit_message(message, f"<b>Searching for <i>{key}</i></b>")
87
await _list_drive(key, message, item_type, is_recursive, user_token, user_id)
88
89
90
@new_task
91
async def gdrive_search(_, message):
92
if len(message.text.split()) == 1:
93
return await send_message(message, "Send a search key along with command")
94
user_id = message.from_user.id
95
buttons = await list_buttons(user_id)
96
await send_message(message, "Choose list options:", buttons)
97
98
99
100
101