reverse.py 3.4 KB

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