Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 11 години
преди 11 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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_term(term):
  16. return re.sub(r"\W", "", term.lower())
  17. @staticmethod
  18. def _normalize_text(text):
  19. return re.sub(r"\[(.*?)\]", "\\1", re.sub(r"\s+", " ", text))
  20. def process(self, data):
  21. if not data.args:
  22. self.reply(data, "What do you want to define?")
  23. return
  24. url = "http://api.urbandictionary.com/v0/define?term={0}"
  25. arg = " ".join(data.args)
  26. query = urlopen(url.format(quote(arg, safe=""))).read()
  27. res = loads(query)
  28. results = res.get("list")
  29. if not results:
  30. self.reply(data, 'Sorry, no results found.')
  31. return
  32. result = results[0]
  33. definition = self._normalize_text(result["definition"])
  34. example = self._normalize_text(result["example"])
  35. url = result["permalink"].rsplit("/", 1)[0]
  36. if definition and definition[-1] not in (".", "!", "?"):
  37. definition += "."
  38. msg = "{0} \x02Example\x0F: {1} {2}".format(
  39. definition.encode("utf8"), example.encode("utf8"), url)
  40. if self._normalize_term(result["word"]) != self._normalize_term(arg):
  41. msg = "\x02{0}\x0F: {1}".format(result["word"].encode("utf8"), msg)
  42. self.reply(data, msg)