images.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from wbb.utils import cust_filter
  2. from wbb import app, WALL_API_KEY
  3. import urllib.request
  4. import json
  5. from random import randint
  6. import requests as r
  7. __MODULE__ = "Images"
  8. __HELP__ = "/cat - Get Cute Cats Images\n" + \
  9. "/dog - Get Cute Dogs Images\n" + \
  10. "/wall [something] - Get Wallpapers"
  11. @app.on_message(cust_filter.command(commands=("cat")))
  12. async def cat(client, message):
  13. with urllib.request.urlopen(
  14. "https://api.thecatapi.com/v1/images/search"
  15. ) as url:
  16. data = json.loads(url.read().decode())
  17. cat_url = (data[0]['url'])
  18. await message.reply_photo(cat_url)
  19. @app.on_message(cust_filter.command(commands=("dog")))
  20. async def dog(client, message):
  21. with urllib.request.urlopen(
  22. "https://api.thedogapi.com/v1/images/search"
  23. ) as url:
  24. data = json.loads(url.read().decode())
  25. dog_url = (data[0]['url'])
  26. await message.reply_photo(dog_url)
  27. @app.on_message(cust_filter.command(commands=("wall")))
  28. async def wall(client, message):
  29. app.set_parse_mode("markdown")
  30. m = await message.reply_text("Searching!")
  31. initial_term = (message.text.replace('/wall', ''))
  32. term = initial_term.replace(' ', '%20')
  33. api = "https://wall.alphacoders.com/api2.0/get.php?auth=" + \
  34. "{}&method=search&term={}".format(WALL_API_KEY, term)
  35. json_rep = r.get(api).json()
  36. if not json_rep.get("success"):
  37. await m.edit("FTS! something unusual happened, " +
  38. "report to support @Thepirategang")
  39. else:
  40. wallpapers = json_rep.get("wallpapers")
  41. if not wallpapers:
  42. await m.edit("404")
  43. return
  44. else:
  45. index = randint(0, len(wallpapers) - 1)
  46. wallpaper = wallpapers[index]
  47. wallpaper = wallpaper.get("url_image")
  48. wallpaper = wallpaper.replace("\\", "")
  49. await message.reply_document(wallpaper)
  50. await m.delete()