diff --git a/userbot/__init__.py b/userbot/__init__.py index 80c1d545e0..075bd7103f 100644 --- a/userbot/__init__.py +++ b/userbot/__init__.py @@ -10,6 +10,7 @@ import signal import sys import time +import requests import heroku3 from fake_useragent import UserAgent @@ -21,6 +22,7 @@ from .helpers.functions.musictool import * from .helpers.utils.utils import runasync from .sql_helper.globals import addgvar, delgvar, gvarstatus +from .core.managers import edit_delete, edit_or_reply __version__ = "3.3.0" __license__ = "GNU Affero General Public License v3.0" @@ -51,6 +53,25 @@ def close_connection(*_): def user_agent(): return agent.random +def upload_to_catbox(file_path, userhash=None): + url = 'https://catbox.moe/user/api.php' + data = { + 'reqtype': 'fileupload', + 'userhash': userhash + } + + with open(file_path, 'rb') as f: + files = { + 'fileToUpload': f + } + response = requests.post(url, data=data, files=files) + + if response.status_code == 200: + return response.text + else: + return f"Error: {response.status_code} - {response.text}" + + UPSTREAM_REPO_URL = Config.UPSTREAM_REPO diff --git a/userbot/helpers/config_helper.py b/userbot/helpers/config_helper.py index 1c692b773d..4c851edba2 100644 --- a/userbot/helpers/config_helper.py +++ b/userbot/helpers/config_helper.py @@ -21,12 +21,16 @@ cmdhd = Config.COMMAND_HAND_LER var_list = [ - "ALIVE_PIC", + "ALIVE_CHANNEL", "ALIVE_EMOJI", + "ALIVE_NAME", + "ALIVE_PIC", "ALIVE_TEMPLATE", "ALIVE_TEXT", "ALLOW_NSFW", + "BOTPIC", "BOT_START_PIC", + "CATBOX", "CHANGE_TIME", "CUSTOM_STICKER_PACKNAME", "DEFAULT_BIO", @@ -34,19 +38,26 @@ "DEFAULT_PIC", "DEFAULT_USER", "DIGITAL_PIC", + "FIRST_NAME", "HELP_EMOJI", + "HELP_PIC", "HELP_TEXT", "IALIVE_PIC", + "INLINE_BOT", + "LAST_NAME", "MAX_FLOOD_IN_PMS", - "NO_OF_ROWS_IN_HELP", "NO_OF_COLUMNS_IN_HELP", + "NO_OF_ROWS_IN_HELP", "PING_PIC", + "PING_PICS", "PING_TEMPLATE", + "PM_BLOCK", "PM_PIC", "PM_TEXT", - "PM_BLOCK", + "PUBLIC_CHANNEL_ID", "SPOILER_MEDIA", "START_TEXT", + "THUMB_IMAGE", ] diff --git a/userbot/plugins/custom.py b/userbot/plugins/custom.py index 00c07b714b..2bff11c92a 100644 --- a/userbot/plugins/custom.py +++ b/userbot/plugins/custom.py @@ -8,17 +8,14 @@ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# from PIL import Image -from telegraph import Telegraph, exceptions, upload_file +from telegraph import Telegraph, exceptions from telethon.tl import types from telethon.tl.functions.users import GetFullUserRequest from urlextract import URLExtract -from userbot import BOTLOG_CHATID, catub +from userbot import BOTLOG_CHATID, catub, edit_delete, edit_or_reply, addgvar, delgvar, gvarstatus, upload_to_catbox from userbot.core.logger import logging -from ..core.managers import edit_delete, edit_or_reply -from ..sql_helper.globals import addgvar, delgvar, gvarstatus - plugin_category = "tools" LOGS = logging.getLogger(__name__) @@ -153,8 +150,8 @@ async def bad(event): # sourcery no-metrics # sourcery skip: low-code-quality try: if downloaded_file_name.endswith((".webp")): resize_image(downloaded_file_name) - media_urls = upload_file(downloaded_file_name) - vinfo = f"https://telegra.ph{media_urls[0]}" + media_urls = upload_to_catbox(downloaded_file_name) + vinfo = f"{media_urls}" except AttributeError: return await event.edit("`Error while making link`") except exceptions.TelegraphException as exc: @@ -162,7 +159,7 @@ async def bad(event): # sourcery no-metrics # sourcery skip: low-code-quality except AttributeError: vinfo = reply.text # ============================================================================== - if vname in vlist: + if vname: if vname in oldvars: vname = oldvars[vname] if cmd == "set": @@ -192,8 +189,8 @@ async def bad(event): # sourcery no-metrics # sourcery skip: low-code-quality try: photos = await catub.get_profile_photos(catub.uid) myphoto = await catub.download_media(photos[0]) - myphoto_urls = upload_file(myphoto) - addgvar("DEFAULT_PIC", f"https://graph.org{myphoto_urls[0]}") + myphoto_urls = upload_to_catbox(myphoto) + addgvar("DEFAULT_PIC", f"{myphoto_urls}") except IndexError: if gvarstatus("DEFAULT_PIC"): delgvar("DEFAULT_PIC") diff --git a/userbot/plugins/webupload.py b/userbot/plugins/webupload.py index 145c8b8746..e4913a491f 100644 --- a/userbot/plugins/webupload.py +++ b/userbot/plugins/webupload.py @@ -98,11 +98,11 @@ async def labstack(event): @catub.cat_cmd( - pattern="webupload ?(.+?|) --(fileio|anonfiles|transfer|filebin|anonymousfiles|bayfiles)", + pattern=r"webupload\s?(.+?)?\s--(fileio|anonfiles|transfer|filebin|anonymousfiles|bayfiles|megaupload|vshare|0x0|ninja|infura)", command=("webupload", plugin_category), info={ "header": "To upload media to some online media sharing platforms.", - "description": "you can upload media to any of the sites mentioned. so you can share link to others.", + "description": "You can upload media to any of the sites mentioned. This helps in sharing links with others.", "options": { "fileio": "to file.io site", "anonfiles": "to anonfiles site", @@ -125,24 +125,29 @@ async def labstack(event): ) async def _(event): "To upload media to some online media sharing platforms" - editor = await edit_or_reply(event, "processing ...") + editor = await edit_or_reply(event, "Processing ...") + + # Capture the input string (file path) and selected transfer option input_str = event.pattern_match.group(1) selected_transfer = event.pattern_match.group(2) catcheck = None + if input_str: file_name = input_str else: reply = await event.get_reply_message() + if not reply or not reply.media: + return await editor.edit("Please reply to a media file.") file_name = await event.client.download_media( reply.media, Config.TMP_DOWNLOAD_DIRECTORY ) catcheck = True - # a dictionary containing the shell commands + + # Command dictionary for various upload services CMD_WEB = { "fileio": 'curl -F "file=@{full_file_path}" https://file.io', "anonfiles": 'curl -F "file=@{full_file_path}" https://api.anonfiles.com/upload', - "transfer": 'curl --upload-file "{full_file_path}" https://transfer.sh/' - + os.path.basename(file_name), + "transfer": 'curl --upload-file "{full_file_path}" https://transfer.sh/' + os.path.basename(file_name), "filebin": 'curl -X POST --data-binary "@{full_file_path}" -H "filename: {bare_local_name}" "https://filebin.net"', "anonymousfiles": 'curl -F "file=@{full_file_path}" https://api.anonymousfiles.io/', "vshare": 'curl -F "file=@{full_file_path}" https://api.vshare.is/upload', @@ -152,37 +157,41 @@ async def _(event): "ninja": "curl -i -F file=@{full_file_path} https://tmp.ninja/api.php?d=upload-tool", "infura": "curl -X POST -F file=@'{full_file_path}' \"https://ipfs.infura.io:5001/api/v0/add?pin=true\"", } + filename = os.path.basename(file_name) + try: selected_one = CMD_WEB[selected_transfer].format( full_file_path=file_name, bare_local_name=filename ) except KeyError: - return await editor.edit("**Invalid selected Transfer**") - cmd = selected_one - # start the subprocess $SHELL + return await editor.edit("**Invalid selected Transfer option**") + + # Start the subprocess to execute the command process = await asyncio.create_subprocess_shell( - cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE + selected_one, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) stdout, stderr = await process.communicate() error = stderr.decode().strip() + if t_response := stdout.decode().strip(): try: t_response = json.dumps(json.loads(t_response), sort_keys=True, indent=4) except Exception as e: - # some sites don't return valid JSONs + # Handle non-JSON responses from some sites LOGS.info(str(e)) - urls = links = re.findall(link_regex, t_response) - result = "" - for i in urls: - if not result: - result = "**Uploaded File link/links :**" - result += f"\n{i[0]}" + urls = re.findall(link_regex, t_response) + result = "**Uploaded File link/links :**\n" + "\n".join(urls) if urls else t_response await editor.edit(result) else: - await editor.edit(error) + await editor.edit(f"Error: {error}") + + # Remove file if it was downloaded if catcheck: - os.remove(file_name) + try: + os.remove(file_name) + except Exception as e: + LOGS.info(f"Error removing file: {str(e)}") # By @FeelDeD