images.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /dog - Get Cute Dogs 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. async def cat(_, message: Message):
  19. with urllib.request.urlopen(
  20. "https://api.thecatapi.com/v1/images/search"
  21. ) as url:
  22. data = json.loads(url.read().decode())
  23. cat_url = (data[0]['url'])
  24. await message.reply_photo(cat_url)
  25. @app.on_message(cust_filter.command(commands=("dog")) & ~filters.edited)
  26. async def dog(_, message: Message):
  27. with urllib.request.urlopen(
  28. "https://api.thedogapi.com/v1/images/search"
  29. ) as url:
  30. data = json.loads(url.read().decode())
  31. dog_url = (data[0]['url'])
  32. await message.reply_photo(dog_url)
  33. @app.on_message(cust_filter.command(commands=("wall")) & ~filters.edited)
  34. async def wall(_, message: Message):
  35. if len(message.command) < 2:
  36. await message.reply_text("/wall needs an argument")
  37. return
  38. initial_term = message.text.split(None, 1)[1]
  39. m = await message.reply_text("Searching!")
  40. term = initial_term.replace(' ', '%20')
  41. api = "https://wall.alphacoders.com/api2.0/get.php?auth=" + \
  42. "{}&method=search&term={}".format(WALL_API_KEY, term)
  43. json_rep = r.get(api).json()
  44. if not json_rep.get("success"):
  45. await m.edit("Something happened! Shit.")
  46. else:
  47. wallpapers = json_rep.get("wallpapers")
  48. if not wallpapers:
  49. await m.edit("Found literally nothing!,"
  50. + "You should work on your English.")
  51. return
  52. if len(wallpapers) > 10:
  53. selection = 10
  54. else:
  55. selection = len(wallpapers)
  56. index = randint(0, selection - 1)
  57. wallpaper = wallpapers[index]
  58. wallpaper = wallpaper.get("url_image")
  59. wallpaper = wallpaper.replace("\\", "")
  60. await message.reply_document(wallpaper)
  61. await m.delete()