forked from Learningbots79/movies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
142 lines (136 loc) · 6.61 KB
/
index.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
import asyncio
from pyrogram import Client, filters, enums
from pyrogram.errors import FloodWait
from pyrogram.errors.exceptions.bad_request_400 import ChannelInvalid, ChatAdminRequired, UsernameInvalid, UsernameNotModified
from info import ADMINS, LOG_CHANNEL, CHANNELS
from database.ia_filterdb import save_file
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from utils import temp, get_readable_time
import time
lock = asyncio.Lock()
@Client.on_callback_query(filters.regex(r'^index'))
async def index_files(bot, query):
_, ident, chat, lst_msg_id, skip = query.data.split("#")
if ident == 'yes':
msg = query.message
await msg.edit("<b>Indexing started...</b>")
try:
chat = int(chat)
except:
chat = chat
await index_files_to_db(int(lst_msg_id), chat, msg, bot, int(skip))
elif ident == 'cancel':
temp.CANCEL = True
await query.message.edit("Trying to cancel Indexing...")
@Client.on_message(filters.command('index') & filters.private & filters.incoming & filters.user(ADMINS))
async def send_for_index(bot, message):
if lock.locked():
return await message.reply('Wait until previous process complete.')
i = await message.reply("Forward last message or send last message link.")
msg = await bot.listen(chat_id=message.chat.id, user_id=message.from_user.id)
await i.delete()
if msg.text and msg.text.startswith("https://t.me"):
try:
msg_link = msg.text.split("/")
last_msg_id = int(msg_link[-1])
chat_id = msg_link[-2]
if chat_id.isnumeric():
chat_id = int(("-100" + chat_id))
except:
await message.reply('Invalid message link!')
return
elif msg.forward_from_chat and msg.forward_from_chat.type == enums.ChatType.CHANNEL:
last_msg_id = msg.forward_from_message_id
chat_id = msg.forward_from_chat.username or msg.forward_from_chat.id
else:
await message.reply('This is not forwarded message or link.')
return
try:
chat = await bot.get_chat(chat_id)
except Exception as e:
return await message.reply(f'Errors - {e}')
if chat.type != enums.ChatType.CHANNEL:
return await message.reply("I can index only channels.")
s = await message.reply("Send skip message number.")
msg = await bot.listen(chat_id=message.chat.id, user_id=message.from_user.id)
await s.delete()
try:
skip = int(msg.text)
except:
return await message.reply("Number is invalid.")
buttons = [[
InlineKeyboardButton('YES', callback_data=f'index#yes#{chat_id}#{last_msg_id}#{skip}')
],[
InlineKeyboardButton('CLOSE', callback_data='close_data'),
]]
reply_markup = InlineKeyboardMarkup(buttons)
await message.reply(f'Do you want to index {chat.title} channel?\nTotal Messages: <code>{last_msg_id}</code>', reply_markup=reply_markup)
@Client.on_message(filters.command('channel'))
async def channel_info(bot, message):
if message.from_user.id not in ADMINS:
await message.reply('ᴏɴʟʏ ᴛʜᴇ ʙᴏᴛ ᴏᴡɴᴇʀ ᴄᴀɴ ᴜsᴇ ᴛʜɪs ᴄᴏᴍᴍᴀɴᴅ... 😑')
return
ids = CHANNELS
if not ids:
return await message.reply("Not set CHANNELS")
text = '**Indexed Channels:**\n\n'
for id in ids:
chat = await bot.get_chat(id)
text += f'{chat.title}\n'
text += f'\n**Total:** {len(ids)}'
await message.reply(text)
async def index_files_to_db(lst_msg_id, chat, msg, bot, skip):
start_time = time.time()
total_files = 0
duplicate = 0
errors = 0
deleted = 0
no_media = 0
unsupported = 0
current = skip
async with lock:
try:
async for message in bot.iter_messages(chat, lst_msg_id, skip):
time_taken = get_readable_time(time.time()-start_time)
if temp.CANCEL:
temp.CANCEL = False
await msg.edit(f"Successfully Cancelled!\nCompleted in {time_taken}\n\nSaved <code>{total_files}</code> files to Database!\nDuplicate Files Skipped: <code>{duplicate}</code>\nDeleted Messages Skipped: <code>{deleted}</code>\nNon-Media messages skipped: <code>{no_media + unsupported}</code>\nUnsupported Media: <code>{unsupported}</code>\nErrors Occurred: <code>{errors}</code>")
return
current += 1
if current % 100 == 0:
btn = [[
InlineKeyboardButton('CANCEL', callback_data=f'index#cancel#{chat}#{lst_msg_id}#{skip}')
]]
await msg.edit_text(text=f"Total messages received: <code>{current}</code>\nTotal messages saved: <code>{total_files}</code>\nDuplicate Files Skipped: <code>{duplicate}</code>\nDeleted Messages Skipped: <code>{deleted}</code>\nNon-Media messages skipped: <code>{no_media + unsupported}</code>\nUnsupported Media: <code>{unsupported}</code>\nErrors Occurred: <code>{errors}</code>", reply_markup=InlineKeyboardMarkup(btn))
await asyncio.sleep(2)
if message.empty:
deleted += 1
continue
elif not message.media:
no_media += 1
continue
elif message.media not in [enums.MessageMediaType.VIDEO, enums.MessageMediaType.DOCUMENT]:
unsupported += 1
continue
media = getattr(message, message.media.value, None)
if not media:
unsupported += 1
continue
elif media.mime_type not in ['video/mp4', 'video/x-matroska']:
unsupported += 1
continue
media.caption = message.caption
sts = await save_file(media)
if sts == 'suc':
total_files += 1
elif sts == 'dup':
duplicate += 1
elif sts == 'err':
errors += 1
except FloodWait as e:
await asyncio.sleep(e.x)
except Exception as e:
await msg.reply(f'Index canceled due to Error - {e}')
else:
time_taken = get_readable_time(time.time()-start_time)
await msg.edit(f'Succesfully saved <code>{total_files}</code> to Database!\nCompleted in {time_taken}\n\nDuplicate Files Skipped: <code>{duplicate}</code>\nDeleted Messages Skipped: <code>{deleted}</code>\nNon-Media messages skipped: <code>{no_media + unsupported}</code>\nUnsupported Media: <code>{unsupported}</code>\nErrors Occurred: <code>{errors}</code>')