# CREATED BY: @Qewertyy
# <============================================== IMPORTS =========================================================>
import os
import traceback
from pyrogram import Client, filters
from pyrogram import types as t
from Mikobot import app
from Mikobot.state import state
from .telegraph import telegraph, upload_file
# <================================================ FUNCTIONS =====================================================>
@app.on_message(filters.command(["p", "pp", "reverse", "sauce"]))
async def reverseImageSearch(_: Client, m: t.Message):
try:
reply = await m.reply_text("`Downloading...`")
file = None
if not m.reply_to_message:
return await reply.edit("Reply to an image?")
if m.reply_to_message.document is False or m.reply_to_message.photo is False:
return await reply.edit("Reply to an image?")
if (
m.reply_to_message.document
and m.reply_to_message.document.mime_type
in ["image/png", "image/jpg", "image/jpeg"]
or m.reply_to_message.photo
):
if (
m.reply_to_message.document
and m.reply_to_message.document.file_size > 5242880
):
return await reply.edit("Reply to an image?")
file = await m.reply_to_message.download()
else:
return await reply.edit("Reply to an image?")
await reply.edit("`Uploading to the server...`")
imgUrl = upload_file(file)
os.remove(file)
if imgUrl is None:
return await reply.edit("Ran into an error.")
output = await reverse_image_search("google", f"https://graph.org/{imgUrl[0]}")
if output is None:
return await reply.edit("Ran into an error.")
names = output["content"]["bestResults"]["names"]
urls = output["content"]["bestResults"]["urls"]
btn = t.InlineKeyboardMarkup(
[[t.InlineKeyboardButton(text="IMAGE URL", url=urls[-1])]]
)
if len(names) > 10:
message = "\n".join(
[f"{index+1}. {name}" for index, name in enumerate(names[:10])]
)
htmlMessage = f"
".join(
[f"{index+1}. {name}" for index, name in enumerate(names)]
)
htmlMessage += "