images.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. __MODULE__ = "Images"
  11. __HELP__ = '''/cat - Get Cute Cats Images
  12. /wall - Get Wallpapers'''
  13. async def delete_message_with_delay(delay, message: Message):
  14. await asyncio.sleep(delay)
  15. await message.delete()
  16. @app.on_message(cust_filter.command(commands=("cat")) & ~filters.edited)
  17. async def cat(_, message: 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(cust_filter.command(commands=("wall")) & ~filters.edited)
  25. async def wall(_, message: 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. api = "https://wall.alphacoders.com/api2.0/get.php?auth=" + \
  33. "{}&method=search&term={}".format(WALL_API_KEY, term)
  34. json_rep = r.get(api).json()
  35. if not json_rep.get("success"):
  36. await m.edit("Something happened! Shit.")
  37. else:
  38. wallpapers = json_rep.get("wallpapers")
  39. if not wallpapers:
  40. await m.edit("Found literally nothing!,"
  41. + "You should work on your English.")
  42. return
  43. if len(wallpapers) > 10:
  44. selection = 10
  45. else:
  46. selection = len(wallpapers)
  47. index = randint(0, selection - 1)
  48. wallpaper = wallpapers[index]
  49. wallpaper = wallpaper.get("url_image")
  50. wallpaper = wallpaper.replace("\\", "")
  51. await message.reply_document(wallpaper)
  52. await m.delete()