images.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. __MODULE__ = "Images"
  9. __HELP__ = '''/cat - Get Cute Cats Images
  10. /wall - Get Wallpapers'''
  11. async def delete_message_with_delay(delay, message):
  12. await asyncio.sleep(delay)
  13. await message.delete()
  14. @app.on_message(filters.command("cat") & ~filters.edited)
  15. @capture_err
  16. async def cat(_, message):
  17. with urllib.request.urlopen(
  18. "https://api.thecatapi.com/v1/images/search"
  19. ) as url:
  20. data = json.loads(url.read().decode())
  21. cat_url = (data[0]['url'])
  22. await message.reply_photo(cat_url)
  23. @app.on_message(filters.command("wall") & ~filters.edited)
  24. @capture_err
  25. async def wall(_, message):
  26. if len(message.command) < 2:
  27. await message.reply_text("/wall needs an argument")
  28. return
  29. initial_term = message.text.split(None, 1)[1]
  30. m = await message.reply_text("Searching!")
  31. term = initial_term.replace(' ', '%20')
  32. try:
  33. wallpapers = await arq.wall(term)
  34. except KeyError:
  35. await m.edit("Found literally nothing!,"
  36. + "You should work on your English.")
  37. return
  38. if len(wallpapers) > 10:
  39. selection = 10
  40. else:
  41. selection = len(wallpapers)
  42. index = randint(0, selection - 1)
  43. wallpaper = wallpapers[index].url_image
  44. await message.reply_document(wallpaper)
  45. await m.delete()