images.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import urllib.request
  2. import json
  3. import asyncio
  4. from random import randint
  5. from pyrogram import filters
  6. from wbb import app, ARQ
  7. from wbb.utils.errors import capture_err
  8. from wbb.utils.fetch import fetch
  9. __MODULE__ = "Images"
  10. __HELP__ = '''/cat - Get Cute Cats Images
  11. /wall - Get Wallpapers'''
  12. async def delete_message_with_delay(delay, message):
  13. await asyncio.sleep(delay)
  14. await message.delete()
  15. @app.on_message(filters.command("cat") & ~filters.edited)
  16. @capture_err
  17. async def cat(_, message):
  18. with urllib.request.urlopen(
  19. "https://api.thecatapi.com/v1/images/search"
  20. ) as url:
  21. data = json.loads(url.read().decode())
  22. cat_url = (data[0]['url'])
  23. await message.reply_photo(cat_url)
  24. @app.on_message(filters.command("wall") & ~filters.edited)
  25. @capture_err
  26. async def wall(_, message):
  27. if len(message.command) < 2:
  28. await message.reply_text("/wall needs an argument")
  29. return
  30. initial_term = message.text.split(None, 1)[1]
  31. m = await message.reply_text("Searching!")
  32. term = initial_term.replace(' ', '%20')
  33. json_rep = await fetch(f"{ARQ}wall?query={term}")
  34. if not json_rep.get("success"):
  35. await m.edit("Something happened! Shit.")
  36. else:
  37. wallpapers = json_rep.get("wallpapers")
  38. if not wallpapers:
  39. await m.edit("Found literally nothing!,"
  40. + "You should work on your English.")
  41. return
  42. if len(wallpapers) > 10:
  43. selection = 10
  44. else:
  45. selection = len(wallpapers)
  46. index = randint(0, selection - 1)
  47. wallpaper = wallpapers[index]
  48. wallpaper = wallpaper.get("url_image")
  49. wallpaper = wallpaper.replace("\\", "")
  50. await message.reply_document(wallpaper)
  51. await m.delete()