Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

urbandictionary.py 1.5 KiB

hace 8 años
hace 8 años
hace 11 años
hace 11 años
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Public domain, 2013 Legoktm
  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. def process(self, data):
  15. if not data.args:
  16. self.reply(data, "What do you want to define?")
  17. return
  18. url = "http://api.urbandictionary.com/v0/define?term={0}"
  19. q = quote(' '.join(data.args), safe="")
  20. query = urlopen(url.format(q)).read()
  21. res = loads(query)
  22. if res['result_type'] == 'exact':
  23. definition = re.sub(r"\s+", " ", res['list'][0]['definition'])
  24. example = re.sub(r"\s+", " ", res['list'][0]['example'])
  25. url = res['list'][0]['permalink'].rsplit('/', 1)[0]
  26. if definition and definition[-1] not in (".", "!", "?"):
  27. definition += "."
  28. msg = '{0} \x02Example\x0F: {1} {2}'.format(
  29. definition.encode("utf8"), example.encode("utf8"), url)
  30. self.reply(data, msg)
  31. elif res['result_type'] == 'fulltext':
  32. L = [i['word'] for i in res['list']]
  33. msg = 'Here are some close matches: {0}.'
  34. self.reply(data, msg.format(u", ".join(L).encode("utf8")))
  35. else:
  36. self.reply(data, 'Sorry, no results found.')