From 7bdfe0392f9d048015b884309f045fcccc5ab41f Mon Sep 17 00:00:00 2001 From: thedragonsinn <98635854+thedragonsinn@users.noreply.github.com> Date: Thu, 21 Dec 2023 16:14:42 +0530 Subject: [PATCH] new: .rename plugin. --- app/plugins/admin/fbans.py | 4 +- app/plugins/dev/exec.py | 2 +- app/plugins/files/rename.py | 68 ++++++++++++++++++++++++ app/plugins/{misc => files}/upload.py | 6 ++- app/plugins/misc/download.py | 76 --------------------------- app/plugins/sudo/commands.py | 2 +- app/plugins/sudo/users.py | 8 +-- app/plugins/tools/delete.py | 2 +- app/plugins/tools/get_message.py | 2 +- app/plugins/tools/reply.py | 2 +- app/plugins/utils/help.py | 4 +- app/plugins/utils/restart.py | 2 +- app/plugins/utils/update.py | 2 +- app/utils/downloader.py | 13 ++++- app/utils/helpers.py | 10 ++-- docker_start_cmd | 2 +- 16 files changed, 107 insertions(+), 98 deletions(-) create mode 100644 app/plugins/files/rename.py rename app/plugins/{misc => files}/upload.py (98%) delete mode 100644 app/plugins/misc/download.py diff --git a/app/plugins/admin/fbans.py b/app/plugins/admin/fbans.py index c5b1101..6da8b25 100644 --- a/app/plugins/admin/fbans.py +++ b/app/plugins/admin/fbans.py @@ -37,7 +37,7 @@ async def add_fed(bot: BOT, message: Message): """ CMD: ADDF INFO: Add a Fed Chat to DB. - USAGE: + USAGE: .addf | .addf NAME """ data = dict(name=message.input or message.chat.title, type=str(message.chat.type)) @@ -56,7 +56,7 @@ async def remove_fed(bot: BOT, message: Message): CMD: DELF INFO: Delete a Fed from DB. FLAGS: -all to delete all feds. - USAGE: + USAGE: .delf | .delf id | .delf -all """ if "-all" in message.flags: diff --git a/app/plugins/dev/exec.py b/app/plugins/dev/exec.py index f82ef5d..9f1de1a 100644 --- a/app/plugins/dev/exec.py +++ b/app/plugins/dev/exec.py @@ -15,7 +15,7 @@ async def executor(bot: BOT, message: Message) -> Message | None: CMD: EXEC INFO: Run Python Code. FLAGS: -s to only show output. - USAGE: + USAGE: .exec [-s] return 1 """ code: str = message.flt_input.strip() diff --git a/app/plugins/files/rename.py b/app/plugins/files/rename.py new file mode 100644 index 0000000..1f722c6 --- /dev/null +++ b/app/plugins/files/rename.py @@ -0,0 +1,68 @@ +import asyncio +import os +import time + +from app import BOT, bot +from app.core import Message +from app.plugins.files.download import telegram_download +from app.plugins.files.upload import FILE_TYPE_MAP +from app.utils.downloader import Download, DownloadedFile +from app.utils.helpers import progress + + +@bot.add_cmd(cmd="rename") +async def rename(bot: BOT, message: Message): + """ + CMD: RENAME + INFO: Upload Files with custom name + FLAGS: -s for spoiler + USAGE: + .rename [ url | reply to message ] file_name.ext + """ + input = message.flt_input + response = await message.reply("Checking input...") + if (not message.replied or not message.replied.media) and not message.flt_input: + await response.edit( + "Invalid input...\nReply to a message containing media or give a link and filename with cmd." + ) + return + dl_path = os.path.join("downloads", str(time.time())) + await response.edit("Input verified....Starting Download...") + if message.replied: + download_coro = telegram_download( + message=message.replied, + path=dl_path, + file_name=input, + response=response, + ) + else: + url, file_name = input.split() + dl_obj: Download = await Download.setup( + url=url, path=dl_path, message_to_edit=response, custom_file_name=file_name + ) + download_coro = dl_obj.download() + try: + downloaded_file: DownloadedFile = await download_coro + media: dict = await FILE_TYPE_MAP[downloaded_file.type]( + downloaded_file, has_spoiler="-s" in message.flags + ) + progress_args = ( + response, + "Uploading...", + downloaded_file.name, + downloaded_file.full_path, + ) + await media["method"]( + chat_id=message.chat.id, + reply_to_message_id=message.reply_id, + progress=progress, + progress_args=progress_args, + **media["kwargs"] + ) + await response.delete() + except asyncio.exceptions.CancelledError: + await response.edit("Cancelled....") + except TimeoutError: + await response.edit("Download Timeout...") + except Exception as e: + await response.edit(str(e)) diff --git a/app/plugins/misc/upload.py b/app/plugins/files/upload.py similarity index 98% rename from app/plugins/misc/upload.py rename to app/plugins/files/upload.py index d15a7ea..45b8600 100644 --- a/app/plugins/misc/upload.py +++ b/app/plugins/files/upload.py @@ -83,8 +83,10 @@ async def upload(bot: BOT, message: Message): """ CMD: UPLOAD INFO: Upload Media/Local Files/Plugins to TG. - FLAGS: -d to upload as doc. - USAGE: + FLAGS: + -d: to upload as doc. + -s: spoiler. + USAGE: .upload [-d] URL | Path to File | CMD """ input = message.flt_input diff --git a/app/plugins/misc/download.py b/app/plugins/misc/download.py deleted file mode 100644 index 41d8a36..0000000 --- a/app/plugins/misc/download.py +++ /dev/null @@ -1,76 +0,0 @@ -import asyncio -import os -import time - -from app import BOT, bot -from app.core import Message -from app.utils.downloader import Download, DownloadedFile -from app.utils.helpers import progress -from app.utils.media_helper import get_tg_media_details - - -@bot.add_cmd(cmd="download") -async def down_load(bot: BOT, message: Message): - """ - CMD: DOWNLOAD - INFO: Download Files/TG Media to Bot server. - USAGE: - .download URL | Reply to Media - """ - response = await message.reply("Checking Input...") - if (not message.replied or not message.replied.media) and not message.input: - await response.edit( - "Invalid input...\nReply to a message containing media or give a link with cmd." - ) - return - dl_path = os.path.join("downloads", str(time.time())) - await response.edit("Input verified....Starting Download...") - if message.replied and message.replied.media: - download_coro = telegram_download( - message=message.replied, response=response, path=dl_path - ) - else: - dl_obj: Download = await Download.setup( - url=message.input, path=dl_path, message_to_edit=response - ) - download_coro = dl_obj.download() - try: - downloaded_file: DownloadedFile = await download_coro - await response.edit( - f"Download Completed" - f"\n
"
- f"\nfile={downloaded_file.name}"
- f"\npath={downloaded_file.full_path}"
- f"\nsize={downloaded_file.size}mb"
- )
- return downloaded_file
-
- except asyncio.exceptions.CancelledError:
- await response.edit("Cancelled....")
- except TimeoutError:
- await response.edit("Download Timeout...")
- except Exception as e:
- await response.edit(str(e))
-
-
-async def telegram_download(
- message: Message, response: Message, path: str
-) -> DownloadedFile:
- tg_media = get_tg_media_details(message)
- media_obj: DownloadedFile = DownloadedFile(
- name=tg_media.file_name,
- path=path,
- size=round(tg_media.file_size / 1048576, 1),
- full_path=os.path.join(path, tg_media.file_name),
- )
- progress_args = (
- response,
- "Downloading...",
- media_obj.name,
- media_obj.full_path)
- await message.download(
- file_name=media_obj.full_path,
- progress=progress,
- progress_args=progress_args,
- )
- return media_obj
diff --git a/app/plugins/sudo/commands.py b/app/plugins/sudo/commands.py
index 3082357..f43ad1a 100644
--- a/app/plugins/sudo/commands.py
+++ b/app/plugins/sudo/commands.py
@@ -41,7 +41,7 @@ async def del_scmd(bot: BOT, message: Message):
CMD: DELSCMD
INFO: Remove Sudo Commands.
FLAGS: -all to instantly remove all Commands.
- USAGE:
+ USAGE:
.delscmd ping | .delscmd -all
"""
if "-all" in message.flags:
diff --git a/app/plugins/sudo/users.py b/app/plugins/sudo/users.py
index de2d774..b95872d 100644
--- a/app/plugins/sudo/users.py
+++ b/app/plugins/sudo/users.py
@@ -18,7 +18,7 @@ async def sudo(bot: BOT, message: Message):
CMD: SUDO
INFO: Enable/Disable sudo..
FLAGS: -c to check sudo status.
- USAGE:
+ USAGE:
.sudo | .sudo -c
"""
if "-c" in message.flags:
@@ -36,7 +36,7 @@ async def add_sudo(bot: BOT, message: Message) -> Message | None:
CMD: ADDSUDO
INFO: Add Sudo User.
FLAGS: -temp to temporarily add until bot restarts.
- USAGE:
+ USAGE:
.addsudo [-temp] [ UID | @ | Reply to Message ]
"""
response = await message.reply("Extracting User info...")
@@ -67,7 +67,7 @@ async def remove_sudo(bot: BOT, message: Message) -> Message | None:
CMD: DELSUDO
INFO: Add Remove User.
FLAGS: -temp to temporarily remove until bot restarts.
- USAGE:
+ USAGE:
.delsudo [-temp] [ UID | @ | Reply to Message ]
"""
response = await message.reply("Extracting User info...")
@@ -97,7 +97,7 @@ async def sudo_list(bot: BOT, message: Message):
CMD: VSUDO
INFO: View Sudo Users.
FLAGS: -id to get UIDs
- USAGE:
+ USAGE:
.vsudo | .vsudo -id
"""
output: str = ""
diff --git a/app/plugins/tools/delete.py b/app/plugins/tools/delete.py
index db72ff6..736d442 100644
--- a/app/plugins/tools/delete.py
+++ b/app/plugins/tools/delete.py
@@ -9,7 +9,7 @@ async def delete_message(bot: BOT, message: Message) -> None:
CMD: DEL
INFO: Delete the replied message.
FLAGS: -r to remotely delete a text using its link.
- USAGE:
+ USAGE:
.del | .del -r t.me/......
"""
if "-r" in message.flags:
diff --git a/app/plugins/tools/get_message.py b/app/plugins/tools/get_message.py
index 44886f7..282cb72 100644
--- a/app/plugins/tools/get_message.py
+++ b/app/plugins/tools/get_message.py
@@ -16,7 +16,7 @@ async def get_message(bot: BOT, message: Message):
"""
CMD: Get Message
INFO: Get a Message Json/Attr by providing link.
- USAGE:
+ USAGE:
.gm t.me/.... | .gm t.me/... text [Returns message text]
"""
if not message.input:
diff --git a/app/plugins/tools/reply.py b/app/plugins/tools/reply.py
index 2125bb8..60ee684 100644
--- a/app/plugins/tools/reply.py
+++ b/app/plugins/tools/reply.py
@@ -8,7 +8,7 @@ async def reply(bot: BOT, message: Message) -> None:
CMD: REPLY
INFO: Reply to a Message.
FLAGS: -r to reply remotely using message link.
- USAGE:
+ USAGE:
.reply HI | .reply -r t.me/... HI
"""
if "-r" in message.flags:
diff --git a/app/plugins/utils/help.py b/app/plugins/utils/help.py
index 70ad993..18e3fdb 100644
--- a/app/plugins/utils/help.py
+++ b/app/plugins/utils/help.py
@@ -25,4 +25,6 @@ async def cmd_list(bot: BOT, message: Message) -> None:
f"Invalid {cmd}, check {message.trigger}help", del_in=5
)
else:
- await message.reply(f"Doc:{Config.CMD_DICT[cmd].doc}", del_in=30)
+ await message.reply(
+ f"Doc:{Config.CMD_DICT[cmd].doc}", del_in=30
+ )
diff --git a/app/plugins/utils/restart.py b/app/plugins/utils/restart.py
index c5df055..8cfb0d8 100644
--- a/app/plugins/utils/restart.py
+++ b/app/plugins/utils/restart.py
@@ -11,7 +11,7 @@ async def restart(bot: BOT, message: Message, u_resp: Message | None = None) ->
CMD: RESTART
INFO: Restart the Bot.
FLAGS: -h for hard restart and clearing logs
- Usage:
+ Usage:
.restart | .restart -h
"""
reply: Message = u_resp or await message.reply("restarting....")
diff --git a/app/plugins/utils/update.py b/app/plugins/utils/update.py
index 321b6ef..77b7246 100644
--- a/app/plugins/utils/update.py
+++ b/app/plugins/utils/update.py
@@ -42,7 +42,7 @@ async def updater(bot: BOT, message: Message) -> None | Message:
CMD: UPDATE
INFO: Pull / Check for updates.
FLAGS: -pull to pull updates
- USAGE:
+ USAGE:
.update | .update -pull
"""
reply: Message = await message.reply("Checking for Updates....")
diff --git a/app/utils/downloader.py b/app/utils/downloader.py
index 4f4bd3c..ca42160 100644
--- a/app/utils/downloader.py
+++ b/app/utils/downloader.py
@@ -44,6 +44,8 @@ class Download:
download path without file name.
message_to_edit:
response message to edit for progress.
+ custom_file_name:
+ override the file name.
Returns:
ON success a DownloadedFile object is returned.
@@ -68,11 +70,13 @@ class Download:
file_session: aiohttp.ClientResponse,
session: aiohttp.client,
headers: aiohttp.ClientResponse.headers,
+ custom_file_name: str | None = None,
message_to_edit: Message | Msg | None = None,
):
self.url: str = url
self.path: str = path
self.headers: aiohttp.ClientResponse.headers = headers
+ self.custom_file_name: str = custom_file_name
self.file_session: aiohttp.ClientResponse = file_session
self.session: aiohttp.ClientSession = session
self.message_to_edit: Message | Msg | None = message_to_edit
@@ -100,6 +104,8 @@ class Download:
@cached_property
def file_name(self):
+ if self.custom_file_name:
+ return self.custom_file_name
content_disposition = self.headers.get("Content-Disposition", "")
filename_match = re.search(r'filename="(.+)"', content_disposition)
if filename_match:
@@ -157,7 +163,11 @@ class Download:
@classmethod
async def setup(
- cls, url: str, path: str = "downloads", message_to_edit=None
+ cls,
+ url: str,
+ path: str = "downloads",
+ message_to_edit=None,
+ custom_file_name=None,
) -> "Download":
session = aiohttp.ClientSession()
file_session = await session.get(url=url)
@@ -169,6 +179,7 @@ class Download:
session=session,
headers=headers,
message_to_edit=message_to_edit,
+ custom_file_name=custom_file_name,
)
await obj.check_disk_space()
await obj.check_duplicates()
diff --git a/app/utils/helpers.py b/app/utils/helpers.py
index 2496321..154a930 100644
--- a/app/utils/helpers.py
+++ b/app/utils/helpers.py
@@ -1,10 +1,9 @@
-import os
import time
from pyrogram.types import Message, User
from telegraph.aio import Telegraph
-from app import Config, LOGGER
+from app import LOGGER, Config
from app.utils.media_helper import bytes_to_mb
TELEGRAPH: None | Telegraph = None
@@ -17,11 +16,14 @@ async def init_task():
TELEGRAPH = Telegraph()
try:
await TELEGRAPH.create_account(
- short_name="Plain-UB", author_name="Plain-UB", author_url=Config.UPSTREAM_REPO
+ short_name="Plain-UB",
+ author_name="Plain-UB",
+ author_url=Config.UPSTREAM_REPO,
)
- except Exception as e:
+ except Exception:
LOGGER.error("Failed to Create Telegraph Account.")
+
async def post_to_telegraph(title: str, text: str):
telegraph = await TELEGRAPH.create_page(
title=title,
diff --git a/docker_start_cmd b/docker_start_cmd
index ad85bfd..d60fc73 100644
--- a/docker_start_cmd
+++ b/docker_start_cmd
@@ -5,5 +5,5 @@ git config --global credential.helper store
git clone -q --depth=1 "${UPSTREAM_REPO:-"https://github.com/thedragonsinn/plain-ub"}" ubot
cd ubot
-#pip -q install --no-cache-dir -r req*.txt
+pip -q install --no-cache-dir -r req*.txt
bash run