reverse.py 3.3 KB

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