images.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import urllib.request
  2. import json
  3. from random import randint
  4. import asyncio
  5. import requests as r
  6. from pyrogram.types import Message
  7. from wbb.utils import cust_filter
  8. from wbb import app, WALL_API_KEY
  9. from pyrogram import filters
  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. app.set_parse_mode("markdown")
  36. m = await message.reply_text("Searching!")
  37. initial_term = (message.text.replace('/wall', ''))
  38. term = initial_term.replace(' ', '%20')
  39. api = "https://wall.alphacoders.com/api2.0/get.php?auth=" + \
  40. "{}&method=search&term={}".format(WALL_API_KEY, term)
  41. json_rep = r.get(api).json()
  42. if not json_rep.get("success"):
  43. await m.edit("`\"/wall\"` Needs A Keyword Argument!")
  44. else:
  45. wallpapers = json_rep.get("wallpapers")
  46. if not wallpapers:
  47. await m.edit("Found literally nothing!,"
  48. + "You should work on your English.")
  49. return
  50. if len(wallpapers) > 10:
  51. selection = 10
  52. else:
  53. selection = len(wallpapers)
  54. index = randint(0, selection - 1)
  55. wallpaper = wallpapers[index]
  56. wallpaper = wallpaper.get("url_image")
  57. wallpaper = wallpaper.replace("\\", "")
  58. await message.reply_document(wallpaper)
  59. await delete_message_with_delay(10, m)