From 7d216b7b38e8b28f5536ce315e35e0fb859140c5 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Fri, 1 Jul 2022 12:07:01 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- helper/database.py | 21 +-- plugins/broadcast.py | 38 +++--- plugins/cb_data.py | 291 +++++++++++++++++++++--------------------- plugins/filedetect.py | 98 +++++++------- plugins/start.py | 58 ++++----- plugins/thumbfuc.py | 7 +- 6 files changed, 250 insertions(+), 263 deletions(-) diff --git a/helper/database.py b/helper/database.py index fe4e122..b5320ad 100644 --- a/helper/database.py +++ b/helper/database.py @@ -11,11 +11,9 @@ def insert(chat_id): user_id = int(chat_id) user_det = {"_id":user_id,"file_id":None , "date":0} try: - dbcol.insert_one(user_det) + dbcol.insert_one(user_det) except: - value = 'new' - return value - pass + return 'new' def addthumb(chat_id, file_id): dbcol.update_one({"_id":chat_id},{"$set":{"file_id":file_id}}) @@ -24,18 +22,13 @@ def delthumb(chat_id): dbcol.update_one({"_id":chat_id},{"$set":{"file_id":None}}) def find(chat_id): - id = {"_id":chat_id} - x = dbcol.find(id) - for i in x: - lgcd = i["file_id"] - return lgcd + id = {"_id":chat_id} + x = dbcol.find(id) + for i in x: + return i["file_id"] def getid(): - values = [] - for key in dbcol.find(): - id = key["_id"] - values.append((id)) - return values + return [key["_id"] for key in dbcol.find()] def find_one(id): return dbcol.find_one({"_id":id}) diff --git a/plugins/broadcast.py b/plugins/broadcast.py index 3866153..7f59b72 100644 --- a/plugins/broadcast.py +++ b/plugins/broadcast.py @@ -9,22 +9,22 @@ @Client.on_message(filters.private & filters.user(ADMIN) & filters.command(["broadcast"])) async def broadcast(bot, message): - if (message.reply_to_message): - ms = await message.reply_text("Geting All ids from database ...........") - ids = getid() - tot = len(ids) - success = 0 - failed = 0 - await ms.edit(f"Starting Broadcast .... \n Sending Message To {tot} Users") - for id in ids: - try: - time.sleep(1) - await message.reply_to_message.copy(id) - success += 1 - except: - failed += 1 - pass - try: - await ms.edit( f"Message sent to {success} chat(s). {failed} chat(s) failed on receiving message. \nTotal - {tot}" ) - except FloodWait as e: - await asyncio.sleep(t.x) + if not message.reply_to_message: + return + ms = await message.reply_text("Geting All ids from database ...........") + ids = getid() + tot = len(ids) + success = 0 + failed = 0 + await ms.edit(f"Starting Broadcast .... \n Sending Message To {tot} Users") + for id in ids: + try: + time.sleep(1) + await message.reply_to_message.copy(id) + success += 1 + except: + failed += 1 + try: + await ms.edit( f"Message sent to {success} chat(s). {failed} chat(s) failed on receiving message. \nTotal - {tot}" ) + except FloodWait as e: + await asyncio.sleep(t.x) diff --git a/plugins/cb_data.py b/plugins/cb_data.py index 97e249b..e5077bb 100644 --- a/plugins/cb_data.py +++ b/plugins/cb_data.py @@ -25,160 +25,153 @@ async def rename(bot,update): @Client.on_callback_query(filters.regex("doc")) async def doc(bot,update): - new_name = update.message.text - name = new_name.split(":-") - new_filename = name[1] - file_path = f"downloads/{new_filename}" - file = update.message.reply_to_message - ms = await update.message.edit("``` Trying To Download...```") - c_time = time.time() - try: - path = await bot.download_media(message = file, progress=progress_for_pyrogram,progress_args=( "``` Trying To Download...```", ms, c_time )) - except Exception as e: - await ms.edit(e) - return - - splitpath = path.split("/downloads/") - dow_file_name = splitpath[1] - old_file_name =f"downloads/{dow_file_name}" - os.rename(old_file_name,file_path) - user_id = int(update.message.chat.id) - thumb = find(user_id) - if thumb: - ph_path = await bot.download_media(thumb) - Image.open(ph_path).convert("RGB").save(ph_path) - img = Image.open(ph_path) - img.resize((320, 320)) - img.save(ph_path, "JPEG") - c_time = time.time() - await ms.edit("```Trying To Upload```") - c_time = time.time() - try: - await bot.send_document(update.message.chat.id,document = file_path,thumb=ph_path,caption = f"**{new_filename}**",progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) - await ms.delete() - os.remove(file_path) - os.remove(ph_path) - except Exception as e: - await ms.edit(e) - os.remove(file_path) - os.remove(ph_path) - - else: - await ms.edit("```Trying To Upload```") - c_time = time.time() - try: - await bot.send_document(update.message.chat.id,document = file_path,caption = f"**{new_filename}**",progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) - await ms.delete() - os.remove(file_path) - except Exception as e: - await ms.edit(e) - os.remove(file_path) + new_name = update.message.text + name = new_name.split(":-") + new_filename = name[1] + file_path = f"downloads/{new_filename}" + file = update.message.reply_to_message + ms = await update.message.edit("``` Trying To Download...```") + c_time = time.time() + try: + path = await bot.download_media(message = file, progress=progress_for_pyrogram,progress_args=( "``` Trying To Download...```", ms, c_time )) + except Exception as e: + await ms.edit(e) + return + + splitpath = path.split("/downloads/") + dow_file_name = splitpath[1] + old_file_name =f"downloads/{dow_file_name}" + os.rename(old_file_name,file_path) + user_id = int(update.message.chat.id) + if thumb := find(user_id): + ph_path = await bot.download_media(thumb) + Image.open(ph_path).convert("RGB").save(ph_path) + img = Image.open(ph_path) + img.resize((320, 320)) + img.save(ph_path, "JPEG") + c_time = time.time() + await ms.edit("```Trying To Upload```") + c_time = time.time() + try: + await bot.send_document(update.message.chat.id,document = file_path,thumb=ph_path,caption = f"**{new_filename}**",progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) + await ms.delete() + os.remove(file_path) + os.remove(ph_path) + except Exception as e: + await ms.edit(e) + os.remove(file_path) + os.remove(ph_path) + + else: + await ms.edit("```Trying To Upload```") + c_time = time.time() + try: + await bot.send_document(update.message.chat.id,document = file_path,caption = f"**{new_filename}**",progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) + await ms.delete() + os.remove(file_path) + except Exception as e: + await ms.edit(e) + os.remove(file_path) @Client.on_callback_query(filters.regex("vid")) async def vid(bot,update): - new_name = update.message.text - name = new_name.split(":-") - new_filename = name[1] - file_path = f"downloads/{new_filename}" - file = update.message.reply_to_message - ms = await update.message.edit("``` Trying To Download...```") - c_time = time.time() - try: - path = await bot.download_media(message = file, progress=progress_for_pyrogram,progress_args=( "``` Trying To Download...```", ms, c_time )) - except Exception as e: - await ms.edit(e) - return - - splitpath = path.split("/downloads/") - dow_file_name = splitpath[1] - old_file_name =f"downloads/{dow_file_name}" - os.rename(old_file_name,file_path) - duration = 0 - metadata = extractMetadata(createParser(file_path)) - if metadata.has("duration"): - duration = metadata.get('duration').seconds - user_id = int(update.message.chat.id) - thumb = find(user_id) - if thumb: - ph_path = await bot.download_media(thumb) - Image.open(ph_path).convert("RGB").save(ph_path) - img = Image.open(ph_path) - img.resize((320, 320)) - img.save(ph_path, "JPEG") - c_time = time.time() - await ms.edit("```Trying To Upload```") - c_time = time.time() - try: - await bot.send_video(update.message.chat.id,video = file_path,caption = f"**{new_filename}**",thumb=ph_path,duration =duration, progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) - await ms.delete() - os.remove(file_path) - os.remove(ph_path) - except Exception as e: - await ms.edit(e) - os.remove(file_path) - os.remove(ph_path) - - else: - await ms.edit("```Trying To Upload```") - c_time = time.time() - try: - await bot.send_video(update.message.chat.id,video = file_path,caption = f"**{new_filename}**",duration = duration, progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) - await ms.delete() - os.remove(file_path) - except Exception as e: - await ms.edit(e) - os.remove(file_path) + new_name = update.message.text + name = new_name.split(":-") + new_filename = name[1] + file_path = f"downloads/{new_filename}" + file = update.message.reply_to_message + ms = await update.message.edit("``` Trying To Download...```") + c_time = time.time() + try: + path = await bot.download_media(message = file, progress=progress_for_pyrogram,progress_args=( "``` Trying To Download...```", ms, c_time )) + except Exception as e: + await ms.edit(e) + return + + splitpath = path.split("/downloads/") + dow_file_name = splitpath[1] + old_file_name =f"downloads/{dow_file_name}" + os.rename(old_file_name,file_path) + metadata = extractMetadata(createParser(file_path)) + duration = metadata.get('duration').seconds if metadata.has("duration") else 0 + user_id = int(update.message.chat.id) + if thumb := find(user_id): + ph_path = await bot.download_media(thumb) + Image.open(ph_path).convert("RGB").save(ph_path) + img = Image.open(ph_path) + img.resize((320, 320)) + img.save(ph_path, "JPEG") + c_time = time.time() + await ms.edit("```Trying To Upload```") + c_time = time.time() + try: + await bot.send_video(update.message.chat.id,video = file_path,caption = f"**{new_filename}**",thumb=ph_path,duration =duration, progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) + await ms.delete() + os.remove(file_path) + os.remove(ph_path) + except Exception as e: + await ms.edit(e) + os.remove(file_path) + os.remove(ph_path) + + else: + await ms.edit("```Trying To Upload```") + c_time = time.time() + try: + await bot.send_video(update.message.chat.id,video = file_path,caption = f"**{new_filename}**",duration = duration, progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) + await ms.delete() + os.remove(file_path) + except Exception as e: + await ms.edit(e) + os.remove(file_path) @Client.on_callback_query(filters.regex("aud")) async def aud(bot,update): - new_name = update.message.text - name = new_name.split(":-") - new_filename = name[1] - file_path = f"downloads/{new_filename}" - file = update.message.reply_to_message - ms = await update.message.edit("``` Trying To Download...```") - c_time = time.time() - try: - path = await bot.download_media(message = file , progress=progress_for_pyrogram,progress_args=( "``` Trying To Download...```", ms, c_time )) - except Exception as e: - await ms.edit(e) - return - splitpath = path.split("/downloads/") - dow_file_name = splitpath[1] - old_file_name =f"downloads/{dow_file_name}" - os.rename(old_file_name,file_path) - duration = 0 - metadata = extractMetadata(createParser(file_path)) - if metadata.has("duration"): - duration = metadata.get('duration').seconds - user_id = int(update.message.chat.id) - thumb = find(user_id) - if thumb: - ph_path = await bot.download_media(thumb) - Image.open(ph_path).convert("RGB").save(ph_path) - img = Image.open(ph_path) - img.resize((320, 320)) - img.save(ph_path, "JPEG") - await ms.edit("```Trying To Upload```") - c_time = time.time() - try: - await bot.send_audio(update.message.chat.id,audio = file_path,caption = f"**{new_filename}**",thumb=ph_path,duration =duration, progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) - await ms.delete() - os.remove(file_path) - os.remove(ph_path) - except Exception as e: - await ms.edit(e) - os.remove(file_path) - os.remove(ph_path) - else: - await ms.edit("```Trying To Upload```") - c_time = time.time() - try: - await bot.send_audio(update.message.chat.id,audio = file_path,caption = f"**{new_filename}**",duration = duration, progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) - await ms.delete() - os.remove(file_path) - except Exception as e: - await ms.edit(e) - os.remove(file_path) + new_name = update.message.text + name = new_name.split(":-") + new_filename = name[1] + file_path = f"downloads/{new_filename}" + file = update.message.reply_to_message + ms = await update.message.edit("``` Trying To Download...```") + c_time = time.time() + try: + path = await bot.download_media(message = file , progress=progress_for_pyrogram,progress_args=( "``` Trying To Download...```", ms, c_time )) + except Exception as e: + await ms.edit(e) + return + splitpath = path.split("/downloads/") + dow_file_name = splitpath[1] + old_file_name =f"downloads/{dow_file_name}" + os.rename(old_file_name,file_path) + metadata = extractMetadata(createParser(file_path)) + duration = metadata.get('duration').seconds if metadata.has("duration") else 0 + user_id = int(update.message.chat.id) + if thumb := find(user_id): + ph_path = await bot.download_media(thumb) + Image.open(ph_path).convert("RGB").save(ph_path) + img = Image.open(ph_path) + img.resize((320, 320)) + img.save(ph_path, "JPEG") + await ms.edit("```Trying To Upload```") + c_time = time.time() + try: + await bot.send_audio(update.message.chat.id,audio = file_path,caption = f"**{new_filename}**",thumb=ph_path,duration =duration, progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) + await ms.delete() + os.remove(file_path) + os.remove(ph_path) + except Exception as e: + await ms.edit(e) + os.remove(file_path) + os.remove(ph_path) + else: + await ms.edit("```Trying To Upload```") + c_time = time.time() + try: + await bot.send_audio(update.message.chat.id,audio = file_path,caption = f"**{new_filename}**",duration = duration, progress=progress_for_pyrogram,progress_args=( "```Trying To Uploading```", ms, c_time )) + await ms.delete() + os.remove(file_path) + except Exception as e: + await ms.edit(e) + os.remove(file_path) diff --git a/plugins/filedetect.py b/plugins/filedetect.py index e053f14..d5ce897 100644 --- a/plugins/filedetect.py +++ b/plugins/filedetect.py @@ -3,52 +3,54 @@ @Client.on_message(filters.private & filters.reply) async def refunc(client,message): - if (message.reply_to_message.reply_markup) and isinstance(message.reply_to_message.reply_markup, ForceReply): - new_name = message.text - await message.delete() - media = await client.get_messages(message.chat.id,message.reply_to_message.message_id) - file = media.reply_to_message.document or media.reply_to_message.video or media.reply_to_message.audio - filename = file.file_name - types = file.mime_type.split("/") - mime = types[0] - mg_id = media.reply_to_message.message_id - try: - out = new_name.split(".") - out_name = out[1] - out_filename = out[0] + "."+ out_name - await message.reply_to_message.delete() - if mime == "video": - markup = InlineKeyboardMarkup([[ - InlineKeyboardButton("📁 Document",callback_data = "doc"), - InlineKeyboardButton("🎥 Video",callback_data = "vid") ]]) - elif mime == "audio": - markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") - ,InlineKeyboardButton("🎵 audio",callback_data = "aud") ]]) - else: - markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") ]]) - # dont chenge this message.reply_text - await message.reply_text(f"**Select the output file type**\n**Output FileName** :- ```{out_filename}```",reply_to_message_id=mg_id,reply_markup = markup) - - except: - try: - out = filename.split(".") - out_name = out[1] - out_filename= new_name + "."+ out_name - except: - await message.reply_to_message.delete() - await message.reply_text("**Error** : No Extension in File, Not Supporting" - ,reply_to_message_id=mg_id) - return - await message.reply_to_message.delete() - if mime == "video": - markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") - ,InlineKeyboardButton("🎥 Video",callback_data = "vid") ]]) - elif mime == "audio": - markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") - ,InlineKeyboardButton("🎵 audio",callback_data = "aud") ]]) - else: - markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") ]]) - # dont chenge this message.reply_text - await message.reply_text(f"**Select the output file type**\n**Output FileName** :- ```{out_filename}```", - reply_to_message_id=mg_id,reply_markup = markup) + if not message.reply_to_message.reply_markup or not isinstance( + message.reply_to_message.reply_markup, ForceReply): + return + new_name = message.text + await message.delete() + media = await client.get_messages(message.chat.id,message.reply_to_message.message_id) + file = media.reply_to_message.document or media.reply_to_message.video or media.reply_to_message.audio + filename = file.file_name + types = file.mime_type.split("/") + mime = types[0] + mg_id = media.reply_to_message.message_id + try: + out = new_name.split(".") + out_name = out[1] + out_filename = f"{out[0]}.{out_name}" + await message.reply_to_message.delete() + if mime == "video": + markup = InlineKeyboardMarkup([[ + InlineKeyboardButton("📁 Document",callback_data = "doc"), + InlineKeyboardButton("🎥 Video",callback_data = "vid") ]]) + elif mime == "audio": + markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") + ,InlineKeyboardButton("🎵 audio",callback_data = "aud") ]]) + else: + markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") ]]) + # dont chenge this message.reply_text + await message.reply_text(f"**Select the output file type**\n**Output FileName** :- ```{out_filename}```",reply_to_message_id=mg_id,reply_markup = markup) + + except: + try: + out = filename.split(".") + out_name = out[1] + out_filename = f"{new_name}.{out_name}" + except: + await message.reply_to_message.delete() + await message.reply_text("**Error** : No Extension in File, Not Supporting" + ,reply_to_message_id=mg_id) + return + await message.reply_to_message.delete() + if mime == "video": + markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") + ,InlineKeyboardButton("🎥 Video",callback_data = "vid") ]]) + elif mime == "audio": + markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") + ,InlineKeyboardButton("🎵 audio",callback_data = "aud") ]]) + else: + markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📁 Document",callback_data = "doc") ]]) + # dont chenge this message.reply_text + await message.reply_text(f"**Select the output file type**\n**Output FileName** :- ```{out_filename}```", + reply_to_message_id=mg_id,reply_markup = markup) diff --git a/plugins/start.py b/plugins/start.py index cd27919..3207274 100644 --- a/plugins/start.py +++ b/plugins/start.py @@ -37,32 +37,32 @@ async def start(client,message): @Client.on_message(filters.private &( filters.document | filters.audio | filters.video )) async def send_doc(client,message): - update_channel = CHANNEL - user_id = message.from_user.id - if update_channel : - try: - await client.get_chat_member(update_channel, user_id) - except UserNotParticipant: - await message.reply_text("**__You are not subscribed my channel__** ",reply_to_message_id = message.message_id, reply_markup = InlineKeyboardMarkup([ [ InlineKeyboardButton("Support 🇮🇳" ,url=f"https://t.me/{update_channel}") ] ])) - return - date = message.date - _used_date = find_one(user_id) - used_date = _used_date["date"] - c_time = time.time() - LIMIT = 240 - then = used_date+ LIMIT - left = round(then - c_time) - conversion = datetime.timedelta(seconds=left) - ltime = str(conversion) - if left > 0: - await app.send_chat_action(message.chat.id, "typing") - await message.reply_text(f"```Sorry Dude I am not only for YOU \n Flood control is active so please wait for {ltime}```",reply_to_message_id = message.message_id) - else: - - media = await client.get_messages(message.chat.id,message.message_id) - file = media.document or media.video or media.audio - dcid = FileId.decode(file.file_id).dc_id - filename = file.file_name - filesize = humanize.naturalsize(file.file_size) - fileid = file.file_id - await message.reply_text(f"""__What do you want me to do with this file?__\n**File Name** :- {filename}\n**File Size** :- {filesize}\n**Dc ID** :- {dcid} """,reply_to_message_id = message.message_id,reply_markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📝 Rename",callback_data = "rename"),InlineKeyboardButton("✖️ Cancel",callback_data = "cancel") ]])) + update_channel = CHANNEL + user_id = message.from_user.id + if update_channel : + try: + await client.get_chat_member(update_channel, user_id) + except UserNotParticipant: + await message.reply_text("**__You are not subscribed my channel__** ",reply_to_message_id = message.message_id, reply_markup = InlineKeyboardMarkup([ [ InlineKeyboardButton("Support 🇮🇳" ,url=f"https://t.me/{update_channel}") ] ])) + return + date = message.date + _used_date = find_one(user_id) + used_date = _used_date["date"] + c_time = time.time() + LIMIT = 240 + then = used_date+ LIMIT + left = round(then - c_time) + conversion = datetime.timedelta(seconds=left) + if left > 0: + await app.send_chat_action(message.chat.id, "typing") + ltime = str(conversion) + await message.reply_text(f"```Sorry Dude I am not only for YOU \n Flood control is active so please wait for {ltime}```",reply_to_message_id = message.message_id) + else: + + media = await client.get_messages(message.chat.id,message.message_id) + file = media.document or media.video or media.audio + dcid = FileId.decode(file.file_id).dc_id + filename = file.file_name + filesize = humanize.naturalsize(file.file_size) + fileid = file.file_id + await message.reply_text(f"""__What do you want me to do with this file?__\n**File Name** :- {filename}\n**File Size** :- {filesize}\n**Dc ID** :- {dcid} """,reply_to_message_id = message.message_id,reply_markup = InlineKeyboardMarkup([[ InlineKeyboardButton("📝 Rename",callback_data = "rename"),InlineKeyboardButton("✖️ Cancel",callback_data = "cancel") ]])) diff --git a/plugins/thumbfuc.py b/plugins/thumbfuc.py index af5c4cb..b2c4a24 100644 --- a/plugins/thumbfuc.py +++ b/plugins/thumbfuc.py @@ -3,11 +3,10 @@ @Client.on_message(filters.private & filters.command(['viewthumb'])) async def viewthumb(client,message): - thumb = find(int(message.chat.id)) - if thumb : - await client.send_photo(message.chat.id,photo =f"{thumb}") + if thumb := find(int(message.chat.id)): + await client.send_photo(message.chat.id,photo =f"{thumb}") else: - await message.reply_text("**You don't have any custom thumbnail**") + await message.reply_text("**You don't have any custom thumbnail**") @Client.on_message(filters.private & filters.command(['delthumb']))