images.py 2.0 KB

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