Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

39 linhas
1.3 KiB

  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 using Urban Dictionary."""
  12. name = "urban"
  13. commands = ["urban", "urbandictionary", "dictt", "definee", "defne", "dct",
  14. "ud"]
  15. def process(self, data):
  16. if not data.args:
  17. self.reply(data, "What do you want to define?")
  18. return
  19. url = "http://api.urbandictionary.com/v0/define?term={0}"
  20. q = quote(' '.join(data.args), safe="")
  21. query = urlopen(url.format(q)).read()
  22. res = loads(query)
  23. if res['result_type'] == 'exact':
  24. definition = re.sub(r"\s+", " ", res['list'][0]['definition'])
  25. example = re.sub(r"\s+", " ", res['list'][0]['example'])
  26. msg = '{0}; example: {1}'.format(definition, example)
  27. self.reply(data, msg)
  28. elif res['result_type'] == 'fulltext':
  29. L = [i['word'] for i in res['list']]
  30. msg = 'Here are some close matches: {0}.'.format(', '.join(L))
  31. self.reply(data, msg)
  32. else:
  33. self.reply(data, 'Sorry, no results found.')