Skip to content

Commit

Permalink
catbox and fixing something
Browse files Browse the repository at this point in the history
  • Loading branch information
prono69 committed Sep 17, 2024
1 parent f7aa527 commit 8b5f3a9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 32 deletions.
21 changes: 21 additions & 0 deletions userbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import signal
import sys
import time
import requests

import heroku3
from fake_useragent import UserAgent
Expand All @@ -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"
Expand Down Expand Up @@ -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

Expand Down
17 changes: 14 additions & 3 deletions userbot/helpers/config_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,43 @@
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",
"DEFAULT_NAME",
"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",
]


Expand Down
17 changes: 7 additions & 10 deletions userbot/plugins/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -153,16 +150,16 @@ 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:
return await event.edit(f"**Error** : `{str(exc)}`")
except AttributeError:
vinfo = reply.text
# ==============================================================================
if vname in vlist:
if vname:
if vname in oldvars:
vname = oldvars[vname]
if cmd == "set":
Expand Down Expand Up @@ -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")
Expand Down
47 changes: 28 additions & 19 deletions userbot/plugins/webupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit 8b5f3a9

Please sign in to comment.