new: .rename plugin.

This commit is contained in:
thedragonsinn
2023-12-21 16:14:42 +05:30
parent 90d663b00b
commit 7bdfe0392f
16 changed files with 107 additions and 98 deletions

View File

@@ -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:

View File

@@ -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()

View File

@@ -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))

View File

@@ -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

View File

@@ -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"<b>Download Completed</b>"
f"\n<pre language=bash>"
f"\nfile={downloaded_file.name}"
f"\npath={downloaded_file.full_path}"
f"\nsize={downloaded_file.size}mb</pre>"
)
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

View File

@@ -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:

View File

@@ -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 = ""

View File

@@ -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:

View File

@@ -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:

View File

@@ -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:

View File

@@ -25,4 +25,6 @@ async def cmd_list(bot: BOT, message: Message) -> None:
f"Invalid <b>{cmd}</b>, check {message.trigger}help", del_in=5
)
else:
await message.reply(f"<pre language=js>Doc:{Config.CMD_DICT[cmd].doc}</pre>", del_in=30)
await message.reply(
f"<pre language=js>Doc:{Config.CMD_DICT[cmd].doc}</pre>", del_in=30
)

View File

@@ -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....")

View File

@@ -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....")

View File

@@ -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()

View File

@@ -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,

View File

@@ -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