reverse.py 3.2 KB

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