Files
Ultroid-fork/plugins/google.py
Anonymous 4e8b0543ce Ultroid 0.0.5 - 06/04/21
Co-authored-by: AndrewLaneX <AndrewLaneX@users.noreply.github.com>
Co-authored-by: Aditya <me@xditya.me>
Co-authored-by: Danish <danish@ultroid.tech>
Co-authored-by: buddhhu <buddhuu0@users.noreply.github.com>
Co-authored-by: sppidy <sppidy@users.noreply.github.com>
Co-authored-by: Arnab Paryali <Arnabxd@users.noreply.github.com>
Co-authored-by: divkix <divkix@users.noreply.github.com>
Co-authored-by: hellboi_atul <hellboi-atul@users.noreply.github.com>
Co-authored-by: Programming Error <error@notavailable.live>
Co-authored-by: New-dev0 <New-dev0@notavailable.live>
2021-04-07 07:32:24 +05:30

133 lines
3.6 KiB
Python

# Ultroid - UserBot
# Copyright (C) 2020 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available -
• `{i}google <query>`
For doing google search.
• `{i}img <query>`
For doing Images search.
• `{i}reverse <query>`
Reply an Image or sticker to find its sauce.
"""
import os
from shutil import rmtree
import requests
from bs4 import BeautifulSoup as bs
from PIL import Image
from search_engine_parser import GoogleSearch
from strings import get_string
from . import *
@ultroid_cmd(pattern="google ?(.*)")
async def google(event):
inp = event.pattern_match.group(1)
if not inp:
return await event.edit("`Give something to search..`")
x = await eor(event, get_string("com_2"))
gs = GoogleSearch()
res = await gs.async_search(f"{inp}", cache=False)
out = ""
for i in range(len(res["links"])):
text = res["titles"][i]
url = res["links"][i]
des = res["descriptions"][i]
out += f" 👉🏻 [{text}]({url})\n`{des}`\n\n"
omk = f"**Google Search Query:**\n`{inp}`\n\n**Results:**\n{out}"
opn = []
for bkl in range(0, len(omk), 4095):
opn.append(omk[bkl : bkl + 4095])
for bc in opn:
await ultroid_bot.send_message(event.chat_id, bc, link_preview=False)
await x.delete()
opn.clear()
@ultroid_cmd(pattern="img ?(.*)")
async def goimg(event):
query = event.pattern_match.group(1)
if not query:
return await eor(event, "`Give something to search...`")
nn = await eor(event, "`Processing Keep Patience...`")
if ";" in query:
try:
lmt = int(query.split(";")[1])
except BaseExceptaion:
lmt = 5
else:
lmt = 5
gi = googleimagesdownload()
args = {
"keywords": query,
"limit": lmt,
"format": "jpg",
"output_directory": "./resources/downloads/",
}
pth = gi.download(args)
ok = pth[0][query]
await event.client.send_file(event.chat_id, ok, album=True)
rmtree(f"./resources/downloads/{query}/")
await nn.delete()
@ultroid_cmd(pattern="reverse")
async def reverse(event):
reply = await event.get_reply_message()
if not reply:
return await eor(event, "`Reply to an Image`")
ult = await eor(event, "`Processing...`")
dl = await bot.download_media(reply)
img = Image.open(dl)
x, y = img.size
file = {"encoded_image": (dl, open(dl, "rb"))}
grs = requests.post(
"https://www.google.com/searchbyimage/upload",
files=file,
allow_redirects=False,
)
loc = grs.headers.get("Location")
response = requests.get(
loc,
headers={
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0",
},
)
xx = bs(response.text, "html.parser")
div = xx.find_all("div", {"class": "r5a77d"})[0]
alls = div.find("a")
link = alls["href"]
text = alls.text
await ult.edit(f"`Dimension ~ {x} : {y}`\nSauce ~ [{text}](google.com{link})")
gi = googleimagesdownload()
args = {
"keywords": text,
"limit": 2,
"format": "jpg",
"output_directory": "./resources/downloads/",
}
pth = gi.download(args)
ok = pth[0][text]
await event.client.send_file(
event.chat_id,
ok,
album=True,
caption="Similar Images Realted to Search",
)
rmtree(f"./resources/downloads/{text}/")
os.remove(dl)
HELP.update({f"{__name__.split('.')[1]}": f"{__doc__.format(i=HNDLR)}"})