reverse.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """
  2. MIT License
  3. Copyright (c) 2024 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import os
  21. from asyncio import gather
  22. from base64 import b64decode
  23. from io import BytesIO
  24. from random import randint
  25. import aiofiles
  26. import httpx
  27. from bs4 import BeautifulSoup
  28. from pyrogram import filters
  29. from pyrogram.types import InputMediaPhoto, Message
  30. from wbb import MESSAGE_DUMP_CHAT, SUDOERS, USERBOT_PREFIX, app, app2, eor
  31. from wbb.core.decorators.errors import capture_err
  32. from wbb.utils.functions import get_file_id_from_message
  33. from wbb.utils.http import get
  34. async def get_soup(url: str, headers):
  35. html = await get(url, headers=headers)
  36. return BeautifulSoup(html, "html.parser")
  37. @app2.on_message(
  38. filters.command("reverse", prefixes=USERBOT_PREFIX)
  39. & ~filters.forwarded
  40. & ~filters.via_bot
  41. & SUDOERS
  42. )
  43. @app.on_message(filters.command("reverse"))
  44. @capture_err
  45. async def reverse_image_search(client, message: Message):
  46. if not message.reply_to_message:
  47. return await eor(message, text="Reply to a message to reverse search it.")
  48. reply = message.reply_to_message
  49. if (
  50. not reply.document
  51. and not reply.photo
  52. and not reply.sticker
  53. and not reply.animation
  54. and not reply.video
  55. ):
  56. return await eor(
  57. message,
  58. text="Reply to an image/document/sticker/animation to reverse search it.",
  59. )
  60. m = await eor(message, text="Searching...")
  61. file_id = get_file_id_from_message(reply)
  62. if not file_id:
  63. return await m.edit("Can't reverse that")
  64. image = await client.download_media(file_id, f"{randint(1000, 10000)}.jpg")
  65. async with aiofiles.open(image, "rb") as f:
  66. if image:
  67. search_url = "http://www.google.com/searchbyimage/upload"
  68. multipart = {"encoded_image": (image, await f.read())}
  69. async with httpx.AsyncClient(follow_redirects=False) as client_session:
  70. response = await client_session.post(
  71. search_url,
  72. data={"image_content": ""},
  73. files=multipart,
  74. )
  75. location = response.headers.get("Location")
  76. os.remove(image)
  77. else:
  78. return await m.edit("Something wrong happened.")
  79. headers = {
  80. "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0"
  81. }
  82. try:
  83. soup = await get_soup(location, headers=headers)
  84. div = soup.find_all("div", {"class": "r5a77d"})[0]
  85. text = div.find("a").text
  86. text = f"**Result**: [{text}]({location})"
  87. except Exception:
  88. return await m.edit(
  89. f"**Result**: [Link]({location})",
  90. disable_web_page_preview=True,
  91. )
  92. # Pass if no images detected
  93. try:
  94. url = "https://google.com" + soup.find_all("a", {"class": "ekf0x hSQtef"})[
  95. 0
  96. ].get("href")
  97. soup = await get_soup(url, headers=headers)
  98. media = []
  99. for img in soup.find_all("img"):
  100. if len(media) == 2:
  101. break
  102. if img.get("src"):
  103. img = img.get("src")
  104. if "image/gif" in img:
  105. continue
  106. img = BytesIO(b64decode(img))
  107. img.name = "img.png"
  108. media.append(img)
  109. elif img.get("data-src"):
  110. img = img.get("data-src")
  111. media.append(img)
  112. # Cache images, so we can use file_ids
  113. tasks = [client.send_photo(MESSAGE_DUMP_CHAT, img) for img in media]
  114. messages = await gather(*tasks)
  115. await message.reply_media_group(
  116. [
  117. InputMediaPhoto(
  118. i.photo.file_id,
  119. caption=text,
  120. )
  121. for i in messages
  122. ]
  123. )
  124. except Exception:
  125. pass
  126. await m.edit(
  127. text,
  128. disable_web_page_preview=True,
  129. )