chore(userbot): apply ruff check --fix and ruff format

- ruff check --fix: 210 auto-fixed errors (import sorting, trailing
  whitespace, unused imports, f-string fixups, deprecated annotations)
- ruff format: 104 files reformatted to consistent style
- 268 non-auto-fixable issues remain (S113 requests timeout, etc.)
This commit is contained in:
2026-06-19 12:19:01 +02:00
parent bd9724da69
commit 0506aaaac8
104 changed files with 4338 additions and 5319 deletions
+44 -57
View File
@@ -17,7 +17,6 @@ import requests
from PIL import Image
from pyrogram import Client, enums, filters
from pyrogram.types import Message
from utils.config import rmbg_key
from utils.misc import modules_help, prefix
from utils.scripts import edit_or_reply, format_exc
@@ -43,54 +42,51 @@ async def convert_to_image(message, client) -> None | str:
if message.reply_to_message.photo:
final_path = await message.reply_to_message.download()
elif message.reply_to_message.sticker:
if message.reply_to_message.sticker.mime_type == "image/webp":
final_path = "webp_to_png_s_proton.png"
if message.reply_to_message.sticker.mime_type == 'image/webp':
final_path = 'webp_to_png_s_proton.png'
path_s = await message.reply_to_message.download()
im = Image.open(path_s)
im.save(final_path, "PNG")
im.save(final_path, 'PNG')
else:
path_s = await client.download_media(message.reply_to_message)
final_path = "lottie_proton.png"
cmd = (
f"lottie_convert.py --frame 0 -if lottie -of png {path_s} {final_path}"
)
final_path = 'lottie_proton.png'
cmd = f'lottie_convert.py --frame 0 -if lottie -of png {path_s} {final_path}'
await exec(cmd) # skipcq
elif message.reply_to_message.audio:
thumb = message.reply_to_message.audio.thumbs[0].file_id
final_path = await client.download_media(thumb)
elif message.reply_to_message.video or message.reply_to_message.animation:
final_path = "fetched_thumb.png"
final_path = 'fetched_thumb.png'
vid_path = await client.download_media(message.reply_to_message)
await exec(
f"ffmpeg -i {vid_path} -filter:v scale=500:500 -an {final_path}"
) # skipcq
await exec(f'ffmpeg -i {vid_path} -filter:v scale=500:500 -an {final_path}') # skipcq
elif message.reply_to_message.document:
if message.reply_to_message.document.mime_type == "image/jpeg":
if (
message.reply_to_message.document.mime_type == 'image/jpeg'
or message.reply_to_message.document.mime_type == 'image/png'
):
final_path = await message.reply_to_message.download()
elif message.reply_to_message.document.mime_type == "image/png":
final_path = await message.reply_to_message.download()
elif message.reply_to_message.document.mime_type == "image/webp":
final_path = "webp_to_png_s_proton.png"
elif message.reply_to_message.document.mime_type == 'image/webp':
final_path = 'webp_to_png_s_proton.png'
path_s = await message.reply_to_message.download()
im = Image.open(path_s)
im.save(final_path, "PNG")
im.save(final_path, 'PNG')
else:
return None
return final_path
def remove_background(photo_data):
with open(photo_data, "rb") as image_file:
with open(photo_data, 'rb') as image_file:
image_data = image_file.read()
response = requests.post(
"https://api.remove.bg/v1.0/removebg",
files={"image_file": image_data},
data={"size": "auto"},
headers={"X-Api-Key": rmbg_key},
'https://api.remove.bg/v1.0/removebg',
files={'image_file': image_data},
data={'size': 'auto'},
headers={'X-Api-Key': rmbg_key},
)
if response.status_code == 200:
return BytesIO(response.content)
print("Error:", response.status_code, response.text)
print('Error:', response.status_code, response.text)
return None
@@ -108,26 +104,26 @@ def _check_rmbg(func):
return check_rmbg
@Client.on_message(filters.command("rmbg", prefix) & filters.me)
@Client.on_message(filters.command('rmbg', prefix) & filters.me)
@_check_rmbg
async def rmbg(client: Client, message: Message):
pablo = await edit_or_reply(message, "<code>Processing...</code>")
pablo = await edit_or_reply(message, '<code>Processing...</code>')
if not message.reply_to_message:
await pablo.edit("<code>Reply To A Image Please!</code>")
await pablo.edit('<code>Reply To A Image Please!</code>')
return
cool = await convert_to_image(message, client)
if not cool:
await pablo.edit("<code>Reply to a valid media first.</code>")
await pablo.edit('<code>Reply to a valid media first.</code>')
return
start = datetime.now()
await pablo.edit("sending to ReMove.BG")
await pablo.edit('sending to ReMove.BG')
input_file_name = cool
files = {
"image_file": (input_file_name, open(input_file_name, "rb")),
'image_file': (input_file_name, open(input_file_name, 'rb')),
}
r = requests.post(
"https://api.remove.bg/v1.0/removebg",
headers={"X-Api-Key": rmbg_key},
'https://api.remove.bg/v1.0/removebg',
headers={'X-Api-Key': rmbg_key},
files=files,
allow_redirects=True,
stream=True,
@@ -135,30 +131,23 @@ async def rmbg(client: Client, message: Message):
if os.path.exists(cool):
os.remove(cool)
output_file_name = r
contentType = output_file_name.headers.get("content-type")
if "image" in contentType:
contentType = output_file_name.headers.get('content-type')
if 'image' in contentType:
with io.BytesIO(output_file_name.content) as remove_bg_image:
remove_bg_image.name = "BG_rem.png"
await client.send_document(
message.chat.id, remove_bg_image, reply_to_message_id=message.id
)
remove_bg_image.name = 'BG_rem.png'
await client.send_document(message.chat.id, remove_bg_image, reply_to_message_id=message.id)
end = datetime.now()
ms = (end - start).seconds
await pablo.edit(
f"<code>Removed image's Background in {ms} seconds."
)
if os.path.exists("BG_rem.png"):
os.remove("BG_rem.png")
await pablo.edit(f"<code>Removed image's Background in {ms} seconds.")
if os.path.exists('BG_rem.png'):
os.remove('BG_rem.png')
else:
await pablo.edit(
"ReMove.BG API returned Errors."
+ f"\n`{output_file_name.content.decode('UTF-8')}"
)
await pablo.edit('ReMove.BG API returned Errors.' + f'\n`{output_file_name.content.decode("UTF-8")}')
@Client.on_message(filters.command("rebg", prefix) & filters.me)
@Client.on_message(filters.command('rebg', prefix) & filters.me)
async def rembg(client: Client, message: Message):
await message.edit("<code>Processing...</code>")
await message.edit('<code>Processing...</code>')
chat_id = message.chat.id
try:
try:
@@ -167,28 +156,26 @@ async def rembg(client: Client, message: Message):
try:
photo_data = await message.reply_to_message.download()
except ValueError:
await message.edit("<b>File not found</b>")
await message.edit('<b>File not found</b>')
return
background_removed_data = remove_background(photo_data)
if background_removed_data:
await message.delete()
await client.send_photo(
chat_id, photo=background_removed_data, caption="Background removed!"
)
await client.send_photo(chat_id, photo=background_removed_data, caption='Background removed!')
else:
await message.edit_text(
"`Is Your RMBG Api 'rmbg_key' Valid Or You Didn't Add It??`\n **Check logs for details**",
parse_mode=enums.ParseMode.MARKDOWN,
)
except Exception as e:
await message.reply_text(f"An error occurred: {format_exc(e)}")
await message.reply_text(f'An error occurred: {format_exc(e)}')
finally:
if os.path.exists(photo_data):
os.remove(photo_data)
modules_help["removebg"] = {
"rebg [reply to image]*": "remove background from image without transparency",
"rmbg [reply to image]*": "remove background from image with transparency",
modules_help['removebg'] = {
'rebg [reply to image]*': 'remove background from image without transparency',
'rmbg [reply to image]*': 'remove background from image with transparency',
}