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.

38 lines
1.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Public domain, 2013 Legoktm
  4. #
  5. from json import loads
  6. from urllib import quote
  7. from urllib2 import urlopen
  8. from earwigbot.commands import Command
  9. class UrbanDictionary(Command):
  10. """Get the definition of a word using Urban Dictionary."""
  11. name = "urban"
  12. commands = ["urban", "urbandictionary", "dictt", "definee", "defne", "dct",
  13. "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 = res['list'][0]['definition'].replace("\n", " ")
  24. example = res['list'][0]['example'].replace("\n", " ")
  25. msg = 'Definition: {0}; example: {1}'.format(definition, example)
  26. self.reply(data, msg)
  27. elif res['result_type'] == 'fulltext':
  28. L = [i['word'] for i in res['list']]
  29. msg = 'Here are some close matches: {0}.'.format(', '.join(L))
  30. self.reply(data, msg)
  31. else:
  32. self.reply(data, 'Sorry, no results found.')