reverse.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import os
  2. from asyncio import gather, get_running_loop
  3. from base64 import b64decode
  4. from io import BytesIO
  5. from random import randint
  6. import aiofiles
  7. import requests
  8. from bs4 import BeautifulSoup
  9. from pyrogram import filters
  10. from pyrogram.types import InputMediaPhoto, Message
  11. from wbb import MESSAGE_DUMP_CHAT, app
  12. from wbb.core.decorators.errors import capture_err
  13. from wbb.utils.functions import get_file_id_from_message
  14. from wbb.utils.http import get
  15. async def get_soup(url: str, headers):
  16. html = await get(url, headers=headers)
  17. return BeautifulSoup(html, "html.parser")
  18. @app.on_message(filters.command("reverse"))
  19. @capture_err
  20. async def reverse_image_search(_, message: Message):
  21. if not message.reply_to_message:
  22. return await message.reply_text(
  23. "Reply to a message to reverse search it."
  24. )
  25. reply = message.reply_to_message
  26. if (
  27. not reply.document
  28. and not reply.photo
  29. and not reply.sticker
  30. and not reply.animation
  31. and not reply.video
  32. ):
  33. return await message.reply_text(
  34. "Reply to an image/document/sticker/animation to reverse search it."
  35. )
  36. m = await message.reply_text("Searching...")
  37. file_id = get_file_id_from_message(reply)
  38. if not file_id:
  39. return await m.edit("Can't reverse that")
  40. image = await app.download_media(
  41. file_id, f"{randint(1000, 10000)}.jpg"
  42. )
  43. async with aiofiles.open(image, "rb") as f:
  44. if image:
  45. search_url = "http://www.google.com/searchbyimage/upload"
  46. multipart = {
  47. "encoded_image": (image, await f.read()),
  48. "image_content": "",
  49. }
  50. def post_non_blocking():
  51. return requests.post(
  52. search_url, files=multipart, allow_redirects=False
  53. )
  54. loop = get_running_loop()
  55. response = await loop.run_in_executor(
  56. None, post_non_blocking
  57. )
  58. location = response.headers.get("Location")
  59. os.remove(image)
  60. else:
  61. return await m.edit("Something wrong happened.")
  62. headers = {
  63. "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0"
  64. }
  65. soup = await get_soup(location, headers=headers)
  66. div = soup.find_all("div", {"class": "r5a77d"})[0]
  67. text = div.find("a").text
  68. url = "https://google.com" + soup.find_all(
  69. "a", {"class": "ekf0x hSQtef"}
  70. )[0].get("href")
  71. soup = await get_soup(url, headers=headers)
  72. text = f"**Result**: [{text}]({location})"
  73. media = []
  74. for img in soup.find_all("img"):
  75. if len(media) == 2:
  76. break
  77. if img.get("src"):
  78. img = img.get("src")
  79. if "image/gif" in img:
  80. continue
  81. img = BytesIO(b64decode(img))
  82. img.name = "img.png"
  83. media.append(img)
  84. elif img.get("data-src"):
  85. img = img.get("data-src")
  86. media.append(img)
  87. # Cache images, so we can use file_ids
  88. tasks = [app.send_photo(MESSAGE_DUMP_CHAT, img) for img in media]
  89. messages = await gather(*tasks)
  90. await message.reply_media_group(
  91. [
  92. InputMediaPhoto(
  93. i.photo.file_id,
  94. caption=text,
  95. )
  96. for i in messages
  97. ]
  98. )
  99. await m.edit(text)