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.

37 lines
1.1 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 real definition of a word."""
  11. name = "urban"
  12. commands = ["dictt", "definee", "defne", "dct", "ud"]
  13. def process(self, data):
  14. if not data.args:
  15. return
  16. q = ' '.join(data.args)
  17. url = "http://api.urbandictionary.com/v0/define?term={}"
  18. q = quote(q, safe="")
  19. query = urlopen(url.format(q)).read()
  20. res = loads(query)
  21. if res['result_type'] == 'exact':
  22. defin = res['list'][0]
  23. msg = 'Definition: {definition}; example: {example}'.format(**defin)
  24. self.reply(data, msg)
  25. elif res['result_type'] == 'fulltext':
  26. l = [i['word'] for i in res['list']]
  27. msg = 'Here are some close matches...: {0}'.format(', '.join(l))
  28. self.reply(data, msg)
  29. else:
  30. self.reply(data, 'Sorry, no results found :(')