reverse.py 3.4 KB

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