reverse.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import os
  2. from random import randint
  3. import aiofiles
  4. import aiohttp
  5. import requests
  6. from bs4 import BeautifulSoup
  7. from pyrogram import filters
  8. from wbb import SUDOERS, app
  9. from wbb.core.decorators.errors import capture_err
  10. __MODULE__ = "Reverse"
  11. __HELP__ = "/reverse - Reverse search an image. [SUDOERS ONLY]"
  12. @app.on_message(filters.command("reverse"))
  13. @capture_err
  14. async def reverse_image_search(_, message):
  15. if message.from_user.id not in SUDOERS:
  16. return await message.reply_text("THIS FEATURE IS ONLY FOR SUDO USERS.")
  17. if not message.reply_to_message:
  18. return await message.reply_text("Reply to a message to reverse search it.")
  19. reply = message.reply_to_message
  20. if (
  21. not reply.document
  22. and not reply.photo
  23. and not reply.sticker
  24. and not reply.animation
  25. and not reply.video
  26. ):
  27. return await message.reply_text(
  28. "Reply to an image/document/sticker/animation to reverse search it."
  29. )
  30. m = await message.reply_text("Searching")
  31. if reply.document:
  32. if int(reply.document.file_size) > 3145728:
  33. return await m.edit("File too large")
  34. mime_type = reply.document.mime_type
  35. if mime_type != "image/png" and mime_type != "image/jpeg":
  36. return await m.edit("Document Mimetype Invalid")
  37. file_id = reply.document.file_id
  38. if reply.sticker:
  39. if reply.sticker.is_animated:
  40. if not reply.sticker.thumbs:
  41. return await m.edit("Sticker Has No Thumb")
  42. file_id = reply.sticker.thumbs[0].file_id
  43. else:
  44. file_id = reply.sticker.file_id
  45. if reply.photo:
  46. file_id = reply.photo.file_id
  47. if reply.animation:
  48. if not reply.animation.thumbs:
  49. return await m.edit(
  50. "Gif Has No Thumbnail, so it cannot be reverse searched"
  51. )
  52. file_id = reply.animation.thumbs[0].file_id
  53. if reply.video:
  54. if not reply.video.thumbs:
  55. return
  56. file_id = reply.video.thumbs[0].file_id
  57. image = await app.download_media(file_id, f"{randint(1000, 10000)}.jpg")
  58. async with aiofiles.open(image, "rb") as f:
  59. if image:
  60. search_url = "http://www.google.com/searchbyimage/upload"
  61. multipart = {
  62. "encoded_image": (image, await f.read()),
  63. "image_content": "",
  64. }
  65. response = requests.post(
  66. search_url, files=multipart, allow_redirects=False
  67. )
  68. location = response.headers.get("Location")
  69. os.remove(image)
  70. else:
  71. return await m.edit("Something wrong happened.")
  72. headers = {
  73. "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0"
  74. }
  75. async with aiohttp.ClientSession() as session:
  76. async with session.get(location, headers=headers) as resp:
  77. soup = BeautifulSoup(await resp.text(), "html.parser")
  78. div = soup.find_all("div", {"class": "r5a77d"})[0]
  79. anchor_element = div.find("a")
  80. text = anchor_element.text
  81. try:
  82. await app.send_photo(
  83. message.chat.id,
  84. photo=f"https://webshot.amanoteam.com/print?q={location}",
  85. caption=f"**Query** [{text}]({location})",
  86. )
  87. await m.delete()
  88. except Exception:
  89. text = f"**Result**: [Link]({location})"
  90. await m.edit(text)