reverse.py 3.1 KB

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