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.

42 lines
1.4 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. #
  4. # Public domain, 2013 Legoktm
  5. #
  6. from json import loads
  7. import re
  8. from urllib import quote
  9. from urllib2 import urlopen
  10. from earwigbot.commands import Command
  11. class UrbanDictionary(Command):
  12. """Get the definition of a word or phrase using Urban Dictionary."""
  13. name = "urban"
  14. commands = ["urban", "urbandictionary", "dictt", "definee", "defne", "dct",
  15. "ud"]
  16. def process(self, data):
  17. if not data.args:
  18. self.reply(data, "What do you want to define?")
  19. return
  20. url = "http://api.urbandictionary.com/v0/define?term={0}"
  21. q = quote(' '.join(data.args), safe="")
  22. query = urlopen(url.format(q)).read()
  23. res = loads(query)
  24. if res['result_type'] == 'exact':
  25. definition = re.sub(r"\s+", " ", res['list'][0]['definition'])
  26. example = re.sub(r"\s+", " ", res['list'][0]['example'])
  27. if definition and definition[-1] not in (".", "!", "?"):
  28. definition += "."
  29. msg = '{0} \x02Example\x0F: {1}'.format(definition, example)
  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}.'.format(', '.join(L))
  34. self.reply(data, msg)
  35. else:
  36. self.reply(data, 'Sorry, no results found.')