Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

urbandictionary.py 1.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Public domain, 2013 Legoktm; 2013, 2018 Ben Kurtovic
  4. #
  5. from json import loads
  6. import re
  7. from urllib import quote
  8. from urllib2 import urlopen
  9. from earwigbot.commands import Command
  10. class UrbanDictionary(Command):
  11. """Get the definition of a word or phrase using Urban Dictionary."""
  12. name = "urban"
  13. commands = ["urban", "urbandictionary", "dct", "ud"]
  14. @staticmethod
  15. def _normalize(word):
  16. return re.sub(r"\W", "", word.lower())
  17. def process(self, data):
  18. if not data.args:
  19. self.reply(data, "What do you want to define?")
  20. return
  21. url = "http://api.urbandictionary.com/v0/define?term={0}"
  22. arg = " ".join(data.args)
  23. query = urlopen(url.format(quote(arg, safe=""))).read()
  24. res = loads(query)
  25. results = res.get("list")
  26. if not results:
  27. self.reply(data, 'Sorry, no results found.')
  28. return
  29. result = results[0]
  30. definition = re.sub(r"\s+", " ", result["definition"])
  31. example = re.sub(r"\s+", " ", result["example"])
  32. url = result["permalink"].rsplit("/", 1)[0]
  33. if definition and definition[-1] not in (".", "!", "?"):
  34. definition += "."
  35. msg = "{0} \x02Example\x0F: {1} {2}".format(
  36. definition.encode("utf8"), example.encode("utf8"), url)
  37. if self._normalize(result["word"]) != self._normalize(arg):
  38. msg = "\x02{0}\x0F: {1}".format(result["word"].encode("utf8"), msg)
  39. self.reply(data, msg)