reverse.py 3.3 KB

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