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 line
1.2 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. defin = res['list'][0]
  24. msg = 'Definition: {definition}; example: {example}'.format(**defin)
  25. self.reply(data, msg)
  26. elif res['result_type'] == 'fulltext':
  27. L = [i['word'] for i in res['list']]
  28. msg = 'Here are some close matches: {0}.'.format(', '.join(L))
  29. self.reply(data, msg)
  30. else:
  31. self.reply(data, 'Sorry, no results found.')